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