Skip to content

Commit 30c32ca

Browse files
committed
better set and merge support
1 parent ecfcf20 commit 30c32ca

File tree

5 files changed

+123
-11
lines changed

5 files changed

+123
-11
lines changed

src/Collections/JsonTranslations.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ public function get(string $key): mixed
2222
return $this->items[$key] ?? null;
2323
}
2424

25+
public function set(string $key, null|int|float|string|bool $value): static
26+
{
27+
$items = $this->items;
28+
29+
$items[$key] = $value;
30+
31+
return new static($items);
32+
}
33+
2534
public function dot(): Collection
2635
{
2736
// @phpstan-ignore-next-line
@@ -49,8 +58,10 @@ public function except(array $keys): static
4958
);
5059
}
5160

52-
public function merge(array $values): static
61+
public function merge(Translations|array $values): static
5362
{
63+
$values = $values instanceof Translations ? $values->dot()->all() : $values;
64+
5465
return new static(
5566
array_merge(
5667
$this->items,

src/Collections/PhpTranslations.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ public function has(string $key): bool
4242
return Arr::has($this->items, $key);
4343
}
4444

45+
public function set(string $key, null|int|float|string|bool $value): static
46+
{
47+
$items = $this->items;
48+
49+
Arr::set($items, $key, $value);
50+
51+
return new static($items);
52+
}
53+
4554
public function only(array $keys): static
4655
{
4756
$items = [];
@@ -176,14 +185,17 @@ public function map(?callable $callback = null): static
176185
);
177186
}
178187

179-
public function merge(array $values): static
188+
public function merge(Translations|array $values): static
180189
{
181-
return new static(
182-
array_merge_recursive(
183-
$this->items,
184-
$values
185-
)
186-
);
190+
$values = $values instanceof Translations ? $values->dot()->all() : $values;
191+
192+
$items = new static($this->items);
193+
194+
foreach ($values as $key => $value) {
195+
$items = $items->set($key, $value);
196+
}
197+
198+
return $items;
187199
}
188200

189201
public function diff(Translations $translations): static

src/Collections/Translations.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public function getString(string $key): string
5151
return (string) $value;
5252
}
5353

54+
abstract public function set(string $key, null|int|float|string|bool $value): static;
55+
5456
/**
5557
* @return Collection<array-key, null|scalar>
5658
*/
@@ -72,9 +74,9 @@ abstract public function only(array $keys): static;
7274
abstract public function except(array $keys): static;
7375

7476
/**
75-
* @param array<array-key, null|scalar|array<array-key, mixed>> $values
77+
* @param Translations|array<array-key, null|scalar> $values
7678
*/
77-
abstract public function merge(array $values): static;
79+
abstract public function merge(Translations|array $values): static;
7880

7981
abstract public function diff(Translations $translations): static;
8082

src/Facades/Translator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @method static array<int, string> getLocales()
2121
* @method static Translations getTranslations(string $locale)
2222
* @method static array<string, array{ count: int, files: string[] }> getMissingTranslations(string $locale)
23-
* @method static array<int, scalar|null> getDeadTranslations(string $locale)
23+
* @method static Translations getDeadTranslations(string $locale)
2424
* @method static Translations getUntranslatedTranslations(string $source, string $target)
2525
* @method static Translations setTranslations(string $locale, array<string, scalar|null> $values)
2626
* @method static Translations translateTranslations(string $source, string $target, array<int, string> $keys)

tests/Unit/PhpTranslationsTest.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,93 @@
6969
['h.i.j.k', true],
7070
]);
7171

72+
it('sets a translation value using dot notation', function ($key, $value, $expected) use ($translations) {
73+
74+
expect($translations->set($key, $value)->toArray())->toBe($expected);
75+
76+
})->with([
77+
[
78+
'_a',
79+
'_a_value',
80+
[
81+
'a' => [
82+
'b' => 'b_value',
83+
],
84+
'c' => [
85+
'd' => ['0_value', '1_value'],
86+
],
87+
'e' => '',
88+
'f' => [
89+
'g' => '',
90+
],
91+
'h' => [
92+
'i' => [
93+
'j' => [
94+
'k' => 'k_value',
95+
'l' => 'l_value',
96+
],
97+
],
98+
],
99+
'_a' => '_a_value',
100+
],
101+
],
102+
[
103+
'_a._b._c',
104+
'_c_value',
105+
[
106+
'a' => [
107+
'b' => 'b_value',
108+
],
109+
'c' => [
110+
'd' => ['0_value', '1_value'],
111+
],
112+
'e' => '',
113+
'f' => [
114+
'g' => '',
115+
],
116+
'h' => [
117+
'i' => [
118+
'j' => [
119+
'k' => 'k_value',
120+
'l' => 'l_value',
121+
],
122+
],
123+
],
124+
'_a' => [
125+
'_b' => [
126+
'_c' => '_c_value',
127+
],
128+
],
129+
],
130+
],
131+
[
132+
'a._b',
133+
'_b_value',
134+
[
135+
'a' => [
136+
'b' => 'b_value',
137+
'_b' => '_b_value',
138+
139+
],
140+
'c' => [
141+
'd' => ['0_value', '1_value'],
142+
],
143+
'e' => '',
144+
'f' => [
145+
'g' => '',
146+
],
147+
'h' => [
148+
'i' => [
149+
'j' => [
150+
'k' => 'k_value',
151+
'l' => 'l_value',
152+
],
153+
],
154+
],
155+
],
156+
],
157+
]);
158+
72159
it('filters values', function ($callback, $expected) use ($translations) {
73160

74161
$filtered = $translations->filter($callback);

0 commit comments

Comments
 (0)