Completed
Push — master ( b2c37d...136e36 )
by Nicolas
02:36
created

Param::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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, \Countable
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
        $this->_params[$key][] = $value;
140
141
        return $this;
142
    }
143
144
    /**
145
     * Returns a specific param.
146
     *
147
     * @param string $key Key to return
148
     *
149
     * @throws \Elastica\Exception\InvalidException If requested key is not set
150
     *
151
     * @return mixed Key value
152
     */
153
    public function getParam($key)
154
    {
155
        if (!$this->hasParam($key)) {
156
            throw new InvalidException('Param '.$key.' does not exist');
157
        }
158
159
        return $this->_params[$key];
160
    }
161
162
    /**
163
     * Test if a param is set.
164
     *
165
     * @param string $key Key to test
166
     *
167
     * @return bool True if the param is set, false otherwise
168
     */
169
    public function hasParam($key)
170
    {
171
        return isset($this->_params[$key]);
172
    }
173
174
    /**
175
     * Returns the params array.
176
     *
177
     * @return array Params
178
     */
179
    public function getParams()
180
    {
181
        return $this->_params;
182
    }
183
184
    /**
185
     * @inheritdoc
186
     *
187
     * @return int
188
     */
189
    public function count()
190
    {
191
        return count($this->_params);
192
    }
193
}
194