Passed
Push — master ( a80ba9...b999d2 )
by Smoren
01:46
created

NestedStorage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 2
c 1
b 1
f 1
dl 0
loc 4
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Smoren\Schemator\Components;
4
5
use Smoren\Schemator\Exceptions\NestedAccessorException;
6
use Smoren\Schemator\Factories\NestedAccessorFactory;
7
use Smoren\Schemator\Interfaces\NestedAccessorInterface;
8
9
/**
10
 * Storage with nested accessor interface
11
 * @author Smoren <[email protected]>
12
 */
13
class NestedStorage 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
}