1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of gpupo/search |
5
|
|
|
* |
6
|
|
|
* (c) Gilmar Pupo <[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 Gpupo\Search\Query; |
13
|
|
|
|
14
|
|
|
abstract class FiltersAbstract |
15
|
|
|
{ |
16
|
|
|
protected $list = []; |
17
|
|
|
|
18
|
|
|
protected function add(array $array) |
19
|
|
|
{ |
20
|
|
|
$this->list[$array['key']] = $array; |
21
|
|
|
|
22
|
|
|
return $this; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function append($key, $value) |
26
|
|
|
{ |
27
|
|
|
$this->list[$key]['values'][] = $value; |
28
|
|
|
|
29
|
|
|
return $this; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Acesso aos valores de um filtro. |
34
|
|
|
* |
35
|
|
|
* @param string $key |
36
|
|
|
* |
37
|
|
|
* @return array|null |
38
|
|
|
*/ |
39
|
|
|
public function getValues($key) |
40
|
|
|
{ |
41
|
|
|
if (array_key_exists($key, $this->list)) { |
42
|
|
|
return $this->list[$key]['values']; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Adiciona um valor a ValuesFilter existente. |
48
|
|
|
* |
49
|
|
|
* @param string $key |
50
|
|
|
* @param type $value |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function appendValueFilter($key, $value) |
55
|
|
|
{ |
56
|
|
|
if (!empty($value)) { |
57
|
|
|
if ($this->getValues($key)) { |
58
|
|
|
return $this->append($key, $value); |
59
|
|
|
} else { |
60
|
|
|
return $this->addValuesFilter($key, [$value]); |
61
|
|
|
} |
62
|
|
|
} else { |
63
|
|
|
//do nothing |
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Filtra por Lista de valores de uma chave. |
70
|
|
|
* |
71
|
|
|
* @param string $key The key to filter on |
72
|
|
|
* @param array $values The values to be filtered |
73
|
|
|
*/ |
74
|
|
|
public function addValuesFilter($key, array $values) |
75
|
|
|
{ |
76
|
|
|
$array = [ |
77
|
|
|
'key' => $key, |
78
|
|
|
'values' => $values, |
79
|
|
|
]; |
80
|
|
|
|
81
|
|
|
return $this->add($array); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Adiciona um filtro a partir de string no formato 0-10 (inicio - fim). |
86
|
|
|
* |
87
|
|
|
* @param string $key |
88
|
|
|
* @param string $string |
89
|
|
|
*/ |
90
|
|
|
public function addStringRangeFilter($key, $string) |
91
|
|
|
{ |
92
|
|
|
$array = explode('-', trim($string)); |
93
|
|
|
|
94
|
|
|
$min = intval($array[0]); |
95
|
|
|
if (empty($min)) { |
96
|
|
|
$min = 1; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$max = intval($array[1]); |
100
|
|
|
if (empty($max)) { |
101
|
|
|
$max = 9999; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this->addRangeFilter($key, $min, $max); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function addRangeFilter($key, $min, $max) |
108
|
|
|
{ |
109
|
|
|
$array = [ |
110
|
|
|
'key' => $key, |
111
|
|
|
'min' => $min, |
112
|
|
|
'max' => $max, |
113
|
|
|
]; |
114
|
|
|
|
115
|
|
|
return $this->add($array); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function addGreaterThanFilter($key, $int) |
119
|
|
|
{ |
120
|
|
|
return $this->addRangeFilter($key, $int, 99999); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Sintaxe de retorno: |
125
|
|
|
* <code>. |
126
|
|
|
* |
127
|
|
|
* array( // Filters only support integer values |
128
|
|
|
* array( |
129
|
|
|
* 'key' => 'some_search_key', // The key to filter on |
130
|
|
|
* 'values' => array(30,...), // The values to be filtered |
131
|
|
|
* ), |
132
|
|
|
* array( // This is a range filter |
133
|
|
|
* 'key' => 'some_search_key', // The key to filter on |
134
|
|
|
* 'min' => 5, // Min and Max value to filter between |
135
|
|
|
* 'max' => 105, |
136
|
|
|
* ), |
137
|
|
|
* ... |
138
|
|
|
* ); |
139
|
|
|
* </code> |
140
|
|
|
*/ |
141
|
|
|
public function toArray() |
142
|
|
|
{ |
143
|
|
|
return $this->list; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|