Skip to content

Commit af47a08

Browse files
Merge pull request #366 from ChristofVerhoeven/prevent_loss_of_attribute_translations_with_subkeys
Prevent loss of attribute translations with subkeys
2 parents 1e9d4c6 + 8fac918 commit af47a08

File tree

5 files changed

+56
-13
lines changed

5 files changed

+56
-13
lines changed

src/Services/Filesystem/Php.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
use DragonCode\PrettyArray\Services\File;
2121
use DragonCode\Support\Facades\Helpers\Arr;
22+
use DragonCode\Support\Facades\Helpers\Str;
2223
use DragonCode\Support\Helpers\Ables\Arrayable;
2324
use Illuminate\Support\Arr as IlluminateArr;
2425

@@ -39,11 +40,9 @@ public function store(string $path, $content): string
3940
{
4041
$content = $this->sort($content);
4142

42-
$content = $this->expand($content);
43-
44-
if ($this->hasValidation($path)) {
45-
$content = $this->validationSort($content);
46-
}
43+
$content = $this->hasValidation($path)
44+
? $this->expandValidation($content)
45+
: $this->expand($content);
4746

4847
$content = $this->format($content);
4948

@@ -68,20 +67,28 @@ protected function expand(array $values): array
6867
return $result;
6968
}
7069

71-
protected function validationSort(array $items): array
70+
protected function expandValidation(array $values): array
7271
{
73-
$attributes = Arr::get($items, 'attributes');
72+
$attributes = [];
73+
$items = [];
74+
75+
foreach ($values as $key => $value) {
76+
if (Str::startsWith($key, "attributes.")) {
77+
$attributeKey = explode('.', $key, 2)[1];
78+
$attributes[$attributeKey] = $value;
79+
80+
continue;
81+
}
82+
83+
IlluminateArr::set($items, $key, $value);
84+
}
85+
7486
$custom = Arr::get($items, 'custom');
7587

7688
return Arr::of($items)
7789
->except(['attributes', 'custom'])
78-
->when(! empty($attributes), fn (Arrayable $array) => $array->set('attributes', $this->correctNestedAttributes($attributes)))
90+
->when(! empty($attributes), fn (Arrayable $array) => $array->set('attributes', $attributes))
7991
->when(! empty($custom), static fn (Arrayable $array) => $array->set('custom', $custom))
8092
->toArray();
8193
}
82-
83-
protected function correctNestedAttributes(array $attributes): array
84-
{
85-
return Arr::flattenKeys($attributes);
86-
}
8794
}

tests/Fixtures/lang/en/validation.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@
3030
'last_name' => 'last name',
3131
'age' => 'age',
3232

33+
'timeplan' => 'Timeplan',
34+
'timeplan.monday' => 'Monday',
3335
'timeplan.monday.0.0' => 'Monday (from)',
3436
'timeplan.monday.0.1' => 'Monday (to)',
3537
'timeplan.monday.1.0' => 'Monday (from)',
3638
'timeplan.monday.1.1' => 'Monday (to)',
39+
40+
// manually adding arrays works, but will be flattened in update process
41+
'list' => [
42+
'jobs' => 'List of jobs',
43+
'vehicles' => 'List of vehicles',
44+
]
3745
],
3846

3947
'custom' => [

tests/Fixtures/lang/fr/validation.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,18 @@
3030
'last_name' => 'nom',
3131
'age' => 'âge',
3232

33+
'timeplan' => 'Horaire',
34+
'timeplan.monday' => 'Lundi',
3335
'timeplan.monday.0.0' => 'Lundi (de)',
3436
'timeplan.monday.0.1' => 'Lundi (à)',
3537
'timeplan.monday.1.0' => 'Lundi (de)',
3638
'timeplan.monday.1.1' => 'Lundi (à)',
39+
40+
// manually adding arrays works, but will be flattened in update process
41+
'list' => [
42+
'jobs' => 'Liste des dossiers',
43+
'vehicles' => 'Liste des véhicules',
44+
]
3745
],
3846

3947
'custom' => [

tests/Unit/Console/InlineOff/Update/Success/NestedAttributesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public function testNested(): void
5151
'first_name' => 'first name',
5252
'last_name' => 'last name',
5353

54+
'list.jobs' => 'List of jobs',
55+
'list.vehicles' => 'List of vehicles',
56+
57+
'timeplan' => 'Timeplan',
58+
'timeplan.monday' => 'Monday',
5459
'timeplan.monday.0.0' => 'Monday (from)',
5560
'timeplan.monday.0.1' => 'Monday (to)',
5661
'timeplan.monday.1.0' => 'Monday (from)',
@@ -80,6 +85,11 @@ public function testNested(): void
8085
'first_name' => 'prénom',
8186
'last_name' => 'nom',
8287

88+
'list.jobs' => 'Liste des dossiers',
89+
'list.vehicles' => 'Liste des véhicules',
90+
91+
'timeplan' => 'Horaire',
92+
'timeplan.monday' => 'Lundi',
8393
'timeplan.monday.0.0' => 'Lundi (de)',
8494
'timeplan.monday.0.1' => 'Lundi (à)',
8595
'timeplan.monday.1.0' => 'Lundi (de)',

tests/Unit/Console/InlineOn/Update/Success/NestedAttributesTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public function testNested(): void
5151
'first_name' => 'first name',
5252
'last_name' => 'last name',
5353

54+
'list.jobs' => 'List of jobs',
55+
'list.vehicles' => 'List of vehicles',
56+
57+
'timeplan' => 'Timeplan',
58+
'timeplan.monday' => 'Monday',
5459
'timeplan.monday.0.0' => 'Monday (from)',
5560
'timeplan.monday.0.1' => 'Monday (to)',
5661
'timeplan.monday.1.0' => 'Monday (from)',
@@ -80,6 +85,11 @@ public function testNested(): void
8085
'first_name' => 'prénom',
8186
'last_name' => 'nom',
8287

88+
'list.jobs' => 'Liste des dossiers',
89+
'list.vehicles' => 'Liste des véhicules',
90+
91+
'timeplan' => 'Horaire',
92+
'timeplan.monday' => 'Lundi',
8393
'timeplan.monday.0.0' => 'Lundi (de)',
8494
'timeplan.monday.0.1' => 'Lundi (à)',
8595
'timeplan.monday.1.0' => 'Lundi (de)',

0 commit comments

Comments
 (0)