1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* To change this license header, choose License Headers in Project Properties. |
5
|
|
|
* To change this template file, choose Tools | Templates |
6
|
|
|
* and open the template in the editor. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Gendoria\ParamConverterBundle\Request\ParamConverter; |
10
|
|
|
|
11
|
|
|
use ArrayObject; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
14
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* This param converter converts parameters to array objects. |
19
|
|
|
* |
20
|
|
|
* Parameter has to be string of values separated with delimiter. By default |
21
|
|
|
* delimiter is comma, but you can set your own by passing options. |
22
|
|
|
* |
23
|
|
|
* Param converter invocation: |
24
|
|
|
* |
25
|
|
|
* `@ParamConverter("parameter_name")` |
26
|
|
|
* |
27
|
|
|
* With custom delimiter: |
28
|
|
|
* |
29
|
|
|
* `@ParamConverter("parameter_name", options={"delimiter" = "|"})` |
30
|
|
|
* |
31
|
|
|
* @author Tomasz Struczyński <[email protected]> |
32
|
|
|
*/ |
33
|
|
|
class ArrayObjectParamConverter implements ParamConverterInterface |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
* |
38
|
|
|
* @throws InvalidArgumentException Thrown, when parameter has been already parsed to array or object. |
39
|
|
|
*/ |
40
|
6 |
|
public function apply(Request $request, ParamConverter $configuration) |
41
|
|
|
{ |
42
|
|
|
$param = $configuration->getName(); |
43
|
|
|
|
44
|
|
|
if (!$request->attributes->has($param)) { |
45
|
1 |
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$options = $configuration->getOptions(); |
49
|
|
|
$value = $request->attributes->get($param); |
50
|
|
|
|
51
|
5 |
|
if (!$value && $configuration->isOptional()) { |
52
|
1 |
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (is_object($value) || is_array($value)) { |
56
|
|
|
throw new InvalidArgumentException("Parameter already parsed to object or array."); |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
if (!empty($options['delimiter'])) { |
60
|
1 |
|
$delimiter = $options['delimiter']; |
61
|
|
|
} else { |
62
|
3 |
|
$delimiter = ','; |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
$array = $this->getArrayObject($delimiter, $value); |
66
|
|
|
$request->attributes->set($param, $array); |
67
|
|
|
|
68
|
4 |
|
return true; |
69
|
6 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get array object instance based on parameter value. |
73
|
|
|
* |
74
|
|
|
* @param string $delimiter |
75
|
|
|
* @param string $value |
76
|
|
|
* @return ArrayObject |
77
|
|
|
*/ |
78
|
4 |
|
private function getArrayObject($delimiter, $value) |
79
|
|
|
{ |
80
|
4 |
|
if (!empty($value)) { |
81
|
|
|
$arrayValues = explode($delimiter, $value); |
82
|
|
|
} else { |
83
|
1 |
|
$arrayValues = array(); |
84
|
3 |
|
} |
85
|
|
|
return new ArrayObject($arrayValues); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
1 |
|
public function supports(ParamConverter $configuration) |
92
|
|
|
{ |
93
|
|
|
if (null === $configuration->getClass()) { |
94
|
1 |
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return 'ArrayObject' === $configuration->getClass(); |
98
|
|
|
} |
99
|
|
|
} |