|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Xgc\CarbonBundle\Request\ParamConverter; |
|
5
|
|
|
|
|
6
|
|
|
use Carbon\Carbon; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
|
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
12
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class CarbonParamConverter |
|
16
|
|
|
* @package Xgc\CarbonBundle\Request |
|
17
|
|
|
*/ |
|
18
|
|
|
class CarbonParamConverter implements ParamConverterInterface |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Stores the object in the request. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Request $request The request |
|
25
|
|
|
* @param ParamConverter $configuration Contains the name, class and options of the object |
|
26
|
|
|
* |
|
27
|
|
|
* @return bool True if the object has been successfully set, else false |
|
28
|
|
|
* |
|
29
|
|
|
* @throws NotFoundHttpException |
|
30
|
|
|
* @throws InvalidArgumentException |
|
31
|
|
|
*/ |
|
32
|
22 |
|
public function apply(Request $request, ParamConverter $configuration): bool |
|
33
|
|
|
{ |
|
34
|
22 |
|
if (!$this->isValidInput($request, $configuration)) { |
|
35
|
2 |
|
return false; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
20 |
|
$param = $configuration->getName(); |
|
39
|
20 |
|
$options = $configuration->getOptions(); |
|
40
|
20 |
|
$value = $request->attributes->get($param); |
|
41
|
20 |
|
$format = $this->loadFormat($options, $value); |
|
42
|
20 |
|
$carbon = $this->getCarbon($format, $value, $param); |
|
43
|
|
|
|
|
44
|
18 |
|
$request->attributes->set($param, $carbon); |
|
45
|
|
|
|
|
46
|
18 |
|
return true; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Checks if the object is supported. |
|
51
|
|
|
* |
|
52
|
|
|
* @param ParamConverter $configuration Should be an instance of ParamConverter |
|
53
|
|
|
* |
|
54
|
|
|
* @return bool True if the object is supported, else false |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function supports(ParamConverter $configuration): bool |
|
57
|
|
|
{ |
|
58
|
2 |
|
if (null === $configuration->getClass()) { |
|
59
|
1 |
|
return false; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
2 |
|
return Carbon::class === $configuration->getClass(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param Request $request |
|
67
|
|
|
* @param ParamConverter $configuration |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
22 |
|
private function isValidInput(Request $request, ParamConverter $configuration): bool |
|
72
|
|
|
{ |
|
73
|
22 |
|
$param = $configuration->getName(); |
|
74
|
|
|
|
|
75
|
|
|
return !( |
|
76
|
22 |
|
(!$request->attributes->has($param)) || |
|
77
|
22 |
|
(!$request->attributes->get($param) && $configuration->isOptional()) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param array $options |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $value |
|
85
|
|
|
* |
|
86
|
|
|
* @return null|string ; |
|
87
|
|
|
*/ |
|
88
|
20 |
|
private function loadFormat(array $options, string $value): ?string |
|
89
|
|
|
{ |
|
90
|
20 |
|
$format = $options['format'] ?? null; |
|
91
|
|
|
|
|
92
|
20 |
|
if (!$format && \filter_var($value, \FILTER_VALIDATE_INT) !== false) { |
|
93
|
1 |
|
$format = 'U'; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
20 |
|
return $format; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param null|string $format |
|
101
|
|
|
* @param string $value |
|
102
|
|
|
* @param string $param |
|
103
|
|
|
* |
|
104
|
|
|
* @return Carbon |
|
105
|
|
|
* |
|
106
|
|
|
* @throws NotFoundHttpException |
|
107
|
|
|
*/ |
|
108
|
20 |
|
private function getCarbon(?string $format, string $value, string $param): Carbon |
|
109
|
|
|
{ |
|
110
|
|
|
try { |
|
111
|
20 |
|
if ($format) { |
|
|
|
|
|
|
112
|
3 |
|
return Carbon::createFromFormat($format, $value); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
17 |
|
return new Carbon($value); |
|
116
|
2 |
|
} catch (Exception $e) { |
|
117
|
2 |
|
throw new NotFoundHttpException(\sprintf('Invalid date given for parameter "%s".', $param)); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: