Overview

Namespaces

  • PhpCommon
    • Comparison
      • Equivalence
      • Hasher

Classes

  • PhpCommon\Comparison\Equivalence\GenericEquivalence
  • PhpCommon\Comparison\Hasher\DateTimeHasher
  • PhpCommon\Comparison\Hasher\GenericHasher
  • PhpCommon\Comparison\Hasher\IdentityHasher
  • PhpCommon\Comparison\Hasher\ValueHasher

Interfaces

  • PhpCommon\Comparison\Comparable
  • PhpCommon\Comparison\Comparator
  • PhpCommon\Comparison\Equatable
  • PhpCommon\Comparison\Equivalence
  • PhpCommon\Comparison\Hashable
  • PhpCommon\Comparison\Hasher

Exceptions

  • PhpCommon\Comparison\IncomparableException
  • PhpCommon\Comparison\UnexpectedTypeException
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 
<?php

/**
 * This file is part of the phpcommon/comparison package.
 *
 * (c) Marcos Passos <marcos@marcospassos.com>
 *
 * For the full copyright and license information, please view the LICENSE file
 * that was distributed with this source code.
 */

namespace PhpCommon\Comparison\Hasher;

use PhpCommon\Comparison\Equivalence\GenericEquivalence;
use PhpCommon\Comparison\Hasher;
use InvalidArgumentException;

/**
 * Base class for implementing generic hashers.
 *
 * @author Marcos Passos <marcos@marcospassos.com>
 */
abstract class GenericHasher extends GenericEquivalence implements Hasher
{
    /**
     * {@inheritdoc}
     */
    public function hash($value)
    {
        // Delegates the call to the proper hash*() method
        $type = gettype($value);

        switch ($type) {
            case self::TYPE_ARRAY:
                return $this->hashArray($value);

            case self::TYPE_BOOLEAN:
                return $this->hashBoolean($value);

            case self::TYPE_DOUBLE:
                return $this->hashFloat($value);

            case self::TYPE_INTEGER:
                return $this->hashInteger($value);

            case self::TYPE_NULL:
                return $this->hashNull();

            case self::TYPE_OBJECT:
                return $this->hashObject($value);

            case self::TYPE_RESOURCE:
                return $this->hashResource($value);

            case self::TYPE_STRING:
                return $this->hashString($value);
        }

        // This exception should never be thrown unless a new primitive type
        // was introduced
        throw new InvalidArgumentException(
            sprintf('Unknown type "%s".', $type)
        );
    }

    /**
     * Returns a hash code for the given array.
     *
     * The resulting hash code is guaranteed to be _consistent_ with the
     * {@link equivalentArray()} method, which means that for any references
     * `$x` and `$y`, if `equivalentArray($x, $y)`, then
     * `hashArray($x) === hashArray($y)`.
     *
     * @param array $value The array to hash.
     *
     * @return integer The hash code for the given array.
     *
     * @link http://php.net/manual/en/language.types.array.php PHP array
     */
    abstract protected function hashArray(array $value);

    /**
     * Returns a hash code for the given boolean value.
     *
     * The resulting hash code is guaranteed to be _consistent_ with the
     * {@link equivalentBoolean()} method, which means that for any references
     * `$x` and `$y`, if `equivalentBoolean($x, $y)`, then
     * `hashBoolean($x) === hashBoolean($y)`.
     *
     * @param boolean $value The boolean value to hash.
     *
     * @return integer The hash code for the given boolean value.
     *
     * @link http://php.net/manual/en/language.types.boolean.php PHP boolean
     */
    abstract protected function hashBoolean($value);

    /**
     * Returns a hash code for the given floating-point number.
     *
     * The resulting hash code is guaranteed to be _consistent_ with the
     * {@link equivalentFloat()} method, which means that for any references
     * `$x` and `$y`, if `equivalentFloat($x, $y)`, then
     * `hashFloat($x) === hashFloat($y)`.
     *
     * @param float $value The floating-point number to hash.
     *
     * @return integer The hash code for the given floating-point number.
     *
     * @link http://php.net/manual/en/language.types.float.php PHP float
     */
    abstract protected function hashFloat($value);

    /**
     * Returns a hash code for the given integer number.
     *
     * The resulting hash code is guaranteed to be _consistent_ with the
     * {@link equivalentInteger()} method, which means that for any references
     * `$x` and `$y`, if `equivalentInteger($x, $y)`, then
     * `hashInteger($x) === hashInteger($y)`.
     *
     * @param integer $value The integer number to hash.
     *
     * @return integer The hash code for the given integer number.
     *
     * @link http://php.net/manual/en/language.types.integer.php PHP interger
     */
    abstract protected function hashInteger($value);

    /**
     * Returns a hash code for the null value.
     *
     * @return integer The hash code for the `NULL` value.
     *
     * @link http://php.net/manual/en/language.types.null.php PHP NULL
     */
    abstract protected function hashNull();

    /**
     * Returns a hash code for the given object.
     *
     * The resulting hash code is guaranteed to be _consistent_ with the
     * {@link equivalentObject()} method, which means that for any references
     * `$x` and `$y`, if `equivalentObject($x, $y)`, then
     * `hashObject($x) === hashObject($y)`.
     *
     * @param object $value The object to hash.
     *
     * @return integer The hash code for the given object.
     *
     * @link http://php.net/manual/en/language.types.object.php PHP object
     */
    abstract protected function hashObject($value);

    /**
     * Returns a hash code for the given resource.
     *
     * The resulting hash code is guaranteed to be _consistent_ with the
     * {@link equivalentResource()} method, which means that for any references
     * `$x` and `$y`, if `equivalentResource($x, $y)`, then
     * `hashResource($x) === hashResource($y)`.
     *
     * @param resource $value The resource to hash.
     *
     * @return integer The hash code for the given resource.
     *
     * @link http://php.net/manual/en/language.types.resource.php PHP resource
     */
    abstract protected function hashResource($value);

    /**
     * Returns a hash code for the given string value.
     *
     * The resulting hash code is guaranteed to be _consistent_ with the
     * {@link equivalentString()} method, which means that for any references
     * `$x` and `$y`, if `equivalentString($x, $y)`, then
     * `hashString($x) === hashString($y)`.
     *
     * @param string $value The string value to hash.
     *
     * @return integer The hash code for the given object.
     *
     * @link http://php.net/manual/en/language.types.string.php PHP string
     */
    abstract protected function hashString($value);
}
PHPCommon Comparison API API documentation generated by ApiGen