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
|
|
|
$decodedSuccessfuly = $this->decodeHashidOnRoute($request); |
30
|
|
|
|
31
|
|
|
if($decodedSuccessfuly === false) { |
32
|
|
|
$this->decodeArgumentsController($request, $configuration); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this->continueWithNextParamConverters(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function supports(ParamConverter $configuration): bool |
39
|
|
|
{ |
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function decodeHashidOnRoute(Request $request): bool |
44
|
|
|
{ |
45
|
|
|
foreach (['hashid', 'id'] as $item) { |
46
|
|
|
$hashids = $this->hashids->decode($request->attributes->get($item)); |
47
|
|
|
if ($this->hasHashidDecoded($hashids)) { |
48
|
|
|
$hashid = current($hashids); |
49
|
|
|
|
50
|
|
|
$request->attributes->remove($item); |
51
|
|
|
$request->attributes->set('id', $hashid); |
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function decodeArgumentsController(Request $request, ParamConverter $configuration): void |
60
|
|
|
{ |
61
|
|
|
$name = $configuration->getName(); |
62
|
|
|
|
63
|
|
|
$attributes = array_map(function ($name) { |
64
|
|
|
return str_replace('_hash_', '', $name); |
65
|
|
|
}, $request->attributes->keys()); |
66
|
|
|
|
67
|
|
|
$hash = null; |
68
|
|
|
|
69
|
|
|
if (in_array($name, $attributes, true)) { |
70
|
|
|
$hash = $request->attributes->get('_hash_' . $name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!isset($hash) && $this->autoConvert) { |
74
|
|
|
$hash = (string) $request->attributes->get($name); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($this->isSkippable($hash)) { |
78
|
|
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$hashids = $this->hashids->decode($hash); |
82
|
|
|
|
83
|
|
|
if ($this->hasHashidDecoded($hashids)) { |
84
|
|
|
$request->attributes->set($name, current($hashids)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!$this->autoConvert && !$this->hasHashidDecoded($hashids)) { |
88
|
|
|
throw new \LogicException(sprintf('Unable to decode parameter "%s".', $name)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function isSkippable($hash): bool |
93
|
|
|
{ |
94
|
|
|
return empty($hash) || !$this->allCharsAreInAlphabet($hash); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function allCharsAreInAlphabet($hash): bool |
98
|
|
|
{ |
99
|
|
|
return (bool) preg_match(sprintf('{^[%s]+$}', $this->alphabet), $hash); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function hasHashidDecoded($hashids): bool |
103
|
|
|
{ |
104
|
|
|
return $hashids && is_iterable($hashids) && is_int(reset($hashids)); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function continueWithNextParamConverters(): bool |
108
|
|
|
{ |
109
|
|
|
return !$this->passthrough; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|