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