Skip to content

Commit b58630f

Browse files
committed
Switch to brick/varexporter, since it has done the heavy lifting for us :)
1 parent 285c581 commit b58630f

File tree

7 files changed

+130
-29
lines changed

7 files changed

+130
-29
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
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

Lines changed: 7 additions & 9 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 1 addition & 3 deletions
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

Lines changed: 0 additions & 2 deletions
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;
@@ -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

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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 testItOmitsIndexKeys(): 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 testItHandlesMixedArrays(): 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 testItHandlesNonSequentialNumericKeys(): 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/TranslationDumperTest.php

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,9 @@ 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 */
@@ -32,9 +29,6 @@ public function testItDumpsDottedKeysAsExpected(array $given, array $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)