Skip to content

Commit 6b49c1a

Browse files
committed
- deconstructs the dot-key values into associative array
- optimization: skips the non-dotted keys from set() has() and delete() methods - added #[AllowDynamicProperties]
1 parent 0ecd1c1 commit 6b49c1a

1 file changed

Lines changed: 26 additions & 5 deletions

File tree

ExtendedArguments.php

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
use function array_slice;
1919
use function explode;
2020
use function is_array;
21+
use function is_string;
2122
use function join;
23+
use function str_contains;
2224

2325
/**
2426
* Class ExtendedArguments
@@ -30,19 +32,32 @@
3032
*
3133
* @property array $data
3234
*/
35+
36+
#[\AllowDynamicProperties]
3337
class ExtendedArguments extends Arguments
3438
{
39+
public function __construct(protected array $data = [])
40+
{
41+
$this->data = [];
42+
foreach ($data as $index => $value) {
43+
$this->set($index, $value);
44+
}
45+
}
46+
3547
public function get(string $index, mixed $default = null): mixed
3648
{
3749
return $this->find($index, $default);
3850
}
3951

4052
public function set(mixed $index, mixed $value): static
4153
{
54+
if (is_string($index) && false === str_contains($index, '.')) {
55+
return parent::set($index, $value);
56+
}
4257
$data =& $this->data;
43-
foreach (explode('.', $index) as $i) {
44-
if (false === is_array($data[$i]) ||
45-
false === array_key_exists($i, $data)
58+
foreach (explode('.', (string)$index) as $i) {
59+
if (false === array_key_exists($i, $data) ||
60+
false === is_array($data[$i])
4661
) {
4762
$data[$i] = [];
4863
}
@@ -62,6 +77,9 @@ public function append(string $index, mixed $value): static
6277

6378
public function has(string $index): bool
6479
{
80+
if (false === str_contains($index, '.')) {
81+
return array_key_exists($index, $this->data);
82+
}
6583
$data =& $this->data;
6684
foreach (explode('.', $index) as $i) {
6785
if (false === is_array($data) ||
@@ -76,10 +94,13 @@ public function has(string $index): bool
7694

7795
public function delete(string $index): static
7896
{
97+
if (false === str_contains($index, '.')) {
98+
return parent::delete($index);
99+
}
79100
$data =& $this->data;
80101
foreach (explode('.', $index) as $i) {
81-
if (false === is_array($data[$i]) ||
82-
false === array_key_exists($i, $data)
102+
if (false === array_key_exists($i, $data) ||
103+
false === is_array($data[$i])
83104
) {
84105
continue;
85106
}

0 commit comments

Comments
 (0)