-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeyHasher.php
66 lines (55 loc) · 1.58 KB
/
KeyHasher.php
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
<?php
/*
* Opulence
*
* @link https://www.opulencephp.com
* @copyright Copyright (C) 2021 David Young
* @license https://github.com/opulencephp/Opulence/blob/1.2/LICENSE.md
*/
namespace Opulence\Collections;
use RuntimeException;
use Throwable;
/**
* Defines the key hasher
* @internal
*/
class KeyHasher
{
/**
* Gets the hash key for a value
*
* @param string|float|int|object|array|resource $value The value whose hash key we want
* @return string The value's hash key
* @throws RuntimeException Thrown if the value's hash key could not be calculated
*/
public function getHashKey($value) : string
{
if (is_string($value)) {
return "__opulence:s:$value";
}
if (is_int($value)) {
return "__opulence:i:$value";
}
if (is_float($value)) {
return "__opulence:f:$value";
}
if (is_object($value)) {
if (method_exists($value, '__toString')) {
return "__opulence:so:$value";
}
return '__opulence:o:' . spl_object_hash($value);
}
if (is_array($value)) {
return '__opulence:a:' . md5(serialize($value));
}
if (is_resource($value)) {
return '__opulence:r:' . "$value";
}
// As a last-ditch effort, try to convert the value to a string
try {
return '__opulence:u' . (string)$value;
} catch (Throwable $ex) {
throw new RuntimeException('Value could not be converted to a key', 0, $ex);
}
}
}