Skip to content

Commit 4821cc1

Browse files
authored
Merge pull request #22 from ElegantEngineeringTech/php-array
Refactoring PhpTranslations for performance
2 parents 8d399c9 + 5c8d907 commit 4821cc1

19 files changed

+861
-285
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"php": "^8.2",
2323
"illuminate/contracts": "^10.0||^11.0||^12.0",
2424
"nikic/php-parser": "^5.1",
25-
"openai-php/laravel": "^11.0||^0.12.0||^0.13.0",
25+
"openai-php/laravel": "^0.11.0||^0.12.0||^0.13.0",
2626
"spatie/laravel-package-tools": "^1.16",
2727
"spatie/simple-excel": "^3.7",
2828
"symfony/finder": "^6.0||^7.0",

src/Collections/JsonTranslations.php

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,111 @@
55
namespace Elegantly\Translator\Collections;
66

77
use Elegantly\Translator\Drivers\JsonDriver;
8+
use Illuminate\Support\Arr;
9+
use Illuminate\Support\Collection;
810

911
class JsonTranslations extends Translations
1012
{
1113
public string $driver = JsonDriver::class;
1214

13-
//
15+
public function has(string $key): bool
16+
{
17+
return array_key_exists($key, $this->items);
18+
}
19+
20+
public function get(string $key): mixed
21+
{
22+
return $this->items[$key] ?? null;
23+
}
24+
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+
34+
public function dot(): Collection
35+
{
36+
// @phpstan-ignore-next-line
37+
return new Collection($this->items);
38+
}
39+
40+
public static function undot(Collection|array $items): static
41+
{
42+
$items = $items instanceof Collection ? $items->all() : $items;
43+
44+
return new static($items);
45+
}
46+
47+
public function only(array $keys): static
48+
{
49+
return new static(
50+
array_intersect_key($this->items, array_flip((array) $keys))
51+
);
52+
}
53+
54+
public function except(array $keys): static
55+
{
56+
return new static(
57+
array_diff_key($this->items, array_flip((array) $keys))
58+
);
59+
}
60+
61+
public function merge(Translations|array $values): static
62+
{
63+
$values = $values instanceof Translations ? $values->dot()->all() : $values;
64+
65+
return new static(
66+
array_merge(
67+
$this->items,
68+
$values
69+
)
70+
);
71+
}
72+
73+
public function diff(Translations $translations): static
74+
{
75+
return new static(
76+
array_diff_key(
77+
$this->items,
78+
$translations->all()
79+
)
80+
);
81+
}
82+
83+
public function filter(?callable $callback = null): static
84+
{
85+
if ($callback) {
86+
return new static(array_filter(
87+
$this->items,
88+
$callback,
89+
ARRAY_FILTER_USE_BOTH
90+
));
91+
}
92+
93+
return new static(array_filter($this->items));
94+
95+
}
96+
97+
public function map(?callable $callback = null): static
98+
{
99+
return new static(
100+
Arr::map(
101+
$this->items,
102+
$callback
103+
)
104+
);
105+
}
106+
107+
public function sortKeys(int $options = SORT_REGULAR, bool $descending = false): static
108+
{
109+
$items = $this->items;
110+
111+
$descending ? krsort($items, $options) : ksort($items, $options);
112+
113+
return new static($items);
114+
}
14115
}

0 commit comments

Comments
 (0)