array.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. // 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 2012.01.17
  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. // Provide a default value
  42. $input = $input ? $input : array();
  43. try {
  44. return parent::__construct($input, $flags, $iterator_class);
  45. }
  46. catch (InvalidArgumentException $e)
  47. {
  48. throw new CFArray_Exception($e->getMessage());
  49. }
  50. }
  51. /**
  52. * Alternate approach to constructing a new instance. Supports chaining.
  53. *
  54. * @param mixed $input (Optional) The input parameter accepts an array or an Object. The default value is an empty array.
  55. * @param integer $flags (Optional) Flags to control the behavior of the ArrayObject object. Defaults to <STD_PROP_LIST>.
  56. * @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.
  57. * @return mixed Either an array of matches, or a single <CFSimpleXML> element.
  58. */
  59. public static function init($input = array(), $flags = self::STD_PROP_LIST, $iterator_class = 'ArrayIterator')
  60. {
  61. if (version_compare(PHP_VERSION, '5.3.0', '<'))
  62. {
  63. throw new Exception('PHP 5.3 or newer is required to instantiate a new class with CLASS::init().');
  64. }
  65. $self = get_called_class();
  66. return new $self($input, $flags, $iterator_class);
  67. }
  68. /**
  69. * Handles how the object is rendered when cast as a string.
  70. *
  71. * @return string The word "Array".
  72. */
  73. public function __toString()
  74. {
  75. return 'Array';
  76. }
  77. /*%******************************************************************************************%*/
  78. // REFORMATTING
  79. /**
  80. * Maps each element in the <CFArray> object as an integer.
  81. *
  82. * @return array The contents of the <CFArray> object mapped as integers.
  83. */
  84. public function map_integer()
  85. {
  86. return array_map('intval', $this->getArrayCopy());
  87. }
  88. /**
  89. * Maps each element in the CFArray object as a string.
  90. *
  91. * @param string $pcre (Optional) A Perl-Compatible Regular Expression (PCRE) to filter the names against.
  92. * @return array The contents of the <CFArray> object mapped as strings. If there are no results, the method will return an empty array.
  93. */
  94. public function map_string($pcre = null)
  95. {
  96. $list = array_map('strval', $this->getArrayCopy());
  97. $dlist = array();
  98. if ($pcre)
  99. {
  100. foreach ($list as $item)
  101. {
  102. $dlist[] = preg_match($pcre, $item) ? $item : null;
  103. }
  104. $list = array_values(array_filter($dlist));
  105. }
  106. return $list;
  107. }
  108. /*%******************************************************************************************%*/
  109. // CONFIRMATION
  110. /**
  111. * 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.
  112. *
  113. * @return boolean Whether _all_ requests were successful or not.
  114. */
  115. public function areOK()
  116. {
  117. $dlist = array();
  118. $list = $this->getArrayCopy();
  119. foreach ($list as $response)
  120. {
  121. if ($response instanceof CFResponse)
  122. {
  123. $dlist[] = $response->isOK();
  124. }
  125. }
  126. return (array_search(false, $dlist, true) !== false) ? false : true;
  127. }
  128. /*%******************************************************************************************%*/
  129. // ITERATING AND EXECUTING
  130. /**
  131. * Iterates over a <CFArray> object, and executes a function for each matched element.
  132. *
  133. * The callback function takes three parameters: <ul>
  134. * <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
  135. * <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
  136. * <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
  137. *
  138. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  139. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  140. * @return CFArray The original <CFArray> object.
  141. */
  142. public function each($callback, &$bind = null)
  143. {
  144. $items = $this->getArrayCopy();
  145. foreach ($items as $key => &$item)
  146. {
  147. $callback($item, $key, $bind);
  148. }
  149. return $this;
  150. }
  151. /**
  152. * Passes each element in the current <CFArray> object through a function, and produces a new <CFArray> object containing the return values.
  153. *
  154. * The callback function takes three parameters: <ul>
  155. * <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
  156. * <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
  157. * <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
  158. *
  159. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  160. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  161. * @return CFArray A new <CFArray> object containing the return values.
  162. */
  163. public function map($callback, &$bind = null)
  164. {
  165. $items = $this->getArrayCopy();
  166. $collect = array();
  167. foreach ($items as $key => &$item)
  168. {
  169. $collect[] = $callback($item, $key, $bind);
  170. }
  171. return new CFArray($collect);
  172. }
  173. /**
  174. * 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`.
  175. *
  176. * The callback function takes three parameters: <ul>
  177. * <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
  178. * <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
  179. * <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
  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 filter($callback, &$bind = null)
  186. {
  187. $items = $this->getArrayCopy();
  188. $collect = array();
  189. foreach ($items as $key => &$item)
  190. {
  191. if ($callback($item, $key, $bind) !== false)
  192. {
  193. $collect[] = $item;
  194. }
  195. }
  196. return new CFArray($collect);
  197. }
  198. /**
  199. * Alias for <filter()>. This functionality was incorrectly named _reduce_ in earlier versions of the SDK.
  200. *
  201. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  202. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  203. * @return CFArray A new <CFArray> object containing the return values.
  204. */
  205. public function reduce($callback, &$bind = null)
  206. {
  207. return $this->filter($callback, $bind);
  208. }
  209. /*%******************************************************************************************%*/
  210. // TRAVERSAL
  211. /**
  212. * Gets the first result in the array.
  213. *
  214. * @return mixed The first result in the <CFArray> object. Returns `false` if there are no items in the array.
  215. */
  216. public function first()
  217. {
  218. $items = $this->getArrayCopy();
  219. return count($items) ? $items[0] : false;
  220. }
  221. /**
  222. * Gets the last result in the array.
  223. *
  224. * @return mixed The last result in the <CFArray> object. Returns `false` if there are no items in the array.
  225. */
  226. public function last()
  227. {
  228. $items = $this->getArrayCopy();
  229. return count($items) ? end($items) : false;
  230. }
  231. /**
  232. * Removes all `null` values from an array.
  233. *
  234. * @return CFArray A new <CFArray> object containing the non-null values.
  235. */
  236. public function compress()
  237. {
  238. return new CFArray(array_filter($this->getArrayCopy()));
  239. }
  240. /**
  241. * Reindexes the array, starting from zero.
  242. *
  243. * @return CFArray A new <CFArray> object with indexes starting at zero.
  244. */
  245. public function reindex()
  246. {
  247. return new CFArray(array_values($this->getArrayCopy()));
  248. }
  249. /*%******************************************************************************************%*/
  250. // ALTERNATE FORMATS
  251. /**
  252. * Gets the current XML node as a JSON string.
  253. *
  254. * @return string The current XML node as a JSON string.
  255. */
  256. public function to_json()
  257. {
  258. return json_encode($this->getArrayCopy());
  259. }
  260. /**
  261. * Gets the current XML node as a YAML string.
  262. *
  263. * @return string The current XML node as a YAML string.
  264. */
  265. public function to_yaml()
  266. {
  267. return sfYaml::dump($this->getArrayCopy(), 5);
  268. }
  269. }
  270. class CFArray_Exception extends Exception {}