Skip to content

Commit 75d37f8

Browse files
committed
init array php architecture
1 parent 81edbc9 commit 75d37f8

15 files changed

+612
-279
lines changed

src/Collections/JsonTranslations.php

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

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

11+
/**
12+
* @extends Translations<null|scalar>
13+
*/
914
class JsonTranslations extends Translations
1015
{
1116
public string $driver = JsonDriver::class;
1217

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+
}
1495
}

0 commit comments

Comments
 (0)