Skip to content

Commit ecfcf20

Browse files
committed
stan
1 parent 75d37f8 commit ecfcf20

File tree

10 files changed

+175
-44
lines changed

10 files changed

+175
-44
lines changed

src/Collections/JsonTranslations.php

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Collection;
1010

11-
/**
12-
* @extends Translations<null|scalar>
13-
*/
1411
class JsonTranslations extends Translations
1512
{
1613
public string $driver = JsonDriver::class;
@@ -20,17 +17,15 @@ public function has(string $key): bool
2017
return array_key_exists($key, $this->items);
2118
}
2219

23-
/**
24-
* @return null|scalar
25-
*/
2620
public function get(string $key): mixed
2721
{
2822
return $this->items[$key] ?? null;
2923
}
3024

3125
public function dot(): Collection
3226
{
33-
return $this->collect();
27+
// @phpstan-ignore-next-line
28+
return new Collection($this->items);
3429
}
3530

3631
public static function undot(Collection|array $items): static
@@ -76,11 +71,16 @@ public function diff(Translations $translations): static
7671

7772
public function filter(?callable $callback = null): static
7873
{
79-
return new static(array_filter(
80-
$this->items,
81-
$callback,
82-
ARRAY_FILTER_USE_BOTH
83-
));
74+
if ($callback) {
75+
return new static(array_filter(
76+
$this->items,
77+
$callback,
78+
ARRAY_FILTER_USE_BOTH
79+
));
80+
}
81+
82+
return new static(array_filter($this->items));
83+
8484
}
8585

8686
public function map(?callable $callback = null): static
@@ -92,4 +92,13 @@ public function map(?callable $callback = null): static
9292
)
9393
);
9494
}
95+
96+
public function sortKeys(int $options = SORT_REGULAR, bool $descending = false): static
97+
{
98+
$items = $this->items;
99+
100+
$descending ? krsort($items, $options) : ksort($items, $options);
101+
102+
return new static($items);
103+
}
95104
}

src/Collections/PhpTranslations.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use Illuminate\Support\Arr;
99
use Illuminate\Support\Collection;
1010

11-
/**
12-
* @extends Translations<null|scalar|array<array-key, mixed>>
13-
*/
1411
class PhpTranslations extends Translations
1512
{
1613
public string $driver = PhpDriver::class;
@@ -133,10 +130,18 @@ protected function recursiveFilter(array $items, callable $callback): array
133130

134131
public function filter(?callable $callback = null): static
135132
{
133+
if ($callback) {
134+
return new static($this->recursiveFilter(
135+
$this->items,
136+
$callback
137+
));
138+
}
139+
136140
return new static($this->recursiveFilter(
137141
$this->items,
138-
$callback
142+
fn ($value) => (bool) $value
139143
));
144+
140145
}
141146

142147
/**
@@ -188,6 +193,35 @@ public function diff(Translations $translations): static
188193
);
189194
}
190195

196+
/**
197+
* @param array<array-key, null|scalar|array<array-key, mixed>> $items
198+
* @return array<array-key, null|scalar|array<array-key, mixed>>
199+
*/
200+
protected function recursiveSortKeys(array $items, int $options = SORT_REGULAR, bool $descending = false): array
201+
{
202+
foreach ($items as $key => $value) {
203+
if (is_array($value)) {
204+
$items[$key] = $this->recursiveSortKeys($value, $options, $descending);
205+
}
206+
207+
if ($descending) {
208+
krsort($items, $options);
209+
} else {
210+
ksort($items, $options);
211+
}
212+
213+
}
214+
215+
return $items;
216+
}
217+
218+
public function sortKeys(int $options = SORT_REGULAR, bool $descending = false): static
219+
{
220+
return new static(
221+
$this->recursiveSortKeys($this->items, $options, $descending)
222+
);
223+
}
224+
191225
/**
192226
* Dot in translations keys might break the initial array structure
193227
* To prevent that, we encode the dots in unicode

src/Collections/Translations.php

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@
1111
use Illuminate\Support\Collection;
1212

1313
/**
14-
* @template TValue
15-
*
16-
* @implements Arrayable<string, TValue>
14+
* @implements Arrayable<string, null|scalar|array<array-key, mixed>>
1715
*/
1816
abstract class Translations implements Arrayable, Countable, Jsonable
1917
{
@@ -23,12 +21,12 @@ abstract class Translations implements Arrayable, Countable, Jsonable
2321
public string $driver;
2422

2523
/**
26-
* @var array<array-key, TValue>
24+
* @var array<array-key, null|scalar|array<array-key, mixed>>
2725
*/
2826
public array $items = [];
2927

3028
/**
31-
* @param array<array-key, TValue>|Collection<array-key,TValue> $items
29+
* @param array<array-key, null|scalar|array<array-key, mixed>>|Collection<array-key,null|scalar|array<array-key, mixed>> $items
3230
*/
3331
final public function __construct(array|Collection $items = [])
3432
{
@@ -38,17 +36,28 @@ final public function __construct(array|Collection $items = [])
3836
abstract public function has(string $key): bool;
3937

4038
/**
41-
* @return TValue
39+
* @return null|scalar|array<array-key, mixed>
4240
*/
4341
abstract public function get(string $key): mixed;
4442

43+
public function getString(string $key): string
44+
{
45+
$value = $this->get($key);
46+
47+
if (is_array($value)) {
48+
return '';
49+
}
50+
51+
return (string) $value;
52+
}
53+
4554
/**
4655
* @return Collection<array-key, null|scalar>
4756
*/
4857
abstract public function dot(): Collection;
4958

5059
/**
51-
* @param Collection<array-key, TValue>|array<array-key, TValue> $items
60+
* @param Collection<array-key, null|scalar|array<array-key, mixed>>|array<array-key, null|scalar|array<array-key, mixed>> $items
5261
*/
5362
abstract public static function undot(Collection|array $items): static;
5463

@@ -63,25 +72,24 @@ abstract public function only(array $keys): static;
6372
abstract public function except(array $keys): static;
6473

6574
/**
66-
* @param array<array-key, TValue> $values
75+
* @param array<array-key, null|scalar|array<array-key, mixed>> $values
6776
*/
6877
abstract public function merge(array $values): static;
6978

70-
/**
71-
* @param Translations<TValue> $translations
72-
*/
7379
abstract public function diff(Translations $translations): static;
7480

7581
/**
76-
* @param null|(callable(array-key, TValue):mixed) $callback
82+
* @param null|(callable(null|scalar|array<array-key, mixed>, array-key):mixed) $callback
7783
*/
7884
abstract public function filter(?callable $callback = null): static;
7985

8086
/**
81-
* @param null|(callable(array-key, TValue):mixed) $callback
87+
* @param null|(callable(null|scalar|array<array-key, mixed>, array-key):mixed) $callback
8288
*/
8389
abstract public function map(?callable $callback = null): static;
8490

91+
abstract public function sortKeys(int $options = SORT_REGULAR, bool $descending = false): static;
92+
8593
public function notBlank(): static
8694
{
8795
return $this->filter(
@@ -90,7 +98,7 @@ public function notBlank(): static
9098
}
9199

92100
/**
93-
* @return array<array-key, TValue>
101+
* @return array<array-key, null|scalar|array<array-key, mixed>>
94102
*/
95103
public function all(): array
96104
{
@@ -106,10 +114,11 @@ public function keys(): array
106114
}
107115

108116
/**
109-
* @return Collection<array-key, TValue>
117+
* @return Collection<array-key, null|scalar|array<array-key, mixed>>
110118
*/
111119
public function collect(): Collection
112120
{
121+
// @phpstan-ignore-next-line
113122
return new Collection($this->items);
114123
}
115124

@@ -120,6 +129,7 @@ public function collect(): Collection
120129
*/
121130
public function toBase(): Collection
122131
{
132+
// @phpstan-ignore-next-line
123133
return $this->dot();
124134
}
125135

@@ -129,7 +139,7 @@ public function count(): int
129139
}
130140

131141
/**
132-
* @return array<array-key, TValue>
142+
* @return array<array-key, null|scalar|array<array-key, mixed>>
133143
*/
134144
public function toArray(): array
135145
{

src/Commands/AddLocaleCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function handle(): int
6868
->map(function ($value, $key) use ($translations) {
6969
return [
7070
(string) $key,
71-
(string) str((string) $translations->get($key))->limit(25),
71+
(string) str($translations->getString($key))->limit(25),
7272
(string) str((string) $value)->limit(25),
7373
];
7474
})->toArray()

src/Commands/ProofreadCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public function handle(): int
4343
rows: $translations
4444
->dot()
4545
->map(fn ($value, $key) => [
46-
$key,
46+
(string) $key,
4747
(string) str((string) $value)->limit(25),
48-
(string) str((string) $proofread->get($key))->limit(25),
48+
(string) str($proofread->getString($key))->limit(25),
4949
])
5050
->all()
5151
);

src/Commands/UntranslatedCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function handle(): int
6262
->map(function ($value, $key) use ($missing) {
6363
return [
6464
(string) $key,
65-
str((string) $missing->get($key))->limit(25)->value(),
65+
str($missing->getString($key))->limit(25)->value(),
6666
str((string) $value)->limit(25)->value(),
6767
];
6868
})

src/Services/Proofread/ProofreadServiceInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
interface ProofreadServiceInterface
88
{
99
/**
10-
* @param array<string, string> $texts
11-
* @return array<string, string>
10+
* @param array<array-key, null|scalar> $texts
11+
* @return array<array-key, null|scalar>
1212
*/
1313
public function proofreadAll(array $texts): array;
1414

src/Services/Translate/TranslateServiceInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
interface TranslateServiceInterface
88
{
99
/**
10-
* @param array<string, string> $texts
11-
* @return array<string, string>
10+
* @param array<array-key, null|scalar> $texts
11+
* @return array<array-key, null|scalar>
1212
*/
1313
public function translateAll(array $texts, string $targetLocale): array;
1414

src/Translator.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,9 @@ public function translateTranslations(
212212

213213
$sourceTranslations = $this->getTranslations($source)
214214
->only($keys)
215-
->filter(fn ($value) => ! blank($value))
216-
->toArray();
215+
->notBlank()
216+
->dot()
217+
->all();
217218

218219
$translatedValues = $service->translateAll(
219220
$sourceTranslations,
@@ -265,8 +266,9 @@ public function proofreadTranslations(
265266
$proofreadValues = $service->proofreadAll(
266267
texts: $translations
267268
->only($keys)
268-
->filter(fn ($value) => ! blank($value))
269-
->toArray()
269+
->notBlank()
270+
->dot()
271+
->all()
270272
);
271273

272274
return $translations->merge($proofreadValues);

0 commit comments

Comments
 (0)