Skip to content

Commit 30bd855

Browse files
committed
- renamed internal storage array (storage -> data)
- using constructor properties promotion
1 parent 5a4cce3 commit 30bd855

File tree

6 files changed

+53
-61
lines changed

6 files changed

+53
-61
lines changed

AccessorTrait.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
use function get_class;
1919

2020
/**
21-
* @property array $storage
21+
* @property array $data
2222
*/
2323
trait AccessorTrait
2424
{
2525
public function &__get($index)
2626
{
27-
if (false === array_key_exists($index, $this->storage)) {
28-
$this->storage[$index] = null;
27+
if (false === array_key_exists($index, $this->data)) {
28+
$this->data[$index] = null;
2929
}
30-
return $this->storage[$index];
30+
return $this->data[$index];
3131
}
3232

3333
public function __set($index, $value)
@@ -47,12 +47,12 @@ public function __isset($index)
4747

4848
public function get(string $index, mixed $default = null): mixed
4949
{
50-
return $this->storage[$index] ?? $default;
50+
return $this->data[$index] ?? $default;
5151
}
5252

5353
public function has(string $index): bool
5454
{
55-
return array_key_exists($index, $this->storage);
55+
return array_key_exists($index, $this->data);
5656
}
5757

5858
public function equals(string $propertyA, string $propertyB): bool
@@ -62,33 +62,33 @@ public function equals(string $propertyA, string $propertyB): bool
6262

6363
public function count(): int
6464
{
65-
return count($this->storage);
65+
return count($this->data);
6666
}
6767

6868
public function toArray(): array
6969
{
70-
return $this->storage;
70+
return $this->data;
7171
}
7272

7373
public function toJSON(int $options = 0): string
7474
{
75-
return json_serialize($this->storage, $options);
75+
return json_serialize($this->data, $options);
7676
}
7777

7878
public function toXML(string $root): string
7979
{
80-
return xml_serialize($root, $this->storage);
80+
return xml_serialize($root, $this->data);
8181
}
8282

8383
public function getIterator(): Traversable
8484
{
85-
foreach ($this->storage as $k => $v) {
85+
foreach ($this->data as $k => $v) {
8686
yield $k => $v;
8787
}
8888
}
8989

9090
public function jsonSerialize(): mixed
9191
{
92-
return $this->storage;
92+
return $this->data;
9393
}
9494
}

Arguments.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,22 @@ class Arguments implements Argument,
3333
MutatorTrait::__set insteadof AccessorTrait;
3434
}
3535

36-
protected array $storage = [];
36+
public function __construct(protected array $data = []) {}
3737

38-
public function __construct(array $values = [])
39-
{
40-
$this->storage = $values;
41-
}
42-
43-
public function __clone()
44-
{
45-
}
38+
public function __clone() {}
4639

4740
public function namespace(
4841
string $prefix,
4942
bool $lowercase = true,
5043
bool $trim = true): static
5144
{
52-
return new static($this->filter($this->toArray(), $prefix, $lowercase, $trim));
45+
return new static(
46+
$this->filter($this->toArray(), $prefix, $lowercase, $trim)
47+
);
5348
}
5449

5550
public function toImmutable(): Immutable
5651
{
57-
return new Immutable($this->storage);
52+
return new Immutable($this->data);
5853
}
5954
}

ArrayDataFilterTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,26 @@ public function filter(
3838

3939
public function find(string $index, mixed $default = null): mixed
4040
{
41-
if (isset($this->storage[$index])) {
42-
return $this->storage[$index];
41+
if (isset($this->data[$index])) {
42+
return $this->data[$index];
4343
}
44-
$storage = $this->storage;
44+
$data = $this->data;
4545
foreach (explode('.', $index) as $token) {
46-
if (false === is_array($storage) ||
47-
false === array_key_exists($token, $storage)
46+
if (false === is_array($data) ||
47+
false === array_key_exists($token, $data)
4848
) {
4949
return $default;
5050
}
51-
$storage = &$storage[$token];
51+
$data =& $data[$token];
5252
}
53-
return $storage;
53+
return $data;
5454
}
5555

5656
public function extract(array $indexes): array
5757
{
5858
$found = [];
5959
foreach ($indexes as $index) {
60-
$found[$index] = $this->storage[$index] ?? null;
60+
$found[$index] = $this->data[$index] ?? null;
6161
}
6262
return $found;
6363
}

ExtendedArguments.php

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
* - boolean is a wrong type for the key; do not use a boolean key (it's juggled into a string)
2828
*
2929
* Use string values for the keys and you'll be golden.
30+
*
31+
* @property array $data
3032
*/
3133
class ExtendedArguments extends Arguments
3234
{
@@ -37,16 +39,16 @@ public function get(string $index, mixed $default = null): mixed
3739

3840
public function set(mixed $index, mixed $value): static
3941
{
40-
$storage = &$this->storage;
42+
$data =& $this->data;
4143
foreach (explode('.', $index) as $i) {
42-
if (false === is_array($storage[$i]) ||
43-
false === array_key_exists($i, $storage)
44+
if (false === is_array($data[$i]) ||
45+
false === array_key_exists($i, $data)
4446
) {
45-
$storage[$i] = [];
47+
$data[$i] = [];
4648
}
47-
$storage = &$storage[$i];
49+
$data =& $data[$i];
4850
}
49-
$storage = $value;
51+
$data = $value;
5052
return $this;
5153
}
5254

@@ -60,31 +62,31 @@ public function append(string $index, mixed $value): static
6062

6163
public function has(string $index): bool
6264
{
63-
$storage = & $this->storage;
65+
$data =& $this->data;
6466
foreach (explode('.', $index) as $i) {
65-
if (false === is_array($storage) ||
66-
false === array_key_exists($i, $storage)
67+
if (false === is_array($data) ||
68+
false === array_key_exists($i, $data)
6769
) {
6870
return false;
6971
}
70-
$storage = &$storage[$i];
72+
$data =& $data[$i];
7173
}
7274
return true;
7375
}
7476

7577
public function delete(string $index): static
7678
{
77-
$storage = &$this->storage;
79+
$data =& $this->data;
7880
foreach (explode('.', $index) as $i) {
79-
if (false === is_array($storage[$i]) ||
80-
false === array_key_exists($i, $storage)
81+
if (false === is_array($data[$i]) ||
82+
false === array_key_exists($i, $data)
8183
) {
8284
continue;
8385
}
84-
$storage = &$storage[$i];
86+
$data =& $data[$i];
8587
}
8688
if (isset($i)) {
87-
unset($storage[$i]);
89+
unset($data[$i]);
8890
}
8991
return $this;
9092
}
@@ -103,7 +105,7 @@ public function flatten(): static
103105
$indexes = [];
104106
$flatten = [];
105107
$iterator = new RecursiveIteratorIterator(
106-
new RecursiveArrayIterator($this->storage),
108+
new RecursiveArrayIterator($this->data),
107109
RecursiveIteratorIterator::SELF_FIRST
108110
);
109111
foreach ($iterator as $index => $value) {

Immutable.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,10 @@ class Immutable implements Data,
2929

3030
use AccessorTrait, ArrayDataFilterTrait;
3131

32-
protected array $storage = [];
33-
34-
public function __construct(array $values)
35-
{
36-
$this->storage = $values;
37-
}
32+
public function __construct(protected array $data) {}
3833

3934
public function toArguments(): Arguments
4035
{
41-
return new Arguments($this->storage);
36+
return new Arguments($this->data);
4237
}
4338
}

MutatorTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Koded\Stdlib;
1313

1414
/**
15-
* @property array $storage
15+
* @property array $data
1616
*/
1717
trait MutatorTrait
1818
{
@@ -23,13 +23,13 @@ public function __set($index, $value)
2323

2424
public function set(string $index, mixed $value): static
2525
{
26-
$this->storage[$index] = $value;
26+
$this->data[$index] = $value;
2727
return $this;
2828
}
2929

3030
public function bind(string $index, mixed &$variable): static
3131
{
32-
$this->storage[$index] = &$variable;
32+
$this->data[$index] =& $variable;
3333
return $this;
3434
}
3535

@@ -41,27 +41,27 @@ public function upsert(string $index, mixed $value): static
4141
public function pull(string $index, mixed $default = null): mixed
4242
{
4343
$value = $this->get($index, $default);
44-
unset($this->storage[$index]);
44+
unset($this->data[$index]);
4545
return $value;
4646
}
4747

4848
public function import(array $array): static
4949
{
5050
foreach ($array as $index => $value) {
51-
$this->storage[$index] = $value;
51+
$this->data[$index] = $value;
5252
}
5353
return $this;
5454
}
5555

5656
public function delete(string $index): static
5757
{
58-
unset($this->storage[$index]);
58+
unset($this->data[$index]);
5959
return $this;
6060
}
6161

6262
public function clear(): static
6363
{
64-
$this->storage = [];
64+
$this->data = [];
6565
return $this;
6666
}
6767
}

0 commit comments

Comments
 (0)