1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the LoopBackApiBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) Théo FIDRY <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Fidry\LoopBackApiBundle\Http\Request; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Théo FIDRY <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class FilterQueryExtractor implements FilterQueryExtractorInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
private $filterEntryPoint; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string key of the filter query for this filter |
28
|
|
|
*/ |
29
|
|
|
private $filterParameter; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $filterParameter |
33
|
|
|
* @param string|null $filterEntryPoint |
34
|
|
|
*/ |
35
|
|
|
public function __construct($filterParameter, $filterEntryPoint = null) |
36
|
|
|
{ |
37
|
|
|
$this->filterEntryPoint = $filterEntryPoint; |
38
|
|
|
$this->filterParameter = $filterParameter; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* {@inheritdoc} |
43
|
|
|
* |
44
|
|
|
* @example |
45
|
|
|
* $filterParameter = null |
46
|
|
|
* $filterEntryPoint = order |
47
|
|
|
* query parameters = ?order[prop]=ASC |
48
|
|
|
* |
49
|
|
|
* ::extractProperties($request) |
50
|
|
|
* => [ |
51
|
|
|
* 'prop' => 'ASC' |
52
|
|
|
* ] |
53
|
|
|
* |
54
|
|
|
* |
55
|
|
|
* $filterParameter = filter |
56
|
|
|
* $filterEntryPoint = order |
57
|
|
|
* query parameters = ?filter[order][prop]=ASC |
58
|
|
|
* |
59
|
|
|
* ::extractProperties($request) |
60
|
|
|
* => [ |
61
|
|
|
* 'prop' => 'ASC' |
62
|
|
|
* ] |
63
|
|
|
*/ |
64
|
|
|
public function extractProperties(Request $request) |
65
|
|
|
{ |
66
|
|
|
if (null === $this->filterEntryPoint) { |
67
|
|
|
$filter = $request->query->all(); |
68
|
|
|
} else { |
69
|
|
|
$filter = $request->query->get($this->filterEntryPoint, []); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (array_key_exists($this->filterParameter, $filter)) { |
73
|
|
|
return $filter[$this->filterParameter]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return []; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|