Skip to content

Commit c02cd22

Browse files
authored
Merge pull request #7 from bambamboole/switch-to-brick-varexporter
Switch to brick/varexporter
2 parents 285c581 + c403b53 commit c02cd22

9 files changed

+139
-38
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
],
1717
"require": {
1818
"php": "^8.1",
19+
"brick/varexporter": "^0.6.0",
1920
"illuminate/support": "^8.0|^9.0|^10.0|^11.0",
2021
"illuminate/translation": "^8.0|^9.0|^10.0|^11.0"
2122
},

src/ArrayExporter.php

+7-9
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
namespace Bambamboole\LaravelTranslationDumper;
44

5+
use Brick\VarExporter\VarExporter;
6+
57
class ArrayExporter
68
{
7-
public function export(array $array): string
9+
public static function export(array $array): string
810
{
9-
$export = var_export($array, true);
10-
$export = preg_replace('/^([ ]*)(.*)/m', '$1$1$2', $export);
11-
$array = preg_split("/\r\n|\n|\r/", $export);
12-
$array = preg_replace(['/\s*array\s\($/', '/\)(,)?$/', '/\s=>\s$/'], [null, ']$1', ' => ['], $array);
13-
14-
return "<?php declare(strict_types=1);\n\nreturn "
15-
.implode(PHP_EOL, array_filter(['['] + $array))
16-
.';'."\n";
11+
return implode("\n", [
12+
"<?php declare(strict_types=1);\n",
13+
VarExporter::export($array, VarExporter::ADD_RETURN | VarExporter::TRAILING_COMMA_IN_ARRAY),
14+
]);
1715
}
1816
}

src/LaravelTranslationDumperServiceProvider.php

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ public function register(): void
2828
TranslationDumper::class,
2929
static fn (Application $app) => new TranslationDumper(
3030
new Filesystem,
31-
new ArrayExporter,
3231
$app->langPath(),
3332
$app->make(Repository::class)->get('app.locale'),
3433
),

src/TranslationDumper.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class TranslationDumper implements TranslationDumperInterface
99
{
1010
public function __construct(
1111
private readonly Filesystem $filesystem,
12-
private readonly ArrayExporter $exporter,
1312
private readonly string $languageFilePath,
1413
private string $locale,
1514
) {}
@@ -44,9 +43,8 @@ private function dumpPhpTranslations(array $translations): void
4443
$file = "{$path}/{$key}.php";
4544
$keys = $this->mergeWithExistingKeys($file, $value);
4645

47-
$content = $this->exporter->export($keys);
4846
$this->filesystem->ensureDirectoryExists($path);
49-
$this->filesystem->put($file, $content);
47+
$this->filesystem->put($file, ArrayExporter::export($keys));
5048
}
5149
}
5250

tests/Feature/TranslationDumperTest.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Bambamboole\LaravelTranslationDumper\Tests\Feature;
44

5-
use Bambamboole\LaravelTranslationDumper\ArrayExporter;
65
use Bambamboole\LaravelTranslationDumper\DTO\Translation;
76
use Bambamboole\LaravelTranslationDumper\TranslationDumper;
87
use Illuminate\Filesystem\Filesystem;
@@ -12,7 +11,7 @@ class TranslationDumperTest extends TestCase
1211
{
1312
private const TEST_LANGUAGE_PATH = __DIR__.'/fixtures/lang';
1413

15-
public function testItDumpsKeysAsExpected(): void
14+
public function test_it_dumps_keys_as_expected(): void
1615
{
1716
$fs = new Filesystem;
1817
$testFolderName = uniqid();
@@ -44,7 +43,6 @@ private function createSubject(string $folder): TranslationDumper
4443
{
4544
return new TranslationDumper(
4645
new Filesystem,
47-
new ArrayExporter,
4846
$folder,
4947
'en',
5048
);

tests/Unit/ArrayExporterTest.php

+121-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class ArrayExporterTest extends TestCase
99
{
10-
public function testItCanDumpArraysInShortSyntax(): void
10+
public function test_it_can_dump_arrays_in_short_syntax(): void
1111
{
1212
$in = [
1313
'foo' => 'bar',
@@ -27,6 +27,125 @@ public function testItCanDumpArraysInShortSyntax(): void
2727

2828
EOT;
2929

30-
self::assertEquals($out, (new ArrayExporter)->export($in));
30+
self::assertEquals($out, ArrayExporter::export($in));
31+
}
32+
33+
public function test_it_omits_index_keys(): void
34+
{
35+
$in = [
36+
'foo' => 'bar',
37+
'baz' => [
38+
'buzz' => [
39+
'light year',
40+
'light month',
41+
],
42+
],
43+
];
44+
$out = <<<'EOT'
45+
<?php declare(strict_types=1);
46+
47+
return [
48+
'foo' => 'bar',
49+
'baz' => [
50+
'buzz' => [
51+
'light year',
52+
'light month',
53+
],
54+
],
55+
];
56+
57+
EOT;
58+
59+
self::assertEquals($out, ArrayExporter::export($in));
60+
}
61+
62+
public function test_it_handles_mixed_arrays(): void
63+
{
64+
$in = [
65+
'foo' => 'bar',
66+
'indexed' => [
67+
'first',
68+
'second',
69+
'third',
70+
],
71+
'mixed' => [
72+
'string_key' => 'value',
73+
42 => 'numeric key',
74+
'another' => [
75+
'nested',
76+
'values',
77+
'key' => 'with value',
78+
],
79+
],
80+
'empty_array' => [],
81+
0 => 'zero',
82+
1 => 'one',
83+
];
84+
85+
$out = <<<'EOT'
86+
<?php declare(strict_types=1);
87+
88+
return [
89+
'foo' => 'bar',
90+
'indexed' => [
91+
'first',
92+
'second',
93+
'third',
94+
],
95+
'mixed' => [
96+
'string_key' => 'value',
97+
42 => 'numeric key',
98+
'another' => [
99+
0 => 'nested',
100+
1 => 'values',
101+
'key' => 'with value',
102+
],
103+
],
104+
'empty_array' => [],
105+
0 => 'zero',
106+
1 => 'one',
107+
];
108+
109+
EOT;
110+
111+
self::assertEquals($out, ArrayExporter::export($in));
112+
}
113+
114+
public function test_it_handles_non_sequential_numeric_keys(): void
115+
{
116+
$in = [
117+
'sparse' => [
118+
5 => 'five',
119+
10 => 'ten',
120+
15 => 'fifteen',
121+
],
122+
'reindexed' => [
123+
'first',
124+
1 => 'second',
125+
3 => 'fourth',
126+
2 => 'third',
127+
],
128+
];
129+
130+
$out = <<<'EOT'
131+
<?php declare(strict_types=1);
132+
133+
return [
134+
'sparse' => [
135+
5 => 'five',
136+
10 => 'ten',
137+
15 => 'fifteen',
138+
],
139+
'reindexed' => [
140+
0 => 'first',
141+
1 => 'second',
142+
3 => 'fourth',
143+
2 => 'third',
144+
],
145+
];
146+
147+
EOT;
148+
149+
self::assertEquals($out, ArrayExporter::export($in));
31150
}
32151
}

tests/Unit/DumpingTranslatorTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function setUp(): void
2929
$this->translationDumper = $this->createMock(TranslationDumperInterface::class);
3030
}
3131

32-
public function testItProxiesGetAsExpected(): void
32+
public function test_it_proxies_get_as_expected(): void
3333
{
3434
$this->translator
3535
->expects($this->once())
@@ -48,7 +48,7 @@ public function testItProxiesGetAsExpected(): void
4848
);
4949
}
5050

51-
public function testItProxiesChoiceAsExpected(): void
51+
public function test_it_proxies_choice_as_expected(): void
5252
{
5353
$this->translator
5454
->expects($this->once())
@@ -67,7 +67,7 @@ public function testItProxiesChoiceAsExpected(): void
6767
);
6868
}
6969

70-
public function testItProxiesGetLocaleAsExpected(): void
70+
public function test_it_proxies_get_locale_as_expected(): void
7171
{
7272
$this->translator
7373
->expects($this->once())
@@ -77,7 +77,7 @@ public function testItProxiesGetLocaleAsExpected(): void
7777
self::assertEquals(self::TEST_LOCALE, $this->createDumpingTranslator()->getLocale());
7878
}
7979

80-
public function testItProxiesSetLocaleAsExpected(): void
80+
public function test_it_proxies_set_locale_as_expected(): void
8181
{
8282
$this->translationDumper
8383
->expects($this->once())
@@ -91,7 +91,7 @@ public function testItProxiesSetLocaleAsExpected(): void
9191
$this->createDumpingTranslator()->setLocale('de');
9292
}
9393

94-
public function testItCallsTheTranslationDumperOnDestruct(): void
94+
public function test_it_calls_the_translation_dumper_on_destruct(): void
9595
{
9696
$this->translator
9797
->expects($this->once())

tests/Unit/LaravelTranslationDumperServiceProviderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function setUp(): void
1616
$this->app = $this->createMock(Application::class);
1717
}
1818

19-
public function testItDoesOnlyPublishConfigIfAppIsRunningInConsole(): void
19+
public function test_it_does_only_publish_config_if_app_is_running_in_console(): void
2020
{
2121
$this->app
2222
->expects($this->once())

tests/Unit/TranslationDumperTest.php

+2-14
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,18 @@ class TranslationDumperTest extends TestCase
1717

1818
private MockObject|Filesystem $filesystem;
1919

20-
private MockObject|ArrayExporter $exporter;
21-
2220
protected function setUp(): void
2321
{
2422
$this->filesystem = $this->createMock(Filesystem::class);
25-
$this->exporter = $this->createMock(ArrayExporter::class);
2623
}
2724

2825
/** @dataProvider provideTestData */
29-
public function testItDumpsDottedKeysAsExpected(array $given, array $expected): void
26+
public function test_it_dumps_dotted_keys_as_expected(array $given, array $expected): void
3027
{
3128
if (empty($expected)) {
3229
$this->filesystem
3330
->expects($this->never())
3431
->method('exists');
35-
$this->exporter
36-
->expects($this->never())
37-
->method('export');
3832
$this->filesystem
3933
->expects($this->never())
4034
->method('put');
@@ -46,15 +40,10 @@ public function testItDumpsDottedKeysAsExpected(array $given, array $expected):
4640
->method('exists')
4741
->with($file)
4842
->willReturn(false);
49-
$this->exporter
50-
->expects($this->once())
51-
->method('export')
52-
->with($expectedArray)
53-
->willReturn('test');
5443
$this->filesystem
5544
->expects($this->once())
5645
->method('put')
57-
->with($file, 'test');
46+
->with($file, ArrayExporter::export($expectedArray));
5847
}
5948
}
6049

@@ -75,7 +64,6 @@ private function createTranslationDumper(): TranslationDumper
7564
{
7665
return new TranslationDumper(
7766
$this->filesystem,
78-
$this->exporter,
7967
self::TEST_LANGUAGE_FILE_PATH,
8068
self::TEST_LOCALE,
8169
);

0 commit comments

Comments
 (0)