1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Smoren\NestedAccessor\Helpers; |
4
|
|
|
|
5
|
|
|
use Smoren\NestedAccessor\Components\NestedAccessor; |
6
|
|
|
use Smoren\NestedAccessor\Exceptions\NestedAccessorException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Helper for getting and setting to source array or object with nested keys |
10
|
|
|
* @author Smoren <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class NestedHelper |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var NestedAccessor|null nested accessor instance |
16
|
|
|
*/ |
17
|
|
|
protected static ?NestedAccessor $accessor = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Gets value from nested source by given path |
21
|
|
|
* @param array<scalar, mixed>|object $source source |
22
|
|
|
* @param string|array<string> $path path e.g. 'path.to.value' or ['path', 'to', 'value'] |
23
|
|
|
* @param bool $strict if true: throw exception when path is not found in source |
24
|
|
|
* @return mixed value got by path |
25
|
|
|
* @throws NestedAccessorException |
26
|
|
|
*/ |
27
|
|
|
public static function get($source, $path, bool $strict = true) |
28
|
|
|
{ |
29
|
|
|
return static::prepareAccessor($source)->get($path, $strict); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sets value to nested source by given path |
34
|
|
|
* @param array<scalar, mixed>|object $source source |
35
|
|
|
* @param string|array<string> $path path e.g. 'path.to.value' or ['path', 'to', 'value'] |
36
|
|
|
* @param mixed $value value to set |
37
|
|
|
* @param bool $strict if true: throw exception when path is not found in source |
38
|
|
|
* @return void |
39
|
|
|
* @throws NestedAccessorException |
40
|
|
|
*/ |
41
|
|
|
public static function set(&$source, $path, $value, bool $strict = true): void |
42
|
|
|
{ |
43
|
|
|
static::prepareAccessor($source)->set($path, $value, $strict); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Appends value to nested source by given path |
49
|
|
|
* @param array<scalar, mixed>|object $source source |
50
|
|
|
* @param string|array<string> $path path e.g. 'path.to.value' or ['path', 'to', 'value'] |
51
|
|
|
* @param mixed $value value to set |
52
|
|
|
* @param bool $strict if true: throw exception when path is not found in source |
53
|
|
|
* @return void |
54
|
|
|
* @throws NestedAccessorException |
55
|
|
|
*/ |
56
|
|
|
public static function append(&$source, $path, $value, bool $strict = true): void |
57
|
|
|
{ |
58
|
|
|
static::prepareAccessor($source)->append($path, $value, $strict); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Method for preparing accessor to use with source |
63
|
|
|
* @param array<scalar, mixed>|object $source source data |
64
|
|
|
* @return NestedAccessor |
65
|
|
|
* @throws NestedAccessorException |
66
|
|
|
*/ |
67
|
|
|
protected static function prepareAccessor(&$source): NestedAccessor |
68
|
|
|
{ |
69
|
|
|
if(static::$accessor === null) { |
70
|
|
|
static::$accessor = new NestedAccessor($source); |
71
|
|
|
} else { |
72
|
|
|
static::$accessor->setSource($source); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return static::$accessor; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|