1 | <?php |
||
27 | class IdentityHasher extends GenericHasher |
||
28 | { |
||
29 | /** |
||
30 | * Constant used to compute hash codes for `null` |
||
31 | */ |
||
32 | const HASH_NULL = 0; |
||
33 | |||
34 | /** |
||
35 | * Constant used to compute hash codes for arrays |
||
36 | */ |
||
37 | const HASH_ARRAY = 991; |
||
38 | |||
39 | /** |
||
40 | * The hash code for the boolean value `false` |
||
41 | */ |
||
42 | const HASH_FALSE = 1237; |
||
43 | |||
44 | /** |
||
45 | * The hash code for the boolean value `true` |
||
46 | */ |
||
47 | const HASH_TRUE = 1231; |
||
48 | |||
49 | /** |
||
50 | * Constant used to compute hash codes for objects |
||
51 | */ |
||
52 | const HASH_OBJECT = 1093; |
||
53 | |||
54 | /** |
||
55 | * Constant used to compute hash codes for resources |
||
56 | */ |
||
57 | const HASH_RESOURCE = 1471; |
||
58 | |||
59 | /** |
||
60 | * Constant used to compute hash codes for strings |
||
61 | */ |
||
62 | const HASH_STRING = 1321; |
||
63 | |||
64 | /** |
||
65 | * Checks whether the current relation is considered equal to another. |
||
66 | * |
||
67 | * Since this class is stateless, its instances will always be considered |
||
68 | * equal if they are of the same type. |
||
69 | */ |
||
70 | 20 | public function equals(Equatable $other) |
|
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | * |
||
78 | * The specified values are considered equivalent if and only if the |
||
79 | * right-hand value is also an array and they both contain the same |
||
80 | * number of entries, in the same order and each pair of corresponding |
||
81 | * entries is equivalent according to this relation. Empty arrays are |
||
82 | * equivalent to one another. |
||
83 | */ |
||
84 | 46 | protected function equivalentArray(array $left, $right) |
|
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | * |
||
92 | * The specified values are considered equivalent if and only if the |
||
93 | * right-hand value is also a boolean and both are `true` or both are |
||
94 | * `false`. |
||
95 | */ |
||
96 | 66 | protected function equivalentBoolean($left, $right) |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | * |
||
104 | * The specified values are considered equivalent if and only if the |
||
105 | * right-hand value is also a float and they are numerically equal |
||
106 | * (have the same number value). Positive and negative zeros are equal |
||
107 | * to one another. {@link NAN} is not equal to anything, including |
||
108 | * {@link NAN}. |
||
109 | */ |
||
110 | 34 | protected function equivalentFloat($left, $right) |
|
114 | |||
115 | /** |
||
116 | * {@inheritdoc} |
||
117 | * |
||
118 | * The specified values are considered equivalent if and only if the |
||
119 | * right-hand value is also an integer and they are numerically equal |
||
120 | * (have the same number value). Positive and negative zeros are equal |
||
121 | * to one another. |
||
122 | */ |
||
123 | 70 | protected function equivalentInteger($left, $right) |
|
127 | |||
128 | /** |
||
129 | * {@inheritdoc} |
||
130 | * |
||
131 | * The specified value is considered equivalent to `null` if and only if it |
||
132 | * is strictly equal to `null`. |
||
133 | */ |
||
134 | 34 | protected function equivalentNull($right) |
|
138 | |||
139 | /** |
||
140 | * {@inheritdoc} |
||
141 | * |
||
142 | * The specified values are considered equivalent if and only if the |
||
143 | * right-hand value is also an object and both are references to the |
||
144 | * same instance. |
||
145 | */ |
||
146 | 44 | protected function equivalentObject($left, $right) |
|
150 | |||
151 | /** |
||
152 | * {@inheritdoc} |
||
153 | * |
||
154 | * The specified values are considered equivalent if and only if the |
||
155 | * right-hand value is also a resource and both have the same unique |
||
156 | * resource number. |
||
157 | */ |
||
158 | 26 | protected function equivalentResource($left, $right) |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | * |
||
166 | * The specified values are considered equivalent if and only if the |
||
167 | * right-hand value is also a string and both have the same sequence of |
||
168 | * characters. |
||
169 | */ |
||
170 | 82 | protected function equivalentString($left, $right) |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | * |
||
178 | * The hash code is based on the _deep contents_ of the specified array. |
||
179 | * More precisely, it is computed as follows: |
||
180 | * |
||
181 | * ```php |
||
182 | * $hashCode = IdentityEquivalence::HASH_ARRAY; |
||
183 | * foreach ($array => $key => $element) |
||
184 | * $hashCode = 31 * $hashCode + (hash($key) ^ hash($element)); |
||
185 | * ``` |
||
186 | * |
||
187 | * Where `hash()` is a function that returns a hash code for a given value. |
||
188 | * Note that the hash code of an empty array is the constant |
||
189 | * {@link IdentityEquivalence::HASH_ARRAY} itself. |
||
190 | */ |
||
191 | 12 | protected function hashArray(array $value) |
|
204 | |||
205 | /** |
||
206 | * {@inheritdoc} |
||
207 | * |
||
208 | * It is defined by mapping the values `true` and `false` to |
||
209 | * integer numbers defined by {@link IdentityEquivalence::HASH_TRUE} and |
||
210 | * {@link IdentityEquivalence::HASH_FALSE} respectively. |
||
211 | */ |
||
212 | 8 | protected function hashBoolean($value) |
|
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | * |
||
220 | * It is computed by converting the float value to the integer bit |
||
221 | * representation, according to the IEEE 754 floating-point single format |
||
222 | * bit layout. |
||
223 | * |
||
224 | * @link https://pt.wikipedia.org/wiki/IEEE_754 IEEE Standard for |
||
225 | * Floating-Point Arithmetic. |
||
226 | * |
||
227 | */ |
||
228 | 6 | protected function hashFloat($value) |
|
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | * |
||
236 | * This method uses the integer number as the hash code itself. |
||
237 | */ |
||
238 | 12 | protected function hashInteger($value) |
|
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | * |
||
246 | * The hash code for `null` is a constant defined by |
||
247 | * {@link IdentityEquivalence::HASH_NULL}. |
||
248 | */ |
||
249 | 6 | protected function hashNull() |
|
253 | |||
254 | /** |
||
255 | * {@inheritdoc} |
||
256 | * |
||
257 | * The hash code is computed as follows: |
||
258 | * |
||
259 | * ```php |
||
260 | * $k = IdentityEquivalence::HASH_OBJECT; |
||
261 | * $hashCode = $k * hashString(spl_object_hash($object)); |
||
262 | * ``` |
||
263 | * |
||
264 | * Where {@link IdentityEquivalence::HASH_OBJECT} is a constant and |
||
265 | * `hashString()` is a function that returns a hash code for a given |
||
266 | * string. |
||
267 | */ |
||
268 | 6 | protected function hashObject($value) |
|
272 | |||
273 | /** |
||
274 | * {@inheritdoc} |
||
275 | * |
||
276 | * The hash code is computed as follows: |
||
277 | * |
||
278 | * ```php |
||
279 | * $hashCode = IdentityEquivalence::HASH_STRING; |
||
280 | * for ($i = 0; $i < length($string); $i++) |
||
281 | * $hashCode = $hashCode * 31 + charCode($string[$i]); |
||
282 | * ``` |
||
283 | * |
||
284 | * Where {@link IdentityEquivalence::HASH_STRING} is a constant, `$i` is |
||
285 | * the position of the current character in the string, `$string[$i]` is |
||
286 | * the ith character of the string, `length()` is a function that returns |
||
287 | * the length of the string, `charCode()` is a function that returns the |
||
288 | * ASCII code (as an integer) of a given character, and `^` indicates |
||
289 | * exponentiation. Note that the hash code of a zero length string is the |
||
290 | * constant {@link IdentityEquivalence::HASH_STRING} itself. |
||
291 | */ |
||
292 | 16 | protected function hashString($value) |
|
308 | |||
309 | /** |
||
310 | * {@inheritdoc} |
||
311 | * |
||
312 | * The hash code is computed as follows: |
||
313 | * |
||
314 | * ```php |
||
315 | * $k = IdentityEquivalence::HASH_RESOURCE; |
||
316 | * $hashCode = $k * (1 + resourceId($value)); |
||
317 | * ``` |
||
318 | * |
||
319 | * Where `$k` is a constant defined by |
||
320 | * {@link IdentityEquivalence::HASH_RESOURCE} and `resourceId()` is a |
||
321 | * function that returns the unique resource number assigned to the |
||
322 | * resource by PHP at runtime. |
||
323 | */ |
||
324 | 4 | protected function hashResource($value) |
|
328 | } |
||
329 |