|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Smoren\NestedAccessor\Components; |
|
4
|
|
|
|
|
5
|
|
|
use Smoren\NestedAccessor\Exceptions\NestedAccessorException; |
|
6
|
|
|
use Smoren\NestedAccessor\Factories\NestedAccessorFactory; |
|
7
|
|
|
use Smoren\NestedAccessor\Interfaces\NestedAccessorInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Storage with nested accessor interface |
|
11
|
|
|
* @author Smoren <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class NestedArrayStorage implements NestedAccessorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array<scalar, mixed> storage |
|
17
|
|
|
*/ |
|
18
|
|
|
protected array $storage; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var NestedAccessorInterface nested accessor |
|
21
|
|
|
*/ |
|
22
|
|
|
protected NestedAccessorInterface $nestedAccessor; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array<scalar, mixed>|null $storage |
|
26
|
|
|
* @param NestedAccessorFactory|null $factory |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(?array $storage = null, ?NestedAccessorFactory $factory = null) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->storage = $storage ?? []; |
|
31
|
|
|
$this->nestedAccessor = ($factory ?? new NestedAccessorFactory())->create($this->storage); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @inheritDoc |
|
36
|
|
|
* @throws NestedAccessorException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function get($path, bool $strict = true) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->nestedAccessor->get($path, $strict); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @inheritDoc |
|
45
|
|
|
* @throws NestedAccessorException |
|
46
|
|
|
*/ |
|
47
|
|
|
public function set($path, $value, bool $strict = true): self |
|
48
|
|
|
{ |
|
49
|
|
|
$this->nestedAccessor->set($path, $value, $strict); |
|
50
|
|
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function append($path, $value, bool $strict = true): self |
|
57
|
|
|
{ |
|
58
|
|
|
$this->nestedAccessor->append($path, $value, $strict); |
|
59
|
|
|
return $this; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|