Class ValueHasher
Provides an external means for producing hash codes and comparing values for
equality.
This equivalence relation delegates the equality check and hashing
strategy to the methods PhpCommon\Comparison\Equatable::equals() and
PhpCommon\Comparison\Hashable::getHash(), whenever the handled
values are instances of Equatable
and Hashable
respectively.
-
PhpCommon\Comparison\Equivalence\GenericEquivalence
implements
PhpCommon\Comparison\Equivalence
-
PhpCommon\Comparison\Hasher\GenericHasher
implements
PhpCommon\Comparison\Hasher
-
PhpCommon\Comparison\Hasher\IdentityHasher
-
PhpCommon\Comparison\Hasher\ValueHasher
Methods summary
public
|
#
__construct( array $equivalences = [] )
Creates a new value based equivalence relation.
Creates a new value based equivalence relation.
Parameters
- $equivalences
The equivalence relation mapping, with class
names as keys and relations as value.
|
public
boolean
|
#
equals( PhpCommon\Comparison\Equatable $other )
Checks whether the current relation is considered equal to another.
Checks whether the current relation is considered equal to another.
Two instances are considered equals if they are of the same
and if every type-specific relation defined in one is equal to the
corresponding relation in the other.
Parameters
- $other
- The value to compare to.
Returns
boolean Returns true if the current object is equal to the
specified object, false otherwise.
Overrides
|
public
PhpCommon\Comparison\Equivalence[]
|
#
getEquivalences( )
Returns the type-specific equivalence relations.
Returns the type-specific equivalence relations.
Returns
|
protected
PhpCommon\Comparison\Equivalence |boolean
|
#
getEquivalence( string $className )
Returns an equivalence relation suitable for comparing objects of the
specified class, if any.
Returns an equivalence relation suitable for comparing objects of the
specified class, if any.
When no relation is explicitly defined for the specified class, this
method traverses up the class hierarchy to find the nearest ancestor for
which a relation is specified. For example, a relation specified for the
class Vehicle is used to compare instances of its subclass Car , when
no relation is explicitly specified for it.
Parameters
- $className
The fully qualified name of the class for which
the relation should be suitable for.
Returns
|
protected
boolean
|
#
equivalentArray( array $left, mixed $right )
Checks whether an array is equivalent to another value.
Checks whether an array is equivalent to another value.
Parameters
- $left
- The array to compare.
- $right
- The other value to compare.
Returns
boolean Returns true if the given values are considered
equivalent, false otherwise.
Overrides
|
protected
boolean
|
#
equivalentObject( object $left, mixed $right )
Checks whether an object is equivalent to another value.
Checks whether an object is equivalent to another value.
The values are considered equivalent if any of the following conditions
hold:
- The reference value is an instance of
PhpCommon\Comparison\Equatable and the expression $left->equals($right) is evaluated to true
- A specific equivalence relation is mapped to the type of the left-hand value and the expression
$relation->equivalent($left, $right) is evaluated to true
- Both values refer to the same instance of the same class (in a particular namespace)
Parameters
- $left
- The object to compare.
- $right
- The other value to compare.
Returns
boolean Returns true if the given values are considered
equivalent, false otherwise.
Overrides
|
protected
integer
|
#
hashObject( object $value )
Returns a hash code for the given object.
Returns a hash code for the given object.
The resulting hash code is guaranteed to be consistent with the
PhpCommon\Comparison\Hasher\ValueHasher::equivalentObject() method, which means that for any references
$x and $y , if equivalentObject($x, $y) , then
hashObject($x) === hashObject($y) .
The hash code is computed as follows:
- If the specified object is an instance of
PhpCommon\Comparison\Hashable , delegates the hashing strategy to the object being hashed.
- If a specific equivalence relation of type
PhpCommon\Comparison\Hasher is mapped to the type of the given object, then uses the method PhpCommon\Comparison\Hasher::hash() as the hashing function
- If none of the previous rules apply, uses the method PhpCommon\Comparison\Hasher\IdentityHasher::hashObject() as the hash function
Parameters
- $value
- The object to hash.
Returns
integer The hash code for the given object.
See
Overrides
|
Methods inherited from PhpCommon\Comparison\Hasher\IdentityHasher
equivalentBoolean(),
equivalentFloat(),
equivalentInteger(),
equivalentNull(),
equivalentResource(),
equivalentString(),
hashArray(),
hashBoolean(),
hashFloat(),
hashInteger(),
hashNull(),
hashResource(),
hashString()
|