/
DataTypeChecker.php
178 lines (154 loc) · 4.19 KB
/
DataTypeChecker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Concise\Services;
use Closure;
class DataTypeChecker
{
/**
* @var boolean
*/
protected $excludeMode = false;
/**
* @var array
*/
protected $context = array();
/**
* @param array $context
*/
public function setContext(array $context)
{
$this->context = $context;
}
/**
* @param array $acceptedTypes
* @param mixed $value
* @return bool
*/
public function check(array $acceptedTypes, $value)
{
if ($this->excludeMode === true) {
return $this->throwInvalidArgumentException($acceptedTypes, $value, false, "must not be");
}
if (count($acceptedTypes) === 0) {
return $value;
}
return $this->throwInvalidArgumentException($acceptedTypes, $value, true, "not found in");
}
/**
* @param array $acceptedTypes
* @param mixed $value
* @return bool
*/
protected function matchesInAcceptedTypes(array $acceptedTypes, $value)
{
foreach ($acceptedTypes as $acceptedType) {
if ($this->matches($acceptedType, $value)) {
return true;
}
}
return false;
}
/**
* @param array $acceptedTypes
* @param mixed $value
* @param bool $expecting
* @param string $message
* @return mixed
*/
protected function throwInvalidArgumentException(array $acceptedTypes, $value, $expecting, $message)
{
$match = $this->matchesInAcceptedTypes($acceptedTypes, $value);
if ($expecting === $match) {
if (is_object($value) && $value instanceof \Concise\Syntax\Token\Attribute) {
$value = $this->getAttribute($value->getValue());
}
if (in_array('class', $acceptedTypes) && is_string($value) && substr($value, 0, 1) === '\\') {
return substr($value, 1);
}
if (in_array('regex', $acceptedTypes)) {
return $value;
}
return $value;
}
$accepts = implode(' or ', $acceptedTypes);
throw new \InvalidArgumentException($this->getType($value) . " $message " . $accepts);
}
/**
* @param string $name
* @return mixed
*/
protected function getAttribute($name)
{
if (!array_key_exists($name, $this->context)) {
throw new \Exception("Attribute '$name' does not exist.");
}
return $this->context[$name];
}
protected function isRegex($value)
{
return is_object($value) && get_class($value) === 'Concise\Syntax\Token\Regexp';
}
protected function isAttribute($value)
{
return is_object($value) && get_class($value) === 'Concise\Syntax\Token\Attribute';
}
/**
* @param mixed $value
* @return string
*/
protected function getType($value)
{
if ($this->isRegex($value)) {
return 'regex';
}
if ($this->isAttribute($value)) {
return $this->getType($this->getAttribute($value->getValue()));
}
if ($value instanceof Closure) {
return 'callable';
}
return gettype($value);
}
/**
* @param string $type
* @param string $value
* @return bool
*/
protected function singleMatch($type, $value)
{
return $type === $this->simpleType($this->getType($value));
}
/**
* @param string $type
* @param mixed $value
* @return bool
*/
protected function matches($type, $value)
{
if ($type === 'number') {
return $this->singleMatch('int', $value) || $this->singleMatch('float', $value) || is_numeric($value);
}
return $this->singleMatch($this->simpleType($type), $value);
}
/**
* @param string $type
* @return string
*/
protected function simpleType($type)
{
$aliases = array(
'integer' => 'int',
'double' => 'float',
'class' => 'string',
'bool' => 'boolean',
'regex' => 'string',
);
if (array_key_exists($type, $aliases)) {
return $aliases[$type];
}
return $type;
}
public function setExcludeMode()
{
$this->excludeMode = true;
}
}