|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Roukmoute\HashidsBundle; |
|
6
|
|
|
|
|
7
|
|
|
use Hashids\HashidsInterface; |
|
8
|
|
|
use ReflectionObject; |
|
9
|
|
|
use ReflectionProperty; |
|
10
|
|
|
use RuntimeException; |
|
11
|
|
|
|
|
12
|
|
|
class Hashids implements HashidsInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var HashidsInterface */ |
|
15
|
|
|
private $hashids; |
|
16
|
|
|
|
|
17
|
|
|
/** The minimum hash length. */ |
|
18
|
|
|
private $minHashLength; |
|
19
|
|
|
|
|
20
|
|
|
/** @var ReflectionProperty */ |
|
21
|
|
|
private $minHashLengthReflection; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(HashidsInterface $hashids) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->hashids = $hashids; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function encode(...$numbers) |
|
29
|
|
|
{ |
|
30
|
|
|
return $this->hashids->encode($numbers); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function decode($hash) |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->hashids->decode($hash); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function encodeHex($str) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->hashids->encodeHex($str); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function decodeHex($hash) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->hashids->decodeHex($hash); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Encode parameters to generate a hash with custom minimum hash length. |
|
50
|
|
|
* |
|
51
|
|
|
* @param array ...$numbers parameters to encode |
|
52
|
|
|
*/ |
|
53
|
|
|
public function encodeWithCustomHashLength(int $minHashLength, int ...$numbers): string |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setMinHashLength($minHashLength); |
|
56
|
|
|
|
|
57
|
|
|
$hashid = $this->hashids->encode($numbers); |
|
58
|
|
|
|
|
59
|
|
|
$this->restoreMinHashLength(); |
|
60
|
|
|
|
|
61
|
|
|
return $hashid; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Decode parameter to generate a decoded hash with custom minimum hash length. |
|
66
|
|
|
*/ |
|
67
|
|
|
public function decodeWithCustomHashLength(int $minHashLength, string $hash): array |
|
68
|
|
|
{ |
|
69
|
|
|
$this->setMinHashLength($minHashLength); |
|
70
|
|
|
|
|
71
|
|
|
$decodesHashids = $this->hashids->decode($hash); |
|
72
|
|
|
|
|
73
|
|
|
$this->restoreMinHashLength(); |
|
74
|
|
|
|
|
75
|
|
|
return $decodesHashids; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function setMinHashLength(int $minHashLength): void |
|
79
|
|
|
{ |
|
80
|
|
|
$hashIdsReflection = new ReflectionObject($this->hashids); |
|
81
|
|
|
if (!$hashIdsReflection->hasProperty('minHashLength')) { |
|
82
|
|
|
throw new RuntimeException(sprintf('Missing "minHashLength" property in class "%s"', get_class($this->hashids))); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$this->minHashLengthReflection = new ReflectionProperty(get_class($this->hashids), 'minHashLength'); |
|
86
|
|
|
$this->minHashLengthReflection->setAccessible(true); |
|
87
|
|
|
$this->minHashLength = $this->minHashLengthReflection->getValue($this->hashids); |
|
88
|
|
|
$this->minHashLengthReflection->setValue($this->hashids, $minHashLength); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function restoreMinHashLength(): void |
|
92
|
|
|
{ |
|
93
|
|
|
$this->minHashLengthReflection->setValue($this->hashids, $this->minHashLength); |
|
94
|
|
|
$this->minHashLengthReflection->setAccessible(false); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|