1 : <?php
2 :
3 : /**
4 : * This file is part of the Symfony package.
5 : *
6 : * (c) Fabien Potencier <fabien@symfony.com>
7 : *
8 : * LICENSE:
9 : *
10 : * Copyright (c) 2004-2011 Fabien Potencier
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included in
20 : * all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 : * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE
29 : */
30 :
31 : namespace NoiseLabs\ToolKit\GoogleAPI;
32 :
33 : /**
34 : * ParameterBag is a container for key/value pairs.
35 : *
36 : * @author Fabien Potencier <fabien@symfony.com>
37 : */
38 : class ParameterBag
39 0 : {
40 : protected $parameters;
41 :
42 : /**
43 : * Constructor.
44 : *
45 : * @param array $parameters An array of parameters
46 : */
47 : public function __construct(array $parameters = array())
48 : {
49 0 : $this->parameters = $parameters;
50 0 : }
51 :
52 : /**
53 : * Returns the parameters.
54 : *
55 : * @return array An array of parameters
56 : */
57 : public function all()
58 : {
59 0 : return $this->parameters;
60 : }
61 :
62 : /**
63 : * Returns the parameter keys.
64 : *
65 : * @return array An array of parameter keys
66 : */
67 : public function keys()
68 : {
69 0 : return array_keys($this->parameters);
70 : }
71 :
72 : /**
73 : * Replaces the current parameters by a new set.
74 : *
75 : * @param array $parameters An array of parameters
76 : */
77 : public function replace(array $parameters = array())
78 : {
79 0 : $this->parameters = $parameters;
80 0 : }
81 :
82 : /**
83 : * Adds parameters.
84 : *
85 : * @param array $parameters An array of parameters
86 : */
87 : public function add(array $parameters = array())
88 : {
89 0 : $this->parameters = array_replace($this->parameters, $parameters);
90 0 : }
91 :
92 : /**
93 : * Returns a parameter by name.
94 : *
95 : * @param string $path The key
96 : * @param mixed $default The default value
97 : * @param boolean $deep
98 : */
99 : public function get($path, $default = null, $deep = false)
100 : {
101 0 : if (!$deep || false === $pos = strpos($path, '[')) {
102 0 : return array_key_exists($path, $this->parameters) ? $this->parameters[$path] : $default;
103 : }
104 :
105 0 : $root = substr($path, 0, $pos);
106 0 : if (!array_key_exists($root, $this->parameters)) {
107 0 : return $default;
108 : }
109 :
110 0 : $value = $this->parameters[$root];
111 0 : $currentKey = null;
112 0 : for ($i=$pos,$c=strlen($path); $i<$c; $i++) {
113 0 : $char = $path[$i];
114 :
115 0 : if ('[' === $char) {
116 0 : if (null !== $currentKey) {
117 0 : throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "[" at position %d.', $i));
118 : }
119 :
120 0 : $currentKey = '';
121 0 : } else if (']' === $char) {
122 0 : if (null === $currentKey) {
123 0 : throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "]" at position %d.', $i));
124 : }
125 :
126 0 : if (!is_array($value) || !array_key_exists($currentKey, $value)) {
127 0 : return $default;
128 : }
129 :
130 0 : $value = $value[$currentKey];
131 0 : $currentKey = null;
132 0 : } else {
133 0 : if (null === $currentKey) {
134 0 : throw new \InvalidArgumentException(sprintf('Malformed path. Unexpected "%s" at position %d.', $char, $i));
135 : }
136 :
137 0 : $currentKey .= $char;
138 : }
139 0 : }
140 :
141 0 : if (null !== $currentKey) {
142 0 : throw new \InvalidArgumentException(sprintf('Malformed path. Path must end with "]".'));
143 : }
144 :
145 0 : return $value;
146 : }
147 :
148 : /**
149 : * Sets a parameter by name.
150 : *
151 : * @param string $key The key
152 : * @param mixed $value The value
153 : */
154 : public function set($key, $value)
155 : {
156 0 : $this->parameters[$key] = $value;
157 0 : }
158 :
159 : /**
160 : * Returns true if the parameter is defined.
161 : *
162 : * @param string $key The key
163 : *
164 : * @return Boolean true if the parameter exists, false otherwise
165 : */
166 : public function has($key)
167 : {
168 0 : return array_key_exists($key, $this->parameters);
169 : }
170 :
171 : /**
172 : * Removes a parameter.
173 : *
174 : * @param string $key The key
175 : */
176 : public function remove($key)
177 : {
178 0 : unset($this->parameters[$key]);
179 0 : }
180 :
181 : /**
182 : * Returns the alphabetic characters of the parameter value.
183 : *
184 : * @param string $key The parameter key
185 : * @param mixed $default The default value
186 : * @param boolean $deep
187 : *
188 : * @return string The filtered value
189 : */
190 : public function getAlpha($key, $default = '', $deep = false)
191 : {
192 0 : return preg_replace('/[^[:alpha:]]/', '', $this->get($key, $default, $deep));
193 : }
194 :
195 : /**
196 : * Returns the alphabetic characters and digits of the parameter value.
197 : *
198 : * @param string $key The parameter key
199 : * @param mixed $default The default value
200 : * @param boolean $deep
201 : *
202 : * @return string The filtered value
203 : */
204 : public function getAlnum($key, $default = '', $deep = false)
205 : {
206 0 : return preg_replace('/[^[:alnum:]]/', '', $this->get($key, $default, $deep));
207 : }
208 :
209 : /**
210 : * Returns the digits of the parameter value.
211 : *
212 : * @param string $key The parameter key
213 : * @param mixed $default The default value
214 : * @param boolean $deep
215 : *
216 : * @return string The filtered value
217 : */
218 : public function getDigits($key, $default = '', $deep = false)
219 : {
220 0 : return preg_replace('/[^[:digit:]]/', '', $this->get($key, $default, $deep));
221 : }
222 :
223 : /**
224 : * Returns the parameter value converted to integer.
225 : *
226 : * @param string $key The parameter key
227 : * @param mixed $default The default value
228 : * @param boolean $deep
229 : *
230 : * @return string The filtered value
231 : */
232 : public function getInt($key, $default = 0, $deep = false)
233 : {
234 0 : return (int) $this->get($key, $default, $deep);
235 : }
236 : }
|