1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Roukmoute\HashidsBundle\ParamConverter; |
6
|
|
|
|
7
|
|
|
use Hashids\HashidsInterface; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
|
12
|
|
|
class HashidsParamConverter implements ParamConverterInterface |
13
|
|
|
{ |
14
|
|
|
private string $alphabet; |
15
|
|
|
private bool $autoConvert; |
16
|
|
|
private HashidsInterface $hashids; |
17
|
|
|
private bool $passthrough; |
18
|
|
|
|
19
|
|
|
public function __construct(HashidsInterface $hashids, bool $passthrough, bool $autoConvert, string $alphabet) |
20
|
|
|
{ |
21
|
|
|
$this->hashids = $hashids; |
22
|
|
|
$this->passthrough = $passthrough; |
23
|
|
|
$this->autoConvert = $autoConvert; |
24
|
|
|
$this->alphabet = $alphabet; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function apply(Request $request, ParamConverter $configuration): bool |
28
|
|
|
{ |
29
|
|
|
$this->decodeArgumentsController($request, $configuration); |
30
|
|
|
|
31
|
|
|
return $this->continueWithNextParamConverters(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function supports(ParamConverter $configuration): bool |
35
|
|
|
{ |
36
|
|
|
return true; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private function decodeArgumentsController(Request $request, ParamConverter $configuration): void |
40
|
|
|
{ |
41
|
|
|
$name = $configuration->getName(); |
42
|
|
|
$hash = $this->getHash($request, $name); |
43
|
|
|
|
44
|
|
|
if ($this->isSkippable($hash)) { |
45
|
|
|
return; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$hashids = $this->hashids->decode($hash); |
49
|
|
|
|
50
|
|
|
if ($this->hasHashidDecoded($hashids)) { |
51
|
|
|
$request->attributes->set($name, current($hashids)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (!$this->autoConvert && !$this->hasHashidDecoded($hashids)) { |
55
|
|
|
throw new \LogicException(sprintf('Unable to decode parameter "%s".', $name)); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function getHash(Request $request, string &$name): ?string |
60
|
|
|
{ |
61
|
|
|
if (empty($name)) { |
62
|
|
|
return null; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$hash = $request->attributes->get('_hash_' . $name); |
66
|
|
|
|
67
|
|
|
if (!isset($hash) && $this->autoConvert) { |
68
|
|
|
$hash = (string) $request->attributes->get($name); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
if (!isset($hash) && !$request->attributes->has('hashids_prevent_alias')) { |
72
|
|
|
foreach (['hashid', 'id'] as $alias) { |
73
|
|
|
if ($request->attributes->has($alias)) { |
74
|
|
|
$hash = $request->attributes->get($alias); |
75
|
|
|
$request->attributes->set('hashids_prevent_alias', true); |
76
|
|
|
$name = 'id'; |
77
|
|
|
break; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $hash; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
private function isSkippable($hash): bool |
86
|
|
|
{ |
87
|
|
|
return empty($hash) || !$this->allCharsAreInAlphabet($hash); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
private function allCharsAreInAlphabet($hash): bool |
91
|
|
|
{ |
92
|
|
|
return (bool) preg_match(sprintf('{^[%s]+$}', $this->alphabet), $hash); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
private function hasHashidDecoded($hashids): bool |
96
|
|
|
{ |
97
|
|
|
return $hashids && is_iterable($hashids) && is_int(reset($hashids)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function continueWithNextParamConverters(): bool |
101
|
|
|
{ |
102
|
|
|
return !$this->passthrough; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|