These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Elastica; |
||
4 | |||
5 | use Elastica\Exception\InvalidException; |
||
6 | |||
7 | /** |
||
8 | * Class to handle params. |
||
9 | * |
||
10 | * This function can be used to handle params for queries, filter |
||
11 | * |
||
12 | * @author Nicolas Ruflin <[email protected]> |
||
13 | */ |
||
14 | class Param implements ArrayableInterface, \Countable |
||
15 | { |
||
16 | /** |
||
17 | * Params. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $_params = []; |
||
22 | |||
23 | /** |
||
24 | * Raw Params. |
||
25 | * |
||
26 | * @var array |
||
27 | */ |
||
28 | protected $_rawParams = []; |
||
29 | |||
30 | /** |
||
31 | * Converts the params to an array. A default implementation exist to create |
||
32 | * the an array out of the class name (last part of the class name) |
||
33 | * and the params. |
||
34 | * |
||
35 | * @return array Filter array |
||
36 | */ |
||
37 | View Code Duplication | public function toArray() |
|
0 ignored issues
–
show
|
|||
38 | { |
||
39 | $data = [$this->_getBaseName() => $this->getParams()]; |
||
40 | |||
41 | if (!empty($this->_rawParams)) { |
||
42 | $data = \array_merge($data, $this->_rawParams); |
||
43 | } |
||
44 | |||
45 | return $this->_convertArrayable($data); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Sets (overwrites) the value at the given key. |
||
50 | * |
||
51 | * @param string $key Key to set |
||
52 | * @param mixed $value Key Value |
||
53 | * |
||
54 | * @return $this |
||
55 | */ |
||
56 | public function setParam($key, $value) |
||
57 | { |
||
58 | $this->_params[$key] = $value; |
||
59 | |||
60 | return $this; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * Sets (overwrites) all params of this object. |
||
65 | * |
||
66 | * @param array $params Parameter list |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | public function setParams(array $params) |
||
71 | { |
||
72 | $this->_params = $params; |
||
73 | |||
74 | return $this; |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Adds a param to the list. |
||
79 | * |
||
80 | * This function can be used to add an array of params |
||
81 | * |
||
82 | * @param string $key Param key |
||
83 | * @param mixed $value Value to set |
||
84 | * |
||
85 | * @return $this |
||
86 | */ |
||
87 | public function addParam($key, $value) |
||
88 | { |
||
89 | $this->_params[$key][] = $value; |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Returns a specific param. |
||
96 | * |
||
97 | * @param string $key Key to return |
||
98 | * |
||
99 | * @throws \Elastica\Exception\InvalidException If requested key is not set |
||
100 | * |
||
101 | * @return mixed Key value |
||
102 | */ |
||
103 | public function getParam($key) |
||
104 | { |
||
105 | if (!$this->hasParam($key)) { |
||
106 | throw new InvalidException('Param '.$key.' does not exist'); |
||
107 | } |
||
108 | |||
109 | return $this->_params[$key]; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Test if a param is set. |
||
114 | * |
||
115 | * @param string $key Key to test |
||
116 | * |
||
117 | * @return bool True if the param is set, false otherwise |
||
118 | */ |
||
119 | public function hasParam($key) |
||
120 | { |
||
121 | return isset($this->_params[$key]); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Returns the params array. |
||
126 | * |
||
127 | * @return array Params |
||
128 | */ |
||
129 | public function getParams() |
||
130 | { |
||
131 | return $this->_params; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * {@inheritdoc} |
||
136 | * |
||
137 | * @return int |
||
138 | */ |
||
139 | public function count() |
||
140 | { |
||
141 | return \count($this->_params); |
||
142 | } |
||
143 | |||
144 | /** |
||
145 | * Cast objects to arrays. |
||
146 | * |
||
147 | * @return array |
||
148 | */ |
||
149 | protected function _convertArrayable(array $array) |
||
150 | { |
||
151 | $arr = []; |
||
152 | |||
153 | foreach ($array as $key => $value) { |
||
154 | if ($value instanceof ArrayableInterface) { |
||
155 | $arr[$value instanceof NameableInterface ? $value->getName() : $key] = $value->toArray(); |
||
156 | } elseif (\is_array($value)) { |
||
157 | $arr[$key] = $this->_convertArrayable($value); |
||
158 | } else { |
||
159 | $arr[$key] = $value; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | return $arr; |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Param's name |
||
168 | * Picks the last part of the class name and makes it snake_case |
||
169 | * You can override this method if you want to change the name. |
||
170 | * |
||
171 | * @return string name |
||
172 | */ |
||
173 | protected function _getBaseName() |
||
174 | { |
||
175 | return Util::getParamName($this); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Sets params not inside params array. |
||
180 | * |
||
181 | * @param string $key |
||
182 | * @param mixed $value |
||
183 | * |
||
184 | * @return $this |
||
185 | */ |
||
186 | protected function _setRawParam($key, $value) |
||
187 | { |
||
188 | $this->_rawParams[$key] = $value; |
||
189 | |||
190 | return $this; |
||
191 | } |
||
192 | } |
||
193 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.