1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Seblegall\ApiValidatorBundle\Request; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
|
8
|
|
|
class ApiParameterBag extends ParameterBag implements ApiParameterBagInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* override this method to customize which request parameters bags will be used. |
12
|
|
|
* |
13
|
|
|
* @return array |
14
|
|
|
*/ |
15
|
3 |
|
public function getFilteredType() |
16
|
|
|
{ |
17
|
|
|
return array( |
18
|
3 |
|
static::PARAMETERS_TYPE_HEADERS, |
19
|
3 |
|
static::PARAMETERS_TYPE_QUERY, |
20
|
3 |
|
static::PARAMETERS_TYPE_REQUEST, |
21
|
3 |
|
); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* override this methods to select only some parameters key. |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
1 |
|
public function getFilteredKeys() |
30
|
|
|
{ |
31
|
1 |
|
return array(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* prepare all keys to filter before filtering Request. |
36
|
|
|
* |
37
|
|
|
* @return array |
38
|
|
|
*/ |
39
|
9 |
|
protected function prepareFilteredKeys() |
40
|
|
|
{ |
41
|
9 |
|
return $this->getFilteredKeys(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* apply some processing to Request before reading API Parameters from Request. |
46
|
|
|
* |
47
|
|
|
* @param Request $request |
48
|
|
|
*/ |
49
|
9 |
|
protected function prePopulate(Request $request) |
|
|
|
|
50
|
|
|
{ |
51
|
9 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* apply some processing to Request after populating API Parameters from Request. |
55
|
|
|
* |
56
|
|
|
* @param Request $request |
57
|
|
|
*/ |
58
|
9 |
|
protected function postPopulate(Request $request) |
|
|
|
|
59
|
|
|
{ |
60
|
9 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param Request $request |
64
|
|
|
*/ |
65
|
9 |
|
public function populateFromRequest(Request $request) |
66
|
|
|
{ |
67
|
9 |
|
$filteredKeys = $this->prepareFilteredKeys(); |
68
|
|
|
$allowedTypes = array( |
69
|
9 |
|
static::PARAMETERS_TYPE_HEADERS, |
70
|
9 |
|
static::PARAMETERS_TYPE_QUERY, |
71
|
9 |
|
static::PARAMETERS_TYPE_REQUEST, |
72
|
9 |
|
static::PARAMETERS_TYPE_COOKIES, |
73
|
9 |
|
static::PARAMETERS_TYPE_FILES, |
74
|
9 |
|
static::PARAMETERS_TYPE_ATTRIBUTES, |
75
|
9 |
|
); |
76
|
|
|
|
77
|
9 |
|
$this->prePopulate($request); |
78
|
|
|
|
79
|
9 |
|
foreach ($this->getFilteredType() as $type) { |
80
|
9 |
|
if (in_array($type, $allowedTypes)) { |
81
|
9 |
|
$params = empty($filteredKeys) ? |
82
|
9 |
|
$request->$type->all() : |
83
|
9 |
|
array_intersect_key($request->$type->all(), array_flip($filteredKeys)); |
84
|
|
|
|
85
|
9 |
|
if ($type == static::PARAMETERS_TYPE_HEADERS) { |
86
|
3 |
|
$params = array_map(function ($v) { |
87
|
2 |
|
return is_array($v) ? implode(';', $v) : $v; |
88
|
3 |
|
}, $params); |
89
|
3 |
|
} |
90
|
|
|
|
91
|
9 |
|
$this->add($params); |
92
|
9 |
|
} |
93
|
9 |
|
} |
94
|
|
|
|
95
|
9 |
|
$this->postPopulate($request); |
96
|
9 |
|
} |
97
|
|
|
} |
98
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.