|
5 | 5 | namespace Elegantly\Translator\Collections; |
6 | 6 |
|
7 | 7 | use Elegantly\Translator\Drivers\JsonDriver; |
| 8 | +use Illuminate\Support\Arr; |
| 9 | +use Illuminate\Support\Collection; |
8 | 10 |
|
| 11 | +/** |
| 12 | + * @extends Translations<null|scalar> |
| 13 | + */ |
9 | 14 | class JsonTranslations extends Translations |
10 | 15 | { |
11 | 16 | public string $driver = JsonDriver::class; |
12 | 17 |
|
13 | | - // |
| 18 | + public function has(string $key): bool |
| 19 | + { |
| 20 | + return array_key_exists($key, $this->items); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * @return null|scalar |
| 25 | + */ |
| 26 | + public function get(string $key): mixed |
| 27 | + { |
| 28 | + return $this->items[$key] ?? null; |
| 29 | + } |
| 30 | + |
| 31 | + public function dot(): Collection |
| 32 | + { |
| 33 | + return $this->collect(); |
| 34 | + } |
| 35 | + |
| 36 | + public static function undot(Collection|array $items): static |
| 37 | + { |
| 38 | + $items = $items instanceof Collection ? $items->all() : $items; |
| 39 | + |
| 40 | + return new static($items); |
| 41 | + } |
| 42 | + |
| 43 | + public function only(array $keys): static |
| 44 | + { |
| 45 | + return new static( |
| 46 | + array_intersect_key($this->items, array_flip((array) $keys)) |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + public function except(array $keys): static |
| 51 | + { |
| 52 | + return new static( |
| 53 | + array_diff_key($this->items, array_flip((array) $keys)) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + public function merge(array $values): static |
| 58 | + { |
| 59 | + return new static( |
| 60 | + array_merge( |
| 61 | + $this->items, |
| 62 | + $values |
| 63 | + ) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + public function diff(Translations $translations): static |
| 68 | + { |
| 69 | + return new static( |
| 70 | + array_diff_key( |
| 71 | + $this->items, |
| 72 | + $translations->all() |
| 73 | + ) |
| 74 | + ); |
| 75 | + } |
| 76 | + |
| 77 | + public function filter(?callable $callback = null): static |
| 78 | + { |
| 79 | + return new static(array_filter( |
| 80 | + $this->items, |
| 81 | + $callback, |
| 82 | + ARRAY_FILTER_USE_BOTH |
| 83 | + )); |
| 84 | + } |
| 85 | + |
| 86 | + public function map(?callable $callback = null): static |
| 87 | + { |
| 88 | + return new static( |
| 89 | + Arr::map( |
| 90 | + $this->items, |
| 91 | + $callback |
| 92 | + ) |
| 93 | + ); |
| 94 | + } |
14 | 95 | } |
0 commit comments