PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libraries/external/aws/utilities/array.class.php

https://github.com/geraintp/frontend
PHP | 289 lines | 117 code | 40 blank | 132 comment | 7 complexity | e0751b9f3fb5a52fbce7ef87c9ec66c3 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2010-2011 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. // CLASS
  18. /**
  19. * The <CFArray> object extends PHP's built-in <php:ArrayObject> object by providing convenience methods for
  20. * rapidly manipulating array data. Specifically, the `CFArray` object is intended for working with
  21. * <CFResponse> and <CFSimpleXML> objects that are returned by AWS services.
  22. *
  23. * @version 2011.04.25
  24. * @license See the included NOTICE.md file for more information.
  25. * @copyright See the included NOTICE.md file for more information.
  26. * @link http://aws.amazon.com/php/ PHP Developer Center
  27. * @link http://php.net/ArrayObject ArrayObject
  28. */
  29. class CFArray extends ArrayObject
  30. {
  31. /**
  32. * Constructs a new instance of <CFArray>.
  33. *
  34. * @param mixed $input (Optional) The input parameter accepts an array or an Object. The default value is an empty array.
  35. * @param integer $flags (Optional) Flags to control the behavior of the ArrayObject object. Defaults to <STD_PROP_LIST>.
  36. * @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.
  37. * @return mixed Either an array of matches, or a single <CFSimpleXML> element.
  38. */
  39. public function __construct($input = array(), $flags = self::STD_PROP_LIST, $iterator_class = 'ArrayIterator')
  40. {
  41. return parent::__construct($input, $flags, $iterator_class);
  42. }
  43. /**
  44. * Alternate approach to constructing a new instance. Supports chaining.
  45. *
  46. * @param mixed $input (Optional) The input parameter accepts an array or an Object. The default value is an empty array.
  47. * @param integer $flags (Optional) Flags to control the behavior of the ArrayObject object. Defaults to <STD_PROP_LIST>.
  48. * @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.
  49. * @return mixed Either an array of matches, or a single <CFSimpleXML> element.
  50. */
  51. public static function init($input = array(), $flags = self::STD_PROP_LIST, $iterator_class = 'ArrayIterator')
  52. {
  53. if (version_compare(PHP_VERSION, '5.3.0', '<'))
  54. {
  55. throw new Exception('PHP 5.3 or newer is required to instantiate a new class with CLASS::init().');
  56. }
  57. $self = get_called_class();
  58. return new $self($input, $flags, $iterator_class);
  59. }
  60. /**
  61. * Handles how the object is rendered when cast as a string.
  62. *
  63. * @return string The word "Array".
  64. */
  65. public function __toString()
  66. {
  67. return 'Array';
  68. }
  69. /*%******************************************************************************************%*/
  70. // REFORMATTING
  71. /**
  72. * Maps each element in the <CFArray> object as an integer.
  73. *
  74. * @return array The contents of the <CFArray> object mapped as integers.
  75. */
  76. public function map_integer()
  77. {
  78. return array_map('intval', $this->getArrayCopy());
  79. }
  80. /**
  81. * Maps each element in the CFArray object as a string.
  82. *
  83. * @param string $pcre (Optional) A Perl-Compatible Regular Expression (PCRE) to filter the names against.
  84. * @return array The contents of the <CFArray> object mapped as strings. If there are no results, the method will return an empty array.
  85. */
  86. public function map_string($pcre = null)
  87. {
  88. $list = array_map('strval', $this->getArrayCopy());
  89. $dlist = array();
  90. if ($pcre)
  91. {
  92. foreach ($list as $item)
  93. {
  94. $dlist[] = preg_match($pcre, $item) ? $item : null;
  95. }
  96. $list = array_values(array_filter($dlist));
  97. }
  98. return $list;
  99. }
  100. /*%******************************************************************************************%*/
  101. // CONFIRMATION
  102. /**
  103. * 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.
  104. *
  105. * @return boolean Whether _all_ requests were successful or not.
  106. */
  107. public function areOK()
  108. {
  109. $dlist = array();
  110. $list = $this->getArrayCopy();
  111. foreach ($list as $response)
  112. {
  113. if ($response instanceof CFResponse)
  114. {
  115. $dlist[] = $response->isOK();
  116. }
  117. }
  118. return (array_search(false, $dlist, true) !== false) ? false : true;
  119. }
  120. /*%******************************************************************************************%*/
  121. // ITERATING AND EXECUTING
  122. /**
  123. * Iterates over a <CFArray> object, and executes a function for each matched element.
  124. *
  125. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  126. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  127. * @return CFArray The original <CFArray> object.
  128. */
  129. public function each($callback, &$bind = null)
  130. {
  131. $items = $this->getArrayCopy();
  132. $max = count($items);
  133. for ($i = 0; $i < $max; $i++)
  134. {
  135. $callback($items[$i], $i, $bind);
  136. }
  137. return $this;
  138. }
  139. /**
  140. * Passes each element in the current <CFArray> object through a function, and produces a new <CFArray> object containing the return values.
  141. *
  142. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  143. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  144. * @return CFArray A new <CFArray> object containing the return values.
  145. */
  146. public function map($callback, &$bind = null)
  147. {
  148. $items = $this->getArrayCopy();
  149. $max = count($items);
  150. $collect = array();
  151. for ($i = 0; $i < $max; $i++)
  152. {
  153. $collect[] = $callback($items[$i], $i, $bind);
  154. }
  155. return new CFArray($collect);
  156. }
  157. /**
  158. * 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`.
  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 A new <CFArray> object containing the return values.
  163. */
  164. public function filter($callback, &$bind = null)
  165. {
  166. $items = $this->getArrayCopy();
  167. $max = count($items);
  168. $collect = array();
  169. for ($i = 0; $i < $max; $i++)
  170. {
  171. if ($callback($items[$i], $i, $bind) !== false)
  172. {
  173. $collect[] = $items[$i];
  174. }
  175. }
  176. return new CFArray($collect);
  177. }
  178. /**
  179. * Alias for <filter()>. This functionality was incorrectly named _reduce_ in earlier versions of the SDK.
  180. *
  181. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  182. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  183. * @return CFArray A new <CFArray> object containing the return values.
  184. */
  185. public function reduce($callback, &$bind = null)
  186. {
  187. return $this->filter($callback, $bind);
  188. }
  189. /*%******************************************************************************************%*/
  190. // TRAVERSAL
  191. /**
  192. * Gets the first result in the array.
  193. *
  194. * @return mixed The first result in the <CFArray> object. Returns `false` if there are no items in the array.
  195. */
  196. public function first()
  197. {
  198. $items = $this->getArrayCopy();
  199. return count($items) ? $items[0] : false;
  200. }
  201. /**
  202. * Gets the last result in the array.
  203. *
  204. * @return mixed The last result in the <CFArray> object. Returns `false` if there are no items in the array.
  205. */
  206. public function last()
  207. {
  208. $items = $this->getArrayCopy();
  209. return count($items) ? end($items) : false;
  210. }
  211. /**
  212. * Removes all `null` values from an array.
  213. *
  214. * @return CFArray A new <CFArray> object containing the non-null values.
  215. */
  216. public function compress()
  217. {
  218. return new CFArray(array_filter($this->getArrayCopy()));
  219. }
  220. /**
  221. * Reindexes the array, starting from zero.
  222. *
  223. * @return CFArray A new <CFArray> object with indexes starting at zero.
  224. */
  225. public function reindex()
  226. {
  227. return new CFArray(array_values($this->getArrayCopy()));
  228. }
  229. /*%******************************************************************************************%*/
  230. // ALTERNATE FORMATS
  231. /**
  232. * Gets the current XML node as a JSON string.
  233. *
  234. * @return string The current XML node as a JSON string.
  235. */
  236. public function to_json()
  237. {
  238. return json_encode($this->getArrayCopy());
  239. }
  240. /**
  241. * Gets the current XML node as a YAML string.
  242. *
  243. * @return string The current XML node as a YAML string.
  244. */
  245. public function to_yaml()
  246. {
  247. return sfYaml::dump($this->getArrayCopy(), 5);
  248. }
  249. }