|
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
|
|
|
|
|
17
|
|
|
class KeyNested |
|
18
|
|
|
{ |
|
19
|
|
|
public function hasReference($input) |
|
20
|
|
|
{ |
|
21
|
|
|
try { |
|
22
|
|
|
$this->getReferenceValue($input); |
|
23
|
|
|
} catch (ComponentException $cex) { |
|
24
|
|
|
return false; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
return true; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
private function getReferencePieces() |
|
31
|
|
|
{ |
|
32
|
|
|
return explode('.', rtrim($this->reference, '.')); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
private function getValueFromArray($array, $key) |
|
36
|
|
|
{ |
|
37
|
|
|
if (!array_key_exists($key, $array)) { |
|
38
|
|
|
$message = sprintf('Cannot select the key %s from the given array', $this->reference); |
|
39
|
|
|
throw new ComponentException($message); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $array[$key]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
private function getValueFromObject($object, $property) |
|
46
|
|
|
{ |
|
47
|
|
|
if (empty($property) || !property_exists($object, $property)) { |
|
48
|
|
|
$message = sprintf('Cannot select the property %s from the given object', $this->reference); |
|
49
|
|
|
throw new ComponentException($message); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $object->{$property}; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
private function getValue($value, $key) |
|
56
|
|
|
{ |
|
57
|
|
|
if (is_array($value) || $value instanceof ArrayAccess) { |
|
58
|
|
|
return $this->getValueFromArray($value, $key); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
if (is_object($value)) { |
|
62
|
|
|
return $this->getValueFromObject($value, $key); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$message = sprintf('Cannot select the property %s from the given data', $this->reference); |
|
66
|
|
|
throw new ComponentException($message); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function getReferenceValue($input) |
|
70
|
|
|
{ |
|
71
|
|
|
if (is_scalar($input)) { |
|
72
|
|
|
$message = sprintf('Cannot select the %s in the given data', $this->reference); |
|
73
|
|
|
throw new ComponentException($message); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$keys = $this->getReferencePieces(); |
|
77
|
|
|
$value = $input; |
|
78
|
|
|
while (!is_null($key = array_shift($keys))) { |
|
79
|
|
|
$value = $this->getValue($value, $key); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $value; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: