| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* Copyright 2010-2012 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
|
4 |
*
|
|
|
5 |
* Licensed under the Apache License, Version 2.0 (the "License").
|
|
|
6 |
* You may not use this file except in compliance with the License.
|
|
|
7 |
* A copy of the License is located at
|
|
|
8 |
*
|
|
|
9 |
* http://aws.amazon.com/apache2.0
|
|
|
10 |
*
|
|
|
11 |
* or in the "license" file accompanying this file. This file is distributed
|
|
|
12 |
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
|
|
|
13 |
* express or implied. See the License for the specific language governing
|
|
|
14 |
* permissions and limitations under the License.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
|
|
|
18 |
/*%******************************************************************************************%*/
|
|
|
19 |
// CLASS
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* The <CFArray> object extends PHP's built-in <php:ArrayObject> object by providing convenience methods for
|
|
|
23 |
* rapidly manipulating array data. Specifically, the `CFArray` object is intended for working with
|
|
|
24 |
* <CFResponse> and <CFSimpleXML> objects that are returned by AWS services.
|
|
|
25 |
*
|
|
|
26 |
* @version 2012.01.17
|
|
|
27 |
* @license See the included NOTICE.md file for more information.
|
|
|
28 |
* @copyright See the included NOTICE.md file for more information.
|
|
|
29 |
* @link http://aws.amazon.com/php/ PHP Developer Center
|
|
|
30 |
* @link http://php.net/ArrayObject ArrayObject
|
|
|
31 |
*/
|
|
|
32 |
class CFArray extends ArrayObject
|
|
|
33 |
{
|
|
|
34 |
/**
|
|
|
35 |
* Constructs a new instance of <CFArray>.
|
|
|
36 |
*
|
|
|
37 |
* @param mixed $input (Optional) The input parameter accepts an array or an Object. The default value is an empty array.
|
|
|
38 |
* @param integer $flags (Optional) Flags to control the behavior of the ArrayObject object. Defaults to <STD_PROP_LIST>.
|
|
|
39 |
* @param string $iterator_class (Optional) Specify the class that will be used for iteration of the <php:ArrayObject> object. <php:ArrayIterator> is the default class used.
|
|
|
40 |
* @return mixed Either an array of matches, or a single <CFSimpleXML> element.
|
|
|
41 |
*/
|
|
|
42 |
public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iterator_class = 'ArrayIterator')
|
|
|
43 |
{
|
|
|
44 |
// Provide a default value
|
|
|
45 |
$input = $input ? $input : array();
|
|
|
46 |
|
|
|
47 |
try {
|
|
|
48 |
return parent::__construct($input, $flags, $iterator_class);
|
|
|
49 |
}
|
|
|
50 |
catch (InvalidArgumentException $e)
|
|
|
51 |
{
|
|
|
52 |
throw new CFArray_Exception($e->getMessage());
|
|
|
53 |
}
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* Alternate approach to constructing a new instance. Supports chaining.
|
|
|
58 |
*
|
|
|
59 |
* @param mixed $input (Optional) The input parameter accepts an array or an Object. The default value is an empty array.
|
|
|
60 |
* @param integer $flags (Optional) Flags to control the behavior of the ArrayObject object. Defaults to <STD_PROP_LIST>.
|
|
|
61 |
* @param string $iterator_class (Optional) Specify the class that will be used for iteration of the <php:ArrayObject> object. <php:ArrayIterator> is the default class used.
|
|
|
62 |
* @return mixed Either an array of matches, or a single <CFSimpleXML> element.
|
|
|
63 |
*/
|
|
|
64 |
public static function init($input = array(), $flags = self::STD_PROP_LIST, $iterator_class = 'ArrayIterator')
|
|
|
65 |
{
|
|
|
66 |
if (version_compare(PHP_VERSION, '5.3.0', '<'))
|
|
|
67 |
{
|
|
|
68 |
throw new Exception('PHP 5.3 or newer is required to instantiate a new class with CLASS::init().');
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
$self = get_called_class();
|
|
|
72 |
return new $self($input, $flags, $iterator_class);
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
/**
|
|
|
76 |
* Handles how the object is rendered when cast as a string.
|
|
|
77 |
*
|
|
|
78 |
* @return string The word "Array".
|
|
|
79 |
*/
|
|
|
80 |
public function __toString()
|
|
|
81 |
{
|
|
|
82 |
return 'Array';
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
|
|
|
86 |
/*%******************************************************************************************%*/
|
|
|
87 |
// REFORMATTING
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Maps each element in the <CFArray> object as an integer.
|
|
|
91 |
*
|
|
|
92 |
* @return array The contents of the <CFArray> object mapped as integers.
|
|
|
93 |
*/
|
|
|
94 |
public function map_integer()
|
|
|
95 |
{
|
|
|
96 |
return array_map('intval', $this->getArrayCopy());
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/**
|
|
|
100 |
* Maps each element in the CFArray object as a string.
|
|
|
101 |
*
|
|
|
102 |
* @param string $pcre (Optional) A Perl-Compatible Regular Expression (PCRE) to filter the names against.
|
|
|
103 |
* @return array The contents of the <CFArray> object mapped as strings. If there are no results, the method will return an empty array.
|
|
|
104 |
*/
|
|
|
105 |
public function map_string($pcre = null)
|
|
|
106 |
{
|
|
|
107 |
$list = array_map('strval', $this->getArrayCopy());
|
|
|
108 |
$dlist = array();
|
|
|
109 |
|
|
|
110 |
if ($pcre)
|
|
|
111 |
{
|
|
|
112 |
foreach ($list as $item)
|
|
|
113 |
{
|
|
|
114 |
$dlist[] = preg_match($pcre, $item) ? $item : null;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
$list = array_values(array_filter($dlist));
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return $list;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
/*%******************************************************************************************%*/
|
|
|
125 |
// CONFIRMATION
|
|
|
126 |
|
|
|
127 |
/**
|
|
|
128 |
* Verifies that _all_ responses were successful. A single failed request will cause <areOK()> to return false. Equivalent to <CFResponse::isOK()>, except it applies to all responses.
|
|
|
129 |
*
|
|
|
130 |
* @return boolean Whether _all_ requests were successful or not.
|
|
|
131 |
*/
|
|
|
132 |
public function areOK()
|
|
|
133 |
{
|
|
|
134 |
$dlist = array();
|
|
|
135 |
$list = $this->getArrayCopy();
|
|
|
136 |
|
|
|
137 |
foreach ($list as $response)
|
|
|
138 |
{
|
|
|
139 |
if ($response instanceof CFResponse)
|
|
|
140 |
{
|
|
|
141 |
$dlist[] = $response->isOK();
|
|
|
142 |
}
|
|
|
143 |
}
|
|
|
144 |
|
|
|
145 |
return (array_search(false, $dlist, true) !== false) ? false : true;
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
|
|
|
149 |
/*%******************************************************************************************%*/
|
|
|
150 |
// ITERATING AND EXECUTING
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
* Iterates over a <CFArray> object, and executes a function for each matched element.
|
|
|
154 |
*
|
|
|
155 |
* The callback function takes three parameters: <ul>
|
|
|
156 |
* <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
|
|
|
157 |
* <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
|
|
|
158 |
* <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
|
|
|
159 |
*
|
|
|
160 |
* @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
|
|
|
161 |
* @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
|
|
|
162 |
* @return CFArray The original <CFArray> object.
|
|
|
163 |
*/
|
|
|
164 |
public function each($callback, &$bind = null)
|
|
|
165 |
{
|
|
|
166 |
$items = $this->getArrayCopy();
|
|
|
167 |
|
|
|
168 |
foreach ($items as $key => &$item)
|
|
|
169 |
{
|
|
|
170 |
$callback($item, $key, $bind);
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
return $this;
|
|
|
174 |
}
|
|
|
175 |
|
|
|
176 |
/**
|
|
|
177 |
* Passes each element in the current <CFArray> object through a function, and produces a new <CFArray> object containing the return values.
|
|
|
178 |
*
|
|
|
179 |
* The callback function takes three parameters: <ul>
|
|
|
180 |
* <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
|
|
|
181 |
* <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
|
|
|
182 |
* <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
|
|
|
183 |
*
|
|
|
184 |
* @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
|
|
|
185 |
* @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
|
|
|
186 |
* @return CFArray A new <CFArray> object containing the return values.
|
|
|
187 |
*/
|
|
|
188 |
public function map($callback, &$bind = null)
|
|
|
189 |
{
|
|
|
190 |
$items = $this->getArrayCopy();
|
|
|
191 |
$collect = array();
|
|
|
192 |
|
|
|
193 |
foreach ($items as $key => &$item)
|
|
|
194 |
{
|
|
|
195 |
$collect[] = $callback($item, $key, $bind);
|
|
|
196 |
}
|
|
|
197 |
|
|
|
198 |
return new CFArray($collect);
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Filters the list of nodes by passing each value in the current <CFArray> object through a function. The node will be removed if the function returns `false`.
|
|
|
203 |
*
|
|
|
204 |
* The callback function takes three parameters: <ul>
|
|
|
205 |
* <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
|
|
|
206 |
* <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
|
|
|
207 |
* <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
|
|
|
208 |
*
|
|
|
209 |
* @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
|
|
|
210 |
* @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
|
|
|
211 |
* @return CFArray A new <CFArray> object containing the return values.
|
|
|
212 |
*/
|
|
|
213 |
public function filter($callback, &$bind = null)
|
|
|
214 |
{
|
|
|
215 |
$items = $this->getArrayCopy();
|
|
|
216 |
$collect = array();
|
|
|
217 |
|
|
|
218 |
foreach ($items as $key => &$item)
|
|
|
219 |
{
|
|
|
220 |
if ($callback($item, $key, $bind) !== false)
|
|
|
221 |
{
|
|
|
222 |
$collect[] = $item;
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
return new CFArray($collect);
|
|
|
227 |
}
|
|
|
228 |
|
|
|
229 |
/**
|
|
|
230 |
* Alias for <filter()>. This functionality was incorrectly named _reduce_ in earlier versions of the SDK.
|
|
|
231 |
*
|
|
|
232 |
* @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
|
|
|
233 |
* @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
|
|
|
234 |
* @return CFArray A new <CFArray> object containing the return values.
|
|
|
235 |
*/
|
|
|
236 |
public function reduce($callback, &$bind = null)
|
|
|
237 |
{
|
|
|
238 |
return $this->filter($callback, $bind);
|
|
|
239 |
}
|
|
|
240 |
|
|
|
241 |
|
|
|
242 |
/*%******************************************************************************************%*/
|
|
|
243 |
// TRAVERSAL
|
|
|
244 |
|
|
|
245 |
/**
|
|
|
246 |
* Gets the first result in the array.
|
|
|
247 |
*
|
|
|
248 |
* @return mixed The first result in the <CFArray> object. Returns `false` if there are no items in the array.
|
|
|
249 |
*/
|
|
|
250 |
public function first()
|
|
|
251 |
{
|
|
|
252 |
$items = $this->getArrayCopy();
|
|
|
253 |
return count($items) ? $items[0] : false;
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
/**
|
|
|
257 |
* Gets the last result in the array.
|
|
|
258 |
*
|
|
|
259 |
* @return mixed The last result in the <CFArray> object. Returns `false` if there are no items in the array.
|
|
|
260 |
*/
|
|
|
261 |
public function last()
|
|
|
262 |
{
|
|
|
263 |
$items = $this->getArrayCopy();
|
|
|
264 |
return count($items) ? end($items) : false;
|
|
|
265 |
}
|
|
|
266 |
|
|
|
267 |
/**
|
|
|
268 |
* Removes all `null` values from an array.
|
|
|
269 |
*
|
|
|
270 |
* @return CFArray A new <CFArray> object containing the non-null values.
|
|
|
271 |
*/
|
|
|
272 |
public function compress()
|
|
|
273 |
{
|
|
|
274 |
return new CFArray(array_filter($this->getArrayCopy()));
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
/**
|
|
|
278 |
* Reindexes the array, starting from zero.
|
|
|
279 |
*
|
|
|
280 |
* @return CFArray A new <CFArray> object with indexes starting at zero.
|
|
|
281 |
*/
|
|
|
282 |
public function reindex()
|
|
|
283 |
{
|
|
|
284 |
return new CFArray(array_values($this->getArrayCopy()));
|
|
|
285 |
}
|
|
|
286 |
|
|
|
287 |
|
|
|
288 |
/*%******************************************************************************************%*/
|
|
|
289 |
// ALTERNATE FORMATS
|
|
|
290 |
|
|
|
291 |
/**
|
|
|
292 |
* Gets the current XML node as a JSON string.
|
|
|
293 |
*
|
|
|
294 |
* @return string The current XML node as a JSON string.
|
|
|
295 |
*/
|
|
|
296 |
public function to_json()
|
|
|
297 |
{
|
|
|
298 |
return json_encode($this->getArrayCopy());
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/**
|
|
|
302 |
* Gets the current XML node as a YAML string.
|
|
|
303 |
*
|
|
|
304 |
* @return string The current XML node as a YAML string.
|
|
|
305 |
*/
|
|
|
306 |
public function to_yaml()
|
|
|
307 |
{
|
|
|
308 |
return sfYaml::dump($this->getArrayCopy(), 5);
|
|
|
309 |
}
|
|
|
310 |
}
|
|
|
311 |
|
|
|
312 |
class CFArray_Exception extends Exception {}
|