Completed
Push — master ( 3a2d29...d0475e )
by Nicolas
02:57
created

lib/Elastica/Param.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Elastica;
3
4
use Elastica\Exception\InvalidException;
5
6
/**
7
 * Class to handle params.
8
 *
9
 * This function can be used to handle params for queries, filter
10
 *
11
 * @author Nicolas Ruflin <[email protected]>
12
 */
13
class Param implements ArrayableInterface
14
{
15
    /**
16
     * Params.
17
     *
18
     * @var array
19
     */
20
    protected $_params = [];
21
22
    /**
23
     * Raw Params.
24
     *
25
     * @var array
26
     */
27
    protected $_rawParams = [];
28
29
    /**
30
     * Converts the params to an array. A default implementation exist to create
31
     * the an array out of the class name (last part of the class name)
32
     * and the params.
33
     *
34
     * @return array Filter array
35
     */
36
    public function toArray()
37
    {
38
        $data = [$this->_getBaseName() => $this->getParams()];
39
40
        if (!empty($this->_rawParams)) {
41
            $data = array_merge($data, $this->_rawParams);
42
        }
43
44
        return $this->_convertArrayable($data);
45
    }
46
47
    /**
48
     * Cast objects to arrays.
49
     *
50
     * @param array $array
51
     *
52
     * @return array
53
     */
54
    protected function _convertArrayable(array $array)
55
    {
56
        $arr = [];
57
58
        foreach ($array as $key => $value) {
59
            if ($value instanceof ArrayableInterface) {
60
                $arr[$value instanceof NameableInterface ? $value->getName() : $key] = $value->toArray();
61
            } elseif (is_array($value)) {
62
                $arr[$key] = $this->_convertArrayable($value);
63
            } else {
64
                $arr[$key] = $value;
65
            }
66
        }
67
68
        return $arr;
69
    }
70
71
    /**
72
     * Param's name
73
     * Picks the last part of the class name and makes it snake_case
74
     * You can override this method if you want to change the name.
75
     *
76
     * @return string name
77
     */
78
    protected function _getBaseName()
79
    {
80
        return Util::getParamName($this);
81
    }
82
83
    /**
84
     * Sets params not inside params array.
85
     *
86
     * @param string $key
87
     * @param mixed  $value
88
     *
89
     * @return $this
90
     */
91
    protected function _setRawParam($key, $value)
92
    {
93
        $this->_rawParams[$key] = $value;
94
95
        return $this;
96
    }
97
98
    /**
99
     * Sets (overwrites) the value at the given key.
100
     *
101
     * @param string $key   Key to set
102
     * @param mixed  $value Key Value
103
     *
104
     * @return $this
105
     */
106
    public function setParam($key, $value)
107
    {
108
        $this->_params[$key] = $value;
109
110
        return $this;
111
    }
112
113
    /**
114
     * Sets (overwrites) all params of this object.
115
     *
116
     * @param array $params Parameter list
117
     *
118
     * @return $this
119
     */
120
    public function setParams(array $params)
121
    {
122
        $this->_params = $params;
123
124
        return $this;
125
    }
126
127
    /**
128
     * Adds a param to the list.
129
     *
130
     * This function can be used to add an array of params
131
     *
132
     * @param string $key   Param key
133
     * @param mixed  $value Value to set
134
     *
135
     * @return $this
136
     */
137
    public function addParam($key, $value)
138
    {
139
        if ($key != null) {
140
            $this->_params[$key][] = $value;
141
        } else {
142
            $this->_params = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type * is incompatible with the declared type array of property $_params.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
143
        }
144
145
        return $this;
146
    }
147
148
    /**
149
     * Returns a specific param.
150
     *
151
     * @param string $key Key to return
152
     *
153
     * @throws \Elastica\Exception\InvalidException If requested key is not set
154
     *
155
     * @return mixed Key value
156
     */
157
    public function getParam($key)
158
    {
159
        if (!$this->hasParam($key)) {
160
            throw new InvalidException('Param '.$key.' does not exist');
161
        }
162
163
        return $this->_params[$key];
164
    }
165
166
    /**
167
     * Test if a param is set.
168
     *
169
     * @param string $key Key to test
170
     *
171
     * @return bool True if the param is set, false otherwise
172
     */
173
    public function hasParam($key)
174
    {
175
        return isset($this->_params[$key]);
176
    }
177
178
    /**
179
     * Returns the params array.
180
     *
181
     * @return array Params
182
     */
183
    public function getParams()
184
    {
185
        return $this->_params;
186
    }
187
}
188