Overview

Namespaces

  • PhpCommon
    • Comparison
      • Equivalence
      • Hasher

Classes

  • DateTimeHasher
  • GenericHasher
  • IdentityHasher
  • ValueHasher
  • Overview
  • Namespace
  • Class

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
Extended by PhpCommon\Comparison\Hasher\GenericHasher implements PhpCommon\Comparison\Hasher
Extended by PhpCommon\Comparison\Hasher\IdentityHasher
Extended by PhpCommon\Comparison\Hasher\ValueHasher
Namespace: PhpCommon\Comparison\Hasher
Author: Marcos Passos marcos@croct.com
Located at Hasher/ValueHasher.php
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

PhpCommon\Comparison\Hasher\IdentityHasher::equals()
public PhpCommon\Comparison\Equivalence[]
# getEquivalences( )

Returns the type-specific equivalence relations.

Returns the type-specific equivalence relations.

Returns

PhpCommon\Comparison\Equivalence[]

The type-specific relations, with class names as keys and relations as values.

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

PhpCommon\Comparison\Equivalence|boolean

The relation suitable for comparing objects of the specified class, or null no suitable relation is found.

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

PhpCommon\Comparison\Hasher\IdentityHasher::equivalentArray()
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:

  1. The reference value is an instance of PhpCommon\Comparison\Equatable and the expression $left->equals($right) is evaluated to true
  2. 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
  3. 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

PhpCommon\Comparison\Hasher\IdentityHasher::equivalentObject()
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:

  1. If the specified object is an instance of PhpCommon\Comparison\Hashable, delegates the hashing strategy to the object being hashed.
  2. 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
  3. 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

PhpCommon\Comparison\Hasher\ValueHasher::hashObject()
PhpCommon\Comparison\Hashable::getHash()

Overrides

PhpCommon\Comparison\Hasher\IdentityHasher::hashObject()
Methods inherited from PhpCommon\Comparison\Hasher\IdentityHasher
equivalentBoolean(), equivalentFloat(), equivalentInteger(), equivalentNull(), equivalentResource(), equivalentString(), hashArray(), hashBoolean(), hashFloat(), hashInteger(), hashNull(), hashResource(), hashString()
Methods inherited from PhpCommon\Comparison\Hasher\GenericHasher
hash()
Methods inherited from PhpCommon\Comparison\Equivalence\GenericEquivalence
equivalent()
Constants inherited from PhpCommon\Comparison\Hasher\IdentityHasher
HASH_ARRAY, HASH_FALSE, HASH_NULL, HASH_OBJECT, HASH_RESOURCE, HASH_STRING, HASH_TRUE
Constants inherited from PhpCommon\Comparison\Equivalence\GenericEquivalence
TYPE_ARRAY, TYPE_BOOLEAN, TYPE_DOUBLE, TYPE_INTEGER, TYPE_NULL, TYPE_OBJECT, TYPE_RESOURCE, TYPE_STRING
Properties summary
protected PhpCommon\Comparison\Equivalence[] $equivalences

Maps a class name to an equivalence relation.

Maps a class name to an equivalence relation.

# []
PHPCommon Comparison API API documentation generated by ApiGen