1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Jasny\DotKey; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Access objects and arrays through dot notation. |
9
|
|
|
* |
10
|
|
|
* @template TSubject |
11
|
|
|
*/ |
12
|
|
|
class DotKey |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var object|array<string,mixed> |
16
|
|
|
* @phpstan-var TSubject&(object|array<string,mixed>) |
17
|
|
|
*/ |
18
|
|
|
protected $subject; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class constructor. |
22
|
|
|
* |
23
|
|
|
* @param object|array $subject |
24
|
|
|
* |
25
|
|
|
* @phpstan-param TSubject&(object|array<string,mixed>) $subject |
26
|
|
|
*/ |
27
|
109 |
|
public function __construct($subject) |
28
|
|
|
{ |
29
|
109 |
|
if (!\is_object($subject) && !\is_array($subject)) { |
30
|
1 |
|
$type = \gettype($subject); |
31
|
1 |
|
throw new \InvalidArgumentException("Subject should be an array or object; $type given"); |
32
|
|
|
} |
33
|
|
|
|
34
|
108 |
|
$this->subject = $subject; |
35
|
108 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Check if path exists in subject. |
39
|
|
|
*/ |
40
|
10 |
|
public function exists(string $path, string $delimiter = '.'): bool |
41
|
|
|
{ |
42
|
10 |
|
return Internal\Read::exists($this->subject, $path, $delimiter); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get a value from subject by path. |
47
|
|
|
* |
48
|
|
|
* @param string $path |
49
|
|
|
* @param string $delimiter |
50
|
|
|
* @return mixed |
51
|
|
|
* @throws ResolveException |
52
|
|
|
*/ |
53
|
19 |
|
public function get(string $path, string $delimiter = '.') |
54
|
|
|
{ |
55
|
19 |
|
return Internal\Read::get($this->subject, $path, $delimiter); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set a value within the subject by path. |
61
|
|
|
* |
62
|
|
|
* @param string $path |
63
|
|
|
* @param mixed $value |
64
|
|
|
* @param string $delimiter |
65
|
|
|
* @return array|object |
66
|
|
|
* @throws ResolveException |
67
|
|
|
* |
68
|
|
|
* @phpstan-return TSubject&(object|array<string,mixed>) |
69
|
|
|
*/ |
70
|
23 |
|
public function set(string $path, $value, string $delimiter = '.') |
71
|
|
|
{ |
72
|
23 |
|
return Internal\Write::set($this->subject, $path, $value, $delimiter); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Set a value, creating a structure if needed. |
77
|
|
|
* |
78
|
|
|
* @param string $path |
79
|
|
|
* @param mixed $value |
80
|
|
|
* @param string $delimiter |
81
|
|
|
* @param bool|null $assoc Create new structure as array. Omit to base upon subject type. |
82
|
|
|
* @return array|object |
83
|
|
|
* @throws ResolveException |
84
|
|
|
* |
85
|
|
|
* @phpstan-return TSubject&(object|array<string,mixed>) |
86
|
|
|
*/ |
87
|
30 |
|
public function put(string $path, $value, string $delimiter = '.', ?bool $assoc = null) |
88
|
|
|
{ |
89
|
30 |
|
$assoc ??= is_array($this->subject) || $this->subject instanceof \ArrayAccess; |
90
|
|
|
|
91
|
30 |
|
return Internal\Write::put($this->subject, $path, $value, $delimiter, $assoc); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get a particular value back from the config array |
96
|
|
|
* |
97
|
|
|
* @param string $path |
98
|
|
|
* @param string $delimiter |
99
|
|
|
* @return object|array<string,mixed> |
100
|
|
|
* |
101
|
|
|
* @phpstan-return TSubject&(object|array<string,mixed>) |
102
|
|
|
*/ |
103
|
26 |
|
public function remove(string $path, string $delimiter = '.') |
104
|
|
|
{ |
105
|
26 |
|
return Internal\Remove::remove($this->subject, $path, $delimiter); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Factory method. |
112
|
|
|
* |
113
|
|
|
* @param object|array<string,mixed> $subject |
114
|
|
|
* @return static |
115
|
|
|
* |
116
|
|
|
* @phpstan-param TSubject&(object|array<string,mixed>) $subject |
117
|
|
|
* @phpstan-return static<TSubject> |
118
|
|
|
*/ |
119
|
109 |
|
public static function on($subject): self |
120
|
|
|
{ |
121
|
109 |
|
return new static($subject); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|