1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the phpcommon/comparison package. |
5
|
|
|
* |
6
|
|
|
* (c) Marcos Passos <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE file |
9
|
|
|
* that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PhpCommon\Comparison\Equivalence; |
13
|
|
|
|
14
|
|
|
use PhpCommon\Comparison\Equivalence; |
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Base class for implementations of generic equivalence relations. |
19
|
|
|
* |
20
|
|
|
* @author Marcos Passos <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
abstract class GenericEquivalence implements Equivalence |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Constant that represents the primitive type array . |
26
|
|
|
*/ |
27
|
|
|
const TYPE_ARRAY = 'array'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Constant that represents the primitive type boolean. |
31
|
|
|
*/ |
32
|
|
|
const TYPE_BOOLEAN = 'boolean'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constant that represents the primitive type double. |
36
|
|
|
*/ |
37
|
|
|
const TYPE_DOUBLE = 'double'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constant that represents the primitive type integer. |
41
|
|
|
*/ |
42
|
|
|
const TYPE_INTEGER = 'integer'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constant that represents the primitive type NULL. |
46
|
|
|
*/ |
47
|
|
|
const TYPE_NULL = 'NULL'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Constant that represents the primitive type object. |
51
|
|
|
*/ |
52
|
|
|
const TYPE_OBJECT = 'object'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constant that represents the primitive type resource. |
56
|
|
|
*/ |
57
|
|
|
const TYPE_RESOURCE = 'resource'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constant that represents the primitive type string. |
61
|
|
|
*/ |
62
|
|
|
const TYPE_STRING = 'string'; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* {@inheritdoc} |
66
|
|
|
*/ |
67
|
360 |
|
public function equivalent($left, $right) |
68
|
|
|
{ |
69
|
|
|
// Delegates the call to the proper equivalent*() method |
70
|
360 |
|
$type = gettype($left); |
71
|
|
|
|
72
|
|
|
switch ($type) { |
73
|
360 |
|
case self::TYPE_ARRAY: |
74
|
120 |
|
return $this->equivalentArray($left, $right); |
75
|
|
|
|
76
|
290 |
|
case self::TYPE_BOOLEAN: |
77
|
68 |
|
return $this->equivalentBoolean($left, $right); |
78
|
|
|
|
79
|
252 |
|
case self::TYPE_DOUBLE: |
80
|
36 |
|
return $this->equivalentFloat($left, $right); |
81
|
|
|
|
82
|
218 |
|
case self::TYPE_INTEGER: |
83
|
72 |
|
return $this->equivalentInteger($left, $right); |
84
|
|
|
|
85
|
176 |
|
case self::TYPE_NULL: |
86
|
36 |
|
return $this->equivalentNull($right); |
87
|
|
|
|
88
|
140 |
|
case self::TYPE_OBJECT: |
89
|
80 |
|
return $this->equivalentObject($left, $right); |
90
|
|
|
|
91
|
60 |
|
case self::TYPE_RESOURCE: |
92
|
28 |
|
return $this->equivalentResource($left, $right); |
93
|
|
|
|
94
|
32 |
|
case self::TYPE_STRING: |
95
|
30 |
|
return $this->equivalentString($left, $right); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// This exception should never be thrown unless a new primitive type |
99
|
|
|
// was introduced |
100
|
2 |
|
throw new InvalidArgumentException( |
101
|
2 |
|
sprintf('Unknown type "%s".', $type) |
102
|
2 |
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks whether an array is equivalent to another value. |
107
|
|
|
* |
108
|
|
|
* @param array $left The array to compare. |
109
|
|
|
* @param mixed $right The other value to compare. |
110
|
|
|
* |
111
|
|
|
* @return boolean Returns `true` if the given values are considered |
112
|
|
|
* equivalent, `false` otherwise. |
113
|
|
|
* |
114
|
|
|
* @link http://php.net/manual/en/language.types.array.php PHP array |
115
|
|
|
*/ |
116
|
|
|
abstract protected function equivalentArray(array $left, $right); |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Checks whether a boolean value is equivalent to another value. |
120
|
|
|
* |
121
|
|
|
* @param boolean $left The boolean value to compare. |
122
|
|
|
* @param mixed $right The other value to compare. |
123
|
|
|
* |
124
|
|
|
* @return boolean Returns `true` if the given values are considered |
125
|
|
|
* equivalent, `false` otherwise. |
126
|
|
|
* |
127
|
|
|
* @link http://php.net/manual/en/language.types.boolean.php PHP boolean |
128
|
|
|
*/ |
129
|
|
|
abstract protected function equivalentBoolean($left, $right); |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Checks whether a floating-point number is equivalent to another value. |
133
|
|
|
* |
134
|
|
|
* @param float $left The floating-point number to compare. |
135
|
|
|
* @param mixed $right The other value to compare. |
136
|
|
|
* |
137
|
|
|
* @return boolean Returns `true` if the given values are considered |
138
|
|
|
* equivalent, `false` otherwise. |
139
|
|
|
* |
140
|
|
|
* @link http://php.net/manual/en/language.types.float.php PHP float |
141
|
|
|
*/ |
142
|
|
|
abstract protected function equivalentFloat($left, $right); |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Checks whether an integer number is equivalent to another value. |
146
|
|
|
* |
147
|
|
|
* @param integer $left The integer number to compare. |
148
|
|
|
* @param mixed $right The other value to compare. |
149
|
|
|
* |
150
|
|
|
* @return boolean Returns `true` if the given values are considered |
151
|
|
|
* equivalent, `false` otherwise. |
152
|
|
|
* |
153
|
|
|
* @link http://php.net/manual/en/language.types.integer.php PHP integer |
154
|
|
|
*/ |
155
|
|
|
abstract protected function equivalentInteger($left, $right); |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Checks whether value is equivalent to null. |
159
|
|
|
* |
160
|
|
|
* @param mixed $right The value to compare. |
161
|
|
|
* |
162
|
|
|
* @return boolean Returns `true` if the given value is considered |
163
|
|
|
* equivalent to null, `false` otherwise. |
164
|
|
|
* |
165
|
|
|
* @link http://php.net/manual/en/language.types.null.php PHP NULL |
166
|
|
|
*/ |
167
|
|
|
abstract protected function equivalentNull($right); |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Checks whether an object is equivalent to another value. |
171
|
|
|
* |
172
|
|
|
* @param object $left The object to compare. |
173
|
|
|
* @param mixed $right The other value to compare. |
174
|
|
|
* |
175
|
|
|
* @return boolean Returns `true` if the given values are considered |
176
|
|
|
* equivalent, `false` otherwise. |
177
|
|
|
* |
178
|
|
|
* @link http://php.net/manual/en/language.types.object.php PHP object |
179
|
|
|
*/ |
180
|
|
|
abstract protected function equivalentObject($left, $right); |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Checks whether a resource is equivalent to another value. |
184
|
|
|
* |
185
|
|
|
* @param resource $left The resource to compare. |
186
|
|
|
* @param mixed $right The other value to compare. |
187
|
|
|
* |
188
|
|
|
* @return boolean Returns `true` if the given values are considered |
189
|
|
|
* equivalent, `false` otherwise. |
190
|
|
|
* |
191
|
|
|
* @link http://php.net/manual/en/language.types.resource.php PHP resource |
192
|
|
|
*/ |
193
|
|
|
abstract protected function equivalentResource($left, $right); |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Checks whether a string is equivalent to another value. |
197
|
|
|
* |
198
|
|
|
* @param string $left The string value to compare. |
199
|
|
|
* @param mixed $right The other value to compare. |
200
|
|
|
* |
201
|
|
|
* @return boolean Returns `true` if the given values are considered |
202
|
|
|
* equivalent, `false` otherwise. |
203
|
|
|
* |
204
|
|
|
* @link http://php.net/manual/en/language.types.string.php PHP string |
205
|
|
|
*/ |
206
|
|
|
abstract protected function equivalentString($left, $right); |
207
|
|
|
} |
208
|
|
|
|