1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mado\QueryBundle\Objects; |
4
|
|
|
|
5
|
|
|
class Filter |
6
|
|
|
{ |
7
|
|
|
private $rawFilter; |
8
|
|
|
|
9
|
|
|
private $ids; |
10
|
|
|
|
11
|
|
|
private $operator; |
12
|
|
|
|
13
|
|
|
private $path; |
14
|
|
|
|
15
|
4 |
|
public static function box(array $params) |
16
|
|
|
{ |
17
|
4 |
|
$rawIds = $params['ids']; |
18
|
4 |
|
$path = $params['path']; |
19
|
|
|
|
20
|
4 |
|
$operator = key($rawIds); |
21
|
4 |
|
$ids = join(',', current($rawIds)); |
22
|
|
|
|
23
|
4 |
|
return new self([ |
24
|
4 |
|
'raw_filter' => self::buildRawFilter($path, $operator), |
25
|
4 |
|
'ids' => $ids, |
26
|
4 |
|
'operator' => $operator, |
27
|
4 |
|
'path' => $path, |
28
|
|
|
]); |
29
|
|
|
} |
30
|
|
|
|
31
|
4 |
|
private static function buildRawFilter($path, $operator) |
32
|
|
|
{ |
33
|
4 |
|
return $path . '.id|' . $operator; |
34
|
|
|
} |
35
|
|
|
|
36
|
5 |
|
private function __construct(array $params) |
37
|
|
|
{ |
38
|
5 |
|
$this->rawFilter = $params['raw_filter']; |
39
|
5 |
|
$this->ids = $params['ids']; |
40
|
5 |
|
$this->operator = $params['operator']; |
41
|
5 |
|
$this->path = $params['path']; |
42
|
5 |
|
} |
43
|
|
|
|
44
|
5 |
|
public function getFieldAndOperator() |
45
|
|
|
{ |
46
|
5 |
|
return $this->rawFilter; |
47
|
|
|
} |
48
|
|
|
|
49
|
1 |
|
public function getIds() |
50
|
|
|
{ |
51
|
1 |
|
return $this->ids; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function getOperator() |
55
|
|
|
{ |
56
|
2 |
|
return $this->operator; |
57
|
|
|
} |
58
|
|
|
|
59
|
4 |
|
public function getPath() |
60
|
|
|
{ |
61
|
4 |
|
return $this->path; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
public function withPath($path) |
65
|
|
|
{ |
66
|
2 |
|
$rawFilter = self::buildRawFilter($path, $this->operator); |
67
|
|
|
|
68
|
2 |
|
if ($path == '') { |
69
|
1 |
|
$rawFilter = str_replace('.', '', $rawFilter); |
70
|
|
|
} |
71
|
|
|
|
72
|
2 |
|
return new self([ |
73
|
2 |
|
'raw_filter' => $rawFilter, |
74
|
2 |
|
'ids' => $this->ids, |
75
|
2 |
|
'operator' => $this->operator, |
76
|
2 |
|
'path' => $path, |
77
|
|
|
]); |
78
|
|
|
} |
79
|
|
|
|
80
|
1 |
|
public function withFullPath($path) |
81
|
|
|
{ |
82
|
1 |
|
$explodedPath = explode('|', $path); |
83
|
|
|
|
84
|
1 |
|
$path = $explodedPath[0]; |
85
|
1 |
|
$operator = $explodedPath[1]; |
86
|
|
|
|
87
|
1 |
|
return new self([ |
88
|
1 |
|
'raw_filter' => join('|', $explodedPath), |
89
|
1 |
|
'ids' => $this->ids, |
90
|
1 |
|
'operator' => $operator, |
91
|
1 |
|
'path' => $path, |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
5 |
|
public function getField() |
96
|
|
|
{ |
97
|
5 |
|
$explodedPath = explode('|', $this->getFieldAndOperator()); |
98
|
|
|
|
99
|
5 |
|
return $explodedPath[0]; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
public static function fromQueryStringFilter(array $params) |
103
|
|
|
{ |
104
|
1 |
|
return new self([ |
105
|
1 |
|
'raw_filter' => key($params), |
106
|
1 |
|
'ids' => current($params), |
107
|
1 |
|
'operator' => explode('|', key($params))[1], |
108
|
|
|
'path' => null, |
109
|
|
|
]); |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
public function getValue() |
113
|
|
|
{ |
114
|
1 |
|
return $this->ids; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|