1 | <?php |
||
10 | final class Identity |
||
11 | { |
||
12 | private function __construct() |
||
16 | |||
17 | /** |
||
18 | * Returns a string representing the supplied value's identity. |
||
19 | * |
||
20 | * @param mixed $value |
||
21 | * |
||
22 | * @return string |
||
23 | */ |
||
24 | public static function hash($value) |
||
25 | { |
||
26 | $typeIdentifier = gettype($value)[0]; |
||
27 | |||
28 | switch ($typeIdentifier) { |
||
29 | |||
30 | case 's': //string |
||
31 | |||
32 | return 's' . (strlen($value) > 32 ? md5($value) : $value); |
||
33 | |||
34 | case 'i': //integer |
||
35 | case 'b': //boolean |
||
36 | case 'd': //double |
||
37 | case 'r': //resource |
||
38 | case 'u': //unknown type |
||
39 | |||
40 | return $typeIdentifier . $value; |
||
41 | |||
42 | case 'N': //NULL |
||
43 | |||
44 | return 'N'; |
||
45 | |||
46 | case 'o': //object |
||
47 | |||
48 | return 'o' . spl_object_hash($value); |
||
49 | |||
50 | case 'a': //array |
||
51 | |||
52 | return self::arrayHash($value); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * Returns an array of string representations of the supplied values |
||
58 | * |
||
59 | * @param mixed[] $values |
||
60 | * |
||
61 | * @return string[] |
||
62 | */ |
||
63 | public static function hashAll(array $values) |
||
67 | |||
68 | private static function arrayHash(array $array) |
||
79 | } |
||
80 |