1 | <?php |
||
31 | class ValueHasher extends IdentityHasher |
||
32 | { |
||
33 | /** |
||
34 | * Maps a class name to an equivalence relation. |
||
35 | * |
||
36 | * @var Equivalence[] |
||
37 | */ |
||
38 | protected $equivalences = []; |
||
39 | |||
40 | /** |
||
41 | * Creates a new value based equivalence relation. |
||
42 | * |
||
43 | * @param array $equivalences The equivalence relation mapping, with class |
||
44 | * names as keys and relations as value. |
||
45 | */ |
||
46 | 156 | public function __construct(array $equivalences = []) |
|
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | * |
||
54 | * Two instances are considered equals if they are of the same |
||
55 | * and if every type-specific relation defined in one is equal to the |
||
56 | * corresponding relation in the other. |
||
57 | */ |
||
58 | 20 | public function equals(Equatable $other) |
|
87 | |||
88 | /** |
||
89 | * Returns the type-specific equivalence relations. |
||
90 | * |
||
91 | * @return Equivalence[] The type-specific relations, with class names as |
||
92 | * keys and relations as values. |
||
93 | */ |
||
94 | 14 | public function getEquivalences() |
|
98 | |||
99 | /** |
||
100 | * Returns an equivalence relation suitable for comparing objects of the |
||
101 | * specified class, if any. |
||
102 | * |
||
103 | * When no relation is explicitly defined for the specified class, this |
||
104 | * method traverses up the class hierarchy to find the nearest ancestor for |
||
105 | * which a relation is specified. For example, a relation specified for the |
||
106 | * class `Vehicle` is used to compare instances of its subclass `Car`, when |
||
107 | * no relation is explicitly specified for it. |
||
108 | * |
||
109 | * @param string $className The fully qualified name of the class for which |
||
110 | * the relation should be suitable for. |
||
111 | * |
||
112 | * @return Equivalence|boolean The relation suitable for comparing objects |
||
113 | * of the specified class, or `null` no |
||
114 | * suitable relation is found. |
||
115 | */ |
||
116 | 34 | protected function getEquivalence($className) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 72 | protected function equivalentArray(array $left, $right) |
|
156 | |||
157 | /** |
||
158 | * {@inheritdoc} |
||
159 | * |
||
160 | * The values are considered equivalent if any of the following conditions |
||
161 | * hold: |
||
162 | * |
||
163 | * 1. The reference value is an instance of {@link Equatable} and the |
||
164 | * expression `$left->equals($right)` is evaluated to `true` |
||
165 | * 2. A specific equivalence relation is mapped to the type of the left-hand |
||
166 | * value and the expression `$relation->equivalent($left, $right)` is |
||
167 | * evaluated to `true` |
||
168 | * 3. Both values refer to the same instance of the same class (in a |
||
169 | * particular namespace) |
||
170 | */ |
||
171 | 62 | protected function equivalentObject($left, $right) |
|
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | * |
||
201 | * The resulting hash code is guaranteed to be _consistent_ with the |
||
202 | * {@link equivalentObject()} method, which means that for any references |
||
203 | * `$x` and `$y`, if `equivalentObject($x, $y)`, then |
||
204 | * `hashObject($x) === hashObject($y)`. |
||
205 | * |
||
206 | * The hash code is computed as follows: |
||
207 | * |
||
208 | * 1. If the specified object is an instance of {@link Hashable}, delegates |
||
209 | * the hashing strategy to the object being hashed. |
||
210 | * 2. If a specific equivalence relation of type {@link Hasher} is mapped |
||
211 | * to the type of the given object, then uses the method |
||
212 | * {@link PhpCommon\Comparison\Hasher::hash()} as the hashing function |
||
213 | * 3. If none of the previous rules apply, uses the method |
||
214 | * {@link PhpCommon\Comparison\Hasher\IdentityHasher::hashObject()} as |
||
215 | * the hash function |
||
216 | * |
||
217 | * @see hashObject() |
||
218 | * @see PhpCommon\Comparison\Hashable::getHash() |
||
219 | */ |
||
220 | 14 | protected function hashObject($value) |
|
243 | } |
||
244 |