array.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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.06.03
  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. * The callback function takes three parameters: <ul>
  126. * <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
  127. * <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
  128. * <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
  129. *
  130. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  131. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  132. * @return CFArray The original <CFArray> object.
  133. */
  134. public function each($callback, &$bind = null)
  135. {
  136. $items = $this->getArrayCopy();
  137. foreach ($items as $key => &$item)
  138. {
  139. $callback($item, $key, $bind);
  140. }
  141. return $this;
  142. }
  143. /**
  144. * Passes each element in the current <CFArray> object through a function, and produces a new <CFArray> object containing the return values.
  145. *
  146. * The callback function takes three parameters: <ul>
  147. * <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
  148. * <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
  149. * <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
  150. *
  151. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  152. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  153. * @return CFArray A new <CFArray> object containing the return values.
  154. */
  155. public function map($callback, &$bind = null)
  156. {
  157. $items = $this->getArrayCopy();
  158. $collect = array();
  159. foreach ($items as $key => &$item)
  160. {
  161. $collect[] = $callback($item, $key, $bind);
  162. }
  163. return new CFArray($collect);
  164. }
  165. /**
  166. * 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`.
  167. *
  168. * The callback function takes three parameters: <ul>
  169. * <li><code>$item</code> - <code>mixed</code> - Optional - The individual node in the array.</li>
  170. * <li><code>$key</code> - <code>mixed</code> - Optional - The key for the array node.</li>
  171. * <li><code>$bind</code> - <code>mixed</code> - Optional - The variable that was passed into the $bind parameter.</li></ul>
  172. *
  173. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  174. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  175. * @return CFArray A new <CFArray> object containing the return values.
  176. */
  177. public function filter($callback, &$bind = null)
  178. {
  179. $items = $this->getArrayCopy();
  180. $collect = array();
  181. foreach ($items as $key => &$item)
  182. {
  183. if ($callback($item, $key, $bind) !== false)
  184. {
  185. $collect[] = $item;
  186. }
  187. }
  188. return new CFArray($collect);
  189. }
  190. /**
  191. * Alias for <filter()>. This functionality was incorrectly named _reduce_ in earlier versions of the SDK.
  192. *
  193. * @param string|function $callback (Required) The callback function to execute. PHP 5.3 or newer can use an anonymous function.
  194. * @param mixed $bind (Optional) A variable from the calling scope to pass-by-reference into the local scope of the callback function.
  195. * @return CFArray A new <CFArray> object containing the return values.
  196. */
  197. public function reduce($callback, &$bind = null)
  198. {
  199. return $this->filter($callback, $bind);
  200. }
  201. /*%******************************************************************************************%*/
  202. // TRAVERSAL
  203. /**
  204. * Gets the first result in the array.
  205. *
  206. * @return mixed The first result in the <CFArray> object. Returns `false` if there are no items in the array.
  207. */
  208. public function first()
  209. {
  210. $items = $this->getArrayCopy();
  211. return count($items) ? $items[0] : false;
  212. }
  213. /**
  214. * Gets the last result in the array.
  215. *
  216. * @return mixed The last result in the <CFArray> object. Returns `false` if there are no items in the array.
  217. */
  218. public function last()
  219. {
  220. $items = $this->getArrayCopy();
  221. return count($items) ? end($items) : false;
  222. }
  223. /**
  224. * Removes all `null` values from an array.
  225. *
  226. * @return CFArray A new <CFArray> object containing the non-null values.
  227. */
  228. public function compress()
  229. {
  230. return new CFArray(array_filter($this->getArrayCopy()));
  231. }
  232. /**
  233. * Reindexes the array, starting from zero.
  234. *
  235. * @return CFArray A new <CFArray> object with indexes starting at zero.
  236. */
  237. public function reindex()
  238. {
  239. return new CFArray(array_values($this->getArrayCopy()));
  240. }
  241. /*%******************************************************************************************%*/
  242. // ALTERNATE FORMATS
  243. /**
  244. * Gets the current XML node as a JSON string.
  245. *
  246. * @return string The current XML node as a JSON string.
  247. */
  248. public function to_json()
  249. {
  250. return json_encode($this->getArrayCopy());
  251. }
  252. /**
  253. * Gets the current XML node as a YAML string.
  254. *
  255. * @return string The current XML node as a YAML string.
  256. */
  257. public function to_yaml()
  258. {
  259. return sfYaml::dump($this->getArrayCopy(), 5);
  260. }
  261. }