|
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 |
|
9 | 11 | class JsonTranslations extends Translations
|
10 | 12 | {
|
11 | 13 | public string $driver = JsonDriver::class;
|
12 | 14 |
|
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 | + } |
14 | 115 | }
|
0 commit comments