1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Respect/Validation. |
5
|
|
|
* |
6
|
|
|
* (c) Alexandre Gomes Gaigalas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the "LICENSE.md" |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Respect\Validation\Rules; |
13
|
|
|
|
14
|
|
|
use ArrayAccess; |
15
|
|
|
use Respect\Validation\Exceptions\ComponentException; |
16
|
|
|
use Respect\Validation\Rule; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Validates an array key or an object property using `.` to represent nested data. |
20
|
|
|
* |
21
|
|
|
* @author Henrique Moody <[email protected]> |
22
|
|
|
* @author Ivan Zinovyev <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @since 1.0.0 |
25
|
|
|
*/ |
26
|
|
|
final class KeyNested extends AbstractRelated |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* Initializes the rule. |
30
|
|
|
* |
31
|
|
|
* @param string $reference |
32
|
|
|
* @param Rule $rule |
33
|
|
|
* @param bool $mandatory |
34
|
|
|
*/ |
35
|
|
|
public function __construct(string $reference, Rule $rule = null, bool $mandatory = true) |
36
|
|
|
{ |
37
|
|
|
parent::__construct($reference, $rule, $mandatory); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Verifies if the input has the reference. |
42
|
|
|
* |
43
|
|
|
* @param mixed $input |
44
|
|
|
* @param string $reference |
45
|
|
|
* |
46
|
|
|
* @return bool |
47
|
|
|
*/ |
48
|
44 |
|
protected function hasReference($input, $reference): bool |
49
|
|
|
{ |
50
|
|
|
try { |
51
|
44 |
|
$this->getReferenceValue($input, $reference); |
52
|
16 |
|
} catch (ComponentException $exceptions) { |
53
|
16 |
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
28 |
|
return true; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get the value for the reference on the input. |
61
|
|
|
* |
62
|
|
|
* @param mixed $input |
63
|
|
|
* @param string $reference |
64
|
|
|
* |
65
|
|
|
* @throws ComponentException When the value cannot be fetch |
66
|
|
|
* |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
44 |
|
protected function getReferenceValue($input, $reference) |
70
|
|
|
{ |
71
|
44 |
|
if (is_scalar($input)) { |
72
|
8 |
|
$message = sprintf('Cannot select the %s in the given data', $reference); |
73
|
8 |
|
throw new ComponentException($message); |
74
|
|
|
} |
75
|
|
|
|
76
|
36 |
|
$keys = explode('.', rtrim($reference, '.')); |
77
|
36 |
|
$value = $input; |
78
|
36 |
|
while (!is_null($key = array_shift($keys))) { |
79
|
36 |
|
$value = $this->getValue($reference, $value, $key); |
80
|
|
|
} |
81
|
|
|
|
82
|
28 |
|
return $value; |
83
|
|
|
} |
84
|
|
|
|
85
|
36 |
|
private function getValue(string $reference, $value, string $key) |
86
|
|
|
{ |
87
|
36 |
|
if (is_array($value) || $value instanceof ArrayAccess) { |
88
|
36 |
|
return $this->getValueFromArray($reference, $value, $key); |
89
|
|
|
} |
90
|
|
|
|
91
|
12 |
|
if (is_object($value)) { |
92
|
8 |
|
return $this->getValueFromObject($reference, $value, $key); |
93
|
|
|
} |
94
|
|
|
|
95
|
4 |
|
$message = sprintf('Cannot select the reference %s from the given data', $reference); |
96
|
4 |
|
throw new ComponentException($message); |
97
|
|
|
} |
98
|
|
|
|
99
|
36 |
|
private function getValueFromArray(string $reference, $array, string $key) |
100
|
|
|
{ |
101
|
36 |
|
if ((is_array($array) && !array_key_exists($key, $array)) |
102
|
36 |
|
|| !isset($array[$key])) { |
103
|
4 |
|
$message = sprintf('Cannot select the key %s from the given array', $reference); |
104
|
4 |
|
throw new ComponentException($message); |
105
|
|
|
} |
106
|
|
|
|
107
|
36 |
|
return $array[$key]; |
108
|
|
|
} |
109
|
|
|
|
110
|
8 |
|
private function getValueFromObject(string $reference, $object, string $property) |
111
|
|
|
{ |
112
|
8 |
|
if (empty($property) || !property_exists($object, $property)) { |
113
|
|
|
$message = sprintf('Cannot select the property %s from the given object', $reference); |
114
|
|
|
throw new ComponentException($message); |
115
|
|
|
} |
116
|
|
|
|
117
|
8 |
|
return $object->{$property}; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|