Skip to content

Commit 6b45358

Browse files
committed
introduce sanitize
1 parent 5ff3ed0 commit 6b45358

File tree

4 files changed

+91
-24
lines changed

4 files changed

+91
-24
lines changed

src/Collections/JsonTranslations.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ public function set(string|int|null $key, mixed $value): static
1414
return $this->put($key, $value);
1515
}
1616

17+
public function sanitize(): static
18+
{
19+
return $this;
20+
}
21+
1722
public function sortNatural(): static
1823
{
1924
return $this->sortKeys(SORT_NATURAL);

src/Collections/PhpTranslations.php

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -45,48 +45,64 @@ public function except($keys): static
4545
);
4646
}
4747

48+
/**
49+
* Replace empty (nested) array with null
50+
*/
51+
public function sanitize(): static
52+
{
53+
return $this->map(function (mixed $value) {
54+
return $this->sanitizeRecursive($value);
55+
});
56+
}
57+
58+
public function sanitizeRecursive(array|string|int|float|null $value)
59+
{
60+
if (is_array($value)) {
61+
if (empty($value)) {
62+
return null;
63+
}
64+
65+
return array_map(fn ($item) => $this->sanitizeRecursive($item), $value);
66+
}
67+
68+
return $value;
69+
}
70+
4871
public function sortNatural(): static
4972
{
50-
$this->items = $this->recursiveSortNatural($this->items);
73+
$items = $this->items;
5174

52-
return $this;
75+
return new static(
76+
$this->recursiveSortNatural($items)
77+
);
5378
}
5479

5580
protected function recursiveSortNatural(array $items): array
5681
{
5782
ksort($items, SORT_NATURAL);
5883

59-
foreach ($items as $key => $item) {
84+
return array_map(function ($item) {
6085
if (is_array($item)) {
61-
$items[$key] = $this->recursiveSortNatural($item);
86+
return $this->recursiveSortNatural($item);
6287
}
63-
}
6488

65-
return $items;
89+
return $item;
90+
}, $items);
6691
}
6792

68-
public function toDotTranslations(bool $filter = false): Collection
93+
public function toDotTranslations(): Collection
6994
{
70-
/**
71-
* Filtering the array prevent incoherent values such as such as 'key' => []
72-
*/
73-
return $this
74-
->dot()
75-
->toBase()
76-
->when(
77-
$filter,
78-
fn ($c) => $c->filter(fn ($value) => ! blank($value))
79-
);
95+
return $this->dot()->toBase();
8096
}
8197

82-
public function toTranslationsKeys(bool $filter = false): Collection
98+
public function toTranslationsKeys(): Collection
8399
{
84-
return $this->toDotTranslations($filter)->keys();
100+
return $this->toDotTranslations()->keys();
85101
}
86102

87-
public function toTranslationsValues(bool $filter = false): Collection
103+
public function toTranslationsValues(): Collection
88104
{
89-
return $this->toDotTranslations($filter)->values();
105+
return $this->toDotTranslations()->values();
90106
}
91107

92108
public function diffTranslationsKeys(Collection $translations): Collection

src/Collections/TranslationsInterface.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ public function only($keys);
1616

1717
public function except($keys);
1818

19+
public function sanitize(): static;
20+
1921
public function sortNatural(): static;
2022

21-
public function toDotTranslations(bool $filter = false): Collection;
23+
public function toDotTranslations(): Collection;
2224

23-
public function toTranslationsKeys(bool $filter = false): Collection;
25+
public function toTranslationsKeys(): Collection;
2426

25-
public function toTranslationsValues(bool $filter = false): Collection;
27+
public function toTranslationsValues(): Collection;
2628

2729
public function diffTranslationsKeys(Collection $translations): Collection;
2830

tests/Unit/PhpTranslationsTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,47 @@
9797
'd' => 'text',
9898
]);
9999
});
100+
101+
it('sanitize (nested) translations', function () {
102+
$translations = new PhpTranslations([
103+
'a' => 'text',
104+
'b' => [
105+
'a' => [],
106+
'b' => 'text',
107+
],
108+
'c' => [],
109+
'd' => 0,
110+
'e' => null,
111+
'f' => [
112+
'a' => [
113+
'a' => [],
114+
],
115+
],
116+
'g' => [
117+
[],
118+
[],
119+
],
120+
]);
121+
122+
expect(
123+
$translations->sanitize()->toArray()
124+
)->toBe([
125+
'a' => 'text',
126+
'b' => [
127+
'a' => null,
128+
'b' => 'text',
129+
],
130+
'c' => null,
131+
'd' => 0,
132+
'e' => null,
133+
'f' => [
134+
'a' => [
135+
'a' => null,
136+
],
137+
],
138+
'g' => [
139+
null,
140+
null,
141+
],
142+
]);
143+
});

0 commit comments

Comments
 (0)