1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2015-07-05 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Net\Bazzline\Component\Cli\Readline\Configuration; |
8
|
|
|
|
9
|
|
|
use Closure; |
10
|
|
|
use Net\Bazzline\Component\GenericAgreement\Data\ValidatorInterface; |
11
|
|
|
use Net\Bazzline\Component\GenericAgreement\Exception\InvalidArgument; |
12
|
|
|
|
13
|
|
|
class Validator implements ValidatorInterface |
14
|
|
|
{ |
15
|
|
|
/** @var string */ |
16
|
|
|
private $message; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $trace; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param mixed $data |
23
|
|
|
* @return boolean |
24
|
|
|
*/ |
25
|
|
|
public function isValid($data) |
26
|
|
|
{ |
27
|
|
|
$this->resetMessageAndTrace(); |
28
|
|
|
|
29
|
|
|
try { |
30
|
|
|
$this->validate($data); |
31
|
|
|
$isValid = true; |
32
|
|
|
} catch (InvalidArgument $exception) { |
33
|
|
|
$this->message = $exception->getMessage(); |
34
|
|
|
$this->trace = $exception->getTraceAsString(); |
35
|
|
|
$isValid = false; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $isValid; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return null|string |
43
|
|
|
*/ |
44
|
|
|
public function getMessage() |
45
|
|
|
{ |
46
|
|
|
return $this->message; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return null|string |
51
|
|
|
*/ |
52
|
|
|
public function getTrace() |
53
|
|
|
{ |
54
|
|
|
return $this->trace; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private function resetMessageAndTrace() |
58
|
|
|
{ |
59
|
|
|
$this->message = null; |
60
|
|
|
$this->trace = null; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param array $configuration |
65
|
|
|
* @param null $path |
66
|
|
|
*/ |
67
|
|
|
private function validate($configuration, $path = null) |
68
|
|
|
{ |
69
|
|
|
if (!is_array($configuration)) { |
70
|
|
|
throw new InvalidArgument('configuration ' . (is_null($path) ? '' : ' in path "' . $path . '" ') . 'must be an array'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (empty($configuration)) { |
74
|
|
|
throw new InvalidArgument('configuration ' . (is_null($path) ? '' : ' in path "' . $path . '" ') . 'can not be empty'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
foreach ($configuration as $index => $arrayOrCallable) { |
78
|
|
|
$currentPath = (is_null($path)) ? $index : $path . '/' . $index; |
79
|
|
|
|
80
|
|
|
if (is_string($arrayOrCallable)) { |
81
|
|
|
if (!is_callable($arrayOrCallable)) { |
82
|
|
|
throw new InvalidArgument('method in path "' . $currentPath . '" must be callable'); |
83
|
|
|
} |
84
|
|
|
} else if (is_array($arrayOrCallable)) { |
85
|
|
|
$object = current($arrayOrCallable); |
86
|
|
|
|
87
|
|
|
if (is_object($object) && $this->isNotAnClosure($object)) { |
88
|
|
|
$methodName = $arrayOrCallable[1]; |
89
|
|
|
if (!method_exists($object, $methodName)) { |
90
|
|
|
throw new InvalidArgument( |
91
|
|
|
'provided instance of "' . get_class($object) . '" in path "' . $currentPath . '" does not have the method "' . $methodName . '"' |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
$this->validate($arrayOrCallable, $currentPath); |
96
|
|
|
} |
97
|
|
|
} else { |
98
|
|
|
if ($this->isNotAnClosure($arrayOrCallable)) { |
99
|
|
|
throw new InvalidArgument( |
100
|
|
|
'can not handle value "' . var_export($arrayOrCallable, true) . '" in path "' . $currentPath . '"' |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param mixed $data |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
|
|
private function isNotAnClosure($data) |
112
|
|
|
{ |
113
|
|
|
return !($data instanceof Closure); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|