|
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
|
4 |
|
public static function box(array $params) |
|
14
|
|
|
{ |
|
15
|
4 |
|
$rawIds = $params['ids']; |
|
16
|
4 |
|
$path = $params['path']; |
|
17
|
|
|
|
|
18
|
4 |
|
$operator = key($rawIds); |
|
19
|
4 |
|
$ids = join(',', current($rawIds)); |
|
20
|
|
|
|
|
21
|
4 |
|
return new self([ |
|
22
|
4 |
|
'raw_filter' => self::buildRawFilter($path, $operator), |
|
23
|
4 |
|
'ids' => $ids, |
|
24
|
4 |
|
'operator' => $operator, |
|
25
|
4 |
|
'path' => $path, |
|
26
|
|
|
]); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
4 |
|
private static function buildRawFilter($path, $operator) |
|
30
|
|
|
{ |
|
31
|
4 |
|
return $path . '.id|' . $operator; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
5 |
|
private function __construct(array $params) |
|
35
|
|
|
{ |
|
36
|
5 |
|
$this->rawFilter = $params['raw_filter']; |
|
37
|
5 |
|
$this->ids = $params['ids']; |
|
38
|
5 |
|
$this->operator = $params['operator']; |
|
39
|
5 |
|
$this->path = $params['path']; |
|
|
|
|
|
|
40
|
5 |
|
} |
|
41
|
|
|
|
|
42
|
5 |
|
public function getFieldAndOperator() |
|
43
|
|
|
{ |
|
44
|
5 |
|
return $this->rawFilter; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
1 |
|
public function getIds() |
|
48
|
|
|
{ |
|
49
|
1 |
|
return $this->ids; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
public function getOperator() |
|
53
|
|
|
{ |
|
54
|
2 |
|
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
|
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
|
|
|
|