/craft/app/helpers/ArrayHelper.php

https://gitlab.com/madebycloud/derekman · PHP · 215 lines · 115 code · 22 blank · 78 comment · 14 complexity · a521fe6f4a2e39e022345279ea2c4567 MD5 · raw file

  1. <?php
  2. namespace Craft;
  3. /**
  4. * Class ArrayHelper
  5. *
  6. * @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
  7. * @copyright Copyright (c) 2014, Pixel & Tonic, Inc.
  8. * @license http://buildwithcraft.com/license Craft License Agreement
  9. * @see http://buildwithcraft.com
  10. * @package craft.app.helpers
  11. * @since 1.0
  12. */
  13. class ArrayHelper
  14. {
  15. // Public Methods
  16. // =========================================================================
  17. /**
  18. * Flattens a multi-dimensional array into a single-dimensional array.
  19. *
  20. * @param $arr
  21. * @param string $prefix
  22. *
  23. * @return array
  24. */
  25. public static function flattenArray($arr, $prefix = null)
  26. {
  27. $flattened = array();
  28. foreach ($arr as $key => $value)
  29. {
  30. if ($prefix !== null)
  31. {
  32. $key = "{$prefix}[{$key}]";
  33. }
  34. if (is_array($value))
  35. {
  36. $flattened = array_merge($flattened, static::flattenArray($value, $key));
  37. }
  38. else
  39. {
  40. $flattened[$key] = $value;
  41. }
  42. }
  43. return $flattened;
  44. }
  45. /**
  46. * Expands a flattened array back into its original form
  47. *
  48. * @param array $arr
  49. *
  50. * @return array
  51. */
  52. public static function expandArray($arr)
  53. {
  54. $expanded = array();
  55. foreach ($arr as $key => $value)
  56. {
  57. // is this an array element?
  58. if (preg_match('/^(\w+)(\[.*)/', $key, $m))
  59. {
  60. $key = '$expanded["'.$m[1].'"]'.preg_replace('/\[([a-zA-Z]\w*?)\]/', "[\"$1\"]", $m[2]);
  61. eval($key.' = "'.addslashes($value).'";');
  62. }
  63. else
  64. {
  65. $expanded[$key] = $value;
  66. }
  67. }
  68. return $expanded;
  69. }
  70. /**
  71. * @param $settings
  72. *
  73. * @return array
  74. */
  75. public static function expandSettingsArray($settings)
  76. {
  77. $arr = array();
  78. foreach ($settings as $setting)
  79. {
  80. $arr[$setting->name] = $setting->value;
  81. }
  82. return static::expandArray($arr);
  83. }
  84. /**
  85. * Converts a comma-delimited string into a trimmed array, ex:
  86. *
  87. * ArrayHelper::stringToArray('one, two, three') => array('one', 'two', 'three')
  88. *
  89. * @param mixed $str The string to convert to an array
  90. *
  91. * @return array The trimmed array
  92. */
  93. public static function stringToArray($str)
  94. {
  95. if (is_array($str))
  96. {
  97. return $str;
  98. }
  99. else if ($str instanceof \ArrayObject)
  100. {
  101. return (array) $str;
  102. }
  103. else if (empty($str))
  104. {
  105. return array();
  106. }
  107. else if (is_string($str))
  108. {
  109. // Split it on the non-escaped commas
  110. $arr = preg_split('/(?<!\\\),/', $str);
  111. // Remove any of the backslashes used to escape the commas
  112. foreach ($arr as $key => $val)
  113. {
  114. // Remove leading/trailing whitespace
  115. $val = trim($val);
  116. // Remove any backslashes used to escape commas
  117. $val = str_replace('\,', ',', $val);
  118. $arr[$key] = $val;
  119. }
  120. // Remove any empty elements and reset the keys
  121. $arr = array_merge(array_filter($arr));
  122. return $arr;
  123. }
  124. else
  125. {
  126. return array($str);
  127. }
  128. }
  129. /**
  130. * Prepends or appends a value to an array.
  131. *
  132. * @param array &$arr
  133. * @param mixed $value
  134. *
  135. * @param bool $prepend
  136. */
  137. public static function prependOrAppend(&$arr, $value, $prepend)
  138. {
  139. if ($prepend)
  140. {
  141. array_unshift($arr, $value);
  142. }
  143. else
  144. {
  145. array_push($arr, $value);
  146. }
  147. }
  148. /**
  149. * Filters empty strings from an array.
  150. *
  151. * @param array $arr
  152. *
  153. * @return array
  154. */
  155. public static function filterEmptyStringsFromArray($arr)
  156. {
  157. return array_filter($arr, array('\Craft\ArrayHelper', '_isNotAnEmptyString'));
  158. }
  159. /**
  160. * Returns the first value in a given array.
  161. *
  162. * @param array $arr
  163. *
  164. * @return mixed|null
  165. */
  166. public static function getFirstValue($arr)
  167. {
  168. if (count($arr))
  169. {
  170. if (isset($arr[0]))
  171. {
  172. return $arr[0];
  173. }
  174. else
  175. {
  176. return $arr[array_shift(array_keys($arr))];
  177. }
  178. }
  179. }
  180. // Private Methods
  181. // =========================================================================
  182. /**
  183. * The array_filter() callback function for filterEmptyStringsFromArray().
  184. *
  185. * @param string $val
  186. *
  187. * @return bool
  188. */
  189. private static function _isNotAnEmptyString($val)
  190. {
  191. return (mb_strlen($val) != 0);
  192. }
  193. }