|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Jasny\DotKey\Internal; |
|
6
|
|
|
|
|
7
|
|
|
use Jasny\DotKey\ResolveException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Static methods to read subject using path. |
|
11
|
|
|
* @internal |
|
12
|
|
|
*/ |
|
13
|
|
|
final class Read |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Check if path exists in subject. |
|
17
|
|
|
* |
|
18
|
|
|
* @param object|array<string,mixed> $subject |
|
19
|
|
|
* @param string $path |
|
20
|
|
|
* @param string $delimiter |
|
21
|
|
|
* @return bool |
|
22
|
|
|
*/ |
|
23
|
18 |
|
public static function exists($subject, string $path, string $delimiter): bool |
|
24
|
|
|
{ |
|
25
|
18 |
|
$index = Helpers::splitPath($path, $delimiter); |
|
26
|
|
|
|
|
27
|
18 |
|
foreach ($index as $key) { |
|
28
|
18 |
|
if (!\is_array($subject) && !\is_object($subject)) { |
|
29
|
4 |
|
return false; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
18 |
|
$subject = Helpers::descend($subject, $key, $exists, true); |
|
33
|
|
|
|
|
34
|
18 |
|
if (!$exists) { |
|
35
|
14 |
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
14 |
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Get a value from subject by path. |
|
44
|
|
|
* |
|
45
|
|
|
* @param object|array<string,mixed> $subject |
|
46
|
|
|
* @param string $path |
|
47
|
|
|
* @param string $delimiter |
|
48
|
|
|
* @return mixed |
|
49
|
|
|
* @throws ResolveException |
|
50
|
|
|
*/ |
|
51
|
21 |
|
public static function get($subject, string $path, string $delimiter) |
|
52
|
|
|
{ |
|
53
|
21 |
|
$index = Helpers::splitPath($path, $delimiter); |
|
54
|
|
|
|
|
55
|
19 |
|
while ($index !== []) { |
|
56
|
19 |
|
if (!\is_array($subject) && !\is_object($subject)) { |
|
57
|
5 |
|
$msg = "Unable to get '$path': '%s' is of type " . \gettype($subject); |
|
58
|
5 |
|
throw ResolveException::create($msg, $path, $delimiter, $index); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
19 |
|
$key = \array_shift($index); |
|
62
|
|
|
|
|
63
|
|
|
try { |
|
64
|
19 |
|
$subject = Helpers::descend($subject, $key, $exists); |
|
65
|
3 |
|
} catch (\Error $error) { |
|
66
|
3 |
|
$msg = "Unable to get '$path': error at '%s'"; |
|
67
|
3 |
|
throw ResolveException::create($msg, $path, $delimiter, $index, $error); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
17 |
|
if (!$exists) { |
|
71
|
10 |
|
return null; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
10 |
|
return $subject; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Check if a value from subject by path is the same as the given value. |
|
80
|
|
|
* |
|
81
|
|
|
* @param object|array<string,mixed> $subject |
|
82
|
|
|
* @param string $path |
|
83
|
|
|
* @param string $delimiter |
|
84
|
|
|
* @param mixed $value |
|
85
|
|
|
* @return bool |
|
86
|
|
|
*/ |
|
87
|
16 |
|
public static function same($subject, string $path, string $delimiter, $value): bool |
|
88
|
|
|
{ |
|
89
|
16 |
|
$index = Helpers::splitPath($path, $delimiter); |
|
90
|
|
|
|
|
91
|
16 |
|
foreach ($index as $key) { |
|
92
|
16 |
|
if (!\is_array($subject) && !\is_object($subject)) { |
|
93
|
1 |
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
16 |
|
$subject = Helpers::descend($subject, $key, $exists, true); |
|
97
|
|
|
|
|
98
|
16 |
|
if (!$exists) { |
|
99
|
8 |
|
return false; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
11 |
|
return $subject === $value; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|