-
Notifications
You must be signed in to change notification settings - Fork 3
/
DemoController.php
102 lines (88 loc) · 2.94 KB
/
DemoController.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace Pgs\HashIdBundle\Controller;
use Pgs\HashIdBundle\Annotation\Hash;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route('/hash-id/demo')
*/
class DemoController extends AbstractController
{
/**
* @Route("/encode", requirements={"id"="\d+"})
*
* @param int $id
*/
public function encode($id): Response
{
$other = 30;
$url1 = $this->generateUrl('pgs_hash_id_demo_decode', ['id' => $id, 'other' => $other]);
$url2 = $this->generateUrl('pgs_hash_id_demo_decode_more', ['id' => $id, 'other' => $other]);
$response = <<<EOT
<html>
<body>
Provided id: $id, other: $other <br />
Url with encoded parameter: <a href="$url1">$url1</a><br />
Another url with encoded more parameters: <a href="$url2">$url2</a><br />
</body>
</html>
EOT;
return new Response($response);
}
/**
* @Route("/decode/{id}/{other}")
* @Hash("id")
*/
public function decode(Request $request, int $id, int $other): Response
{
return new Response($this->getDecodeResponse($request, $id, $other));
}
/**
* @Route("/decode_more/{id}/{other}")
* @Hash({"id", "other"})
*/
public function decodeMore(Request $request, int $id, int $other): Response
{
return new Response($this->getDecodeResponse($request, $id, $other));
}
/**
* @Hash("id")
*
* @param int $id
*/
public function encodeLocalized($id): Response
{
$url1 = $this->generateUrl('pgs_hash_id_demo_encode_localized', ['id' => $id, '_locale' => 'pl']);
$url2 = $this->generateUrl('pgs_hash_id_demo_encode_localized', ['id' => $id]);
$response = <<<EOT
<html>
<body>
Provided id: $id<br />
Localized url with encoded parameter and locale provided: <a href="$url1">$url1</a><br />
Localized url with encoded parameter: <a href="$url2">$url2</a><br />
</body>
</html>
EOT;
return new Response($response);
}
private function getDecodeResponse(Request $request, int $id, int $other): string
{
$providedId = $this->getRouteParam($request, 'id');
$providedOther = $this->getRouteParam($request, 'other');
$response = <<<EOT
<html>
<body>
Provided id: <b>$providedId</b>, other: <b>$providedOther</b><br />
Decoded id: <b>$id</b>, other: <b>$other</b><br />
</body>
</html>
EOT;
return $response;
}
private function getRouteParam(Request $request, $param)
{
return $request->attributes->get('_route_params')[$param];
}
}