GenericHasher::hashArray()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
ccs 0
cts 0
cp 0
nc 1
1
<?php
2
3
/**
4
 * This file is part of the phpcommon/comparison package.
5
 *
6
 * (c) Marcos Passos <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE file
9
 * that was distributed with this source code.
10
 */
11
12
namespace PhpCommon\Comparison\Hasher;
13
14
use PhpCommon\Comparison\Equivalence\GenericEquivalence;
15
use PhpCommon\Comparison\Hasher;
16
use InvalidArgumentException;
17
18
/**
19
 * Base class for implementing generic hashers.
20
 *
21
 * @author Marcos Passos <[email protected]>
22
 */
23
abstract class GenericHasher extends GenericEquivalence implements Hasher
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 66
    public function hash($value)
29
    {
30
        // Delegates the call to the proper hash*() method
31 66
        $type = gettype($value);
32
33
        switch ($type) {
34 66
            case self::TYPE_ARRAY:
35 14
                return $this->hashArray($value);
36
37 60
            case self::TYPE_BOOLEAN:
38 10
                return $this->hashBoolean($value);
39
40 54
            case self::TYPE_DOUBLE:
41 8
                return $this->hashFloat($value);
42
43 46
            case self::TYPE_INTEGER:
44 14
                return $this->hashInteger($value);
45
46 40
            case self::TYPE_NULL:
47 8
                return $this->hashNull();
48
49 32
            case self::TYPE_OBJECT:
50 18
                return $this->hashObject($value);
51
52 14
            case self::TYPE_RESOURCE:
53 6
                return $this->hashResource($value);
54
55 8
            case self::TYPE_STRING:
56 6
                return $this->hashString($value);
57
        }
58
59
        // This exception should never be thrown unless a new primitive type
60
        // was introduced
61 2
        throw new InvalidArgumentException(
62 2
            sprintf('Unknown type "%s".', $type)
63 2
        );
64
    }
65
66
    /**
67
     * Returns a hash code for the given array.
68
     *
69
     * The resulting hash code is guaranteed to be _consistent_ with the
70
     * {@link equivalentArray()} method, which means that for any references
71
     * `$x` and `$y`, if `equivalentArray($x, $y)`, then
72
     * `hashArray($x) === hashArray($y)`.
73
     *
74
     * @param array $value The array to hash.
75
     *
76
     * @return integer The hash code for the given array.
77
     *
78
     * @link http://php.net/manual/en/language.types.array.php PHP array
79
     */
80
    abstract protected function hashArray(array $value);
81
82
    /**
83
     * Returns a hash code for the given boolean value.
84
     *
85
     * The resulting hash code is guaranteed to be _consistent_ with the
86
     * {@link equivalentBoolean()} method, which means that for any references
87
     * `$x` and `$y`, if `equivalentBoolean($x, $y)`, then
88
     * `hashBoolean($x) === hashBoolean($y)`.
89
     *
90
     * @param boolean $value The boolean value to hash.
91
     *
92
     * @return integer The hash code for the given boolean value.
93
     *
94
     * @link http://php.net/manual/en/language.types.boolean.php PHP boolean
95
     */
96
    abstract protected function hashBoolean($value);
97
98
    /**
99
     * Returns a hash code for the given floating-point number.
100
     *
101
     * The resulting hash code is guaranteed to be _consistent_ with the
102
     * {@link equivalentFloat()} method, which means that for any references
103
     * `$x` and `$y`, if `equivalentFloat($x, $y)`, then
104
     * `hashFloat($x) === hashFloat($y)`.
105
     *
106
     * @param float $value The floating-point number to hash.
107
     *
108
     * @return integer The hash code for the given floating-point number.
109
     *
110
     * @link http://php.net/manual/en/language.types.float.php PHP float
111
     */
112
    abstract protected function hashFloat($value);
113
114
    /**
115
     * Returns a hash code for the given integer number.
116
     *
117
     * The resulting hash code is guaranteed to be _consistent_ with the
118
     * {@link equivalentInteger()} method, which means that for any references
119
     * `$x` and `$y`, if `equivalentInteger($x, $y)`, then
120
     * `hashInteger($x) === hashInteger($y)`.
121
     *
122
     * @param integer $value The integer number to hash.
123
     *
124
     * @return integer The hash code for the given integer number.
125
     *
126
     * @link http://php.net/manual/en/language.types.integer.php PHP interger
127
     */
128
    abstract protected function hashInteger($value);
129
130
    /**
131
     * Returns a hash code for the null value.
132
     *
133
     * @return integer The hash code for the `NULL` value.
134
     *
135
     * @link http://php.net/manual/en/language.types.null.php PHP NULL
136
     */
137
    abstract protected function hashNull();
138
139
    /**
140
     * Returns a hash code for the given object.
141
     *
142
     * The resulting hash code is guaranteed to be _consistent_ with the
143
     * {@link equivalentObject()} method, which means that for any references
144
     * `$x` and `$y`, if `equivalentObject($x, $y)`, then
145
     * `hashObject($x) === hashObject($y)`.
146
     *
147
     * @param object $value The object to hash.
148
     *
149
     * @return integer The hash code for the given object.
150
     *
151
     * @link http://php.net/manual/en/language.types.object.php PHP object
152
     */
153
    abstract protected function hashObject($value);
154
155
    /**
156
     * Returns a hash code for the given resource.
157
     *
158
     * The resulting hash code is guaranteed to be _consistent_ with the
159
     * {@link equivalentResource()} method, which means that for any references
160
     * `$x` and `$y`, if `equivalentResource($x, $y)`, then
161
     * `hashResource($x) === hashResource($y)`.
162
     *
163
     * @param resource $value The resource to hash.
164
     *
165
     * @return integer The hash code for the given resource.
166
     *
167
     * @link http://php.net/manual/en/language.types.resource.php PHP resource
168
     */
169
    abstract protected function hashResource($value);
170
171
    /**
172
     * Returns a hash code for the given string value.
173
     *
174
     * The resulting hash code is guaranteed to be _consistent_ with the
175
     * {@link equivalentString()} method, which means that for any references
176
     * `$x` and `$y`, if `equivalentString($x, $y)`, then
177
     * `hashString($x) === hashString($y)`.
178
     *
179
     * @param string $value The string value to hash.
180
     *
181
     * @return integer The hash code for the given object.
182
     *
183
     * @link http://php.net/manual/en/language.types.string.php PHP string
184
     */
185
    abstract protected function hashString($value);
186
}
187