PageRenderTime 25ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/fulcrum/src/support/helpers/arr-helpers.php

https://gitlab.com/knowthecode/ktc-must-use
PHP | 343 lines | 114 code | 26 blank | 203 comment | 22 complexity | 021052632aa3b2a6590911e3c8e673ec MD5 | raw file
  1. <?php
  2. /**
  3. * Array Helpers Functions
  4. *
  5. * @package Fulcrum
  6. * @since 1.0.0
  7. * @author hellofromTonya
  8. * @link https://UpTechLabs.io
  9. * @license GNU General Public License 2.0+ and MIT Licence (MIT)
  10. */
  11. /**
  12. * This class has been adapted from the Laravel Illuminate framework, which
  13. * is copyrighted to Taylor Otwell and carries a MIT Licence (MIT).
  14. * Changes reflect WordPress coding standard, compliance with PHP 5.3, +
  15. * additional functionality.
  16. */
  17. use Fulcrum\Support\Helpers\Arr;
  18. if ( ! function_exists( 'fulcrum_remove_empty_elements_from_array' ) ) {
  19. /**
  20. * Remove all of the empty elements from the given array. A new array is built
  21. * from the filter and returned.
  22. *
  23. * @since 1.0.0
  24. *
  25. * @param array $array Array to be filtered.
  26. *
  27. * @returns Returns a new array without the empties.
  28. */
  29. function fulcrum_remove_empty_elements_from_array( $array ) {
  30. return array_filter( $array, function ( $value ) {
  31. return $value;
  32. } );
  33. }
  34. }
  35. if ( ! function_exists( 'fulcrum_array_add' ) ) {
  36. /**
  37. * Add an element to an array using "dot" notation if it doesn't exist.
  38. *
  39. * @param array $array
  40. * @param string $key
  41. * @param mixed $value
  42. *
  43. * @return array
  44. */
  45. function fulcrum_array_add( $array, $key, $value ) {
  46. return Arr::add( $array, $key, $value );
  47. }
  48. }
  49. if ( ! function_exists( 'fulcrum_array_dot' ) ) {
  50. /**
  51. * Flatten a multi-dimensional associative array with dots.
  52. *
  53. * @param array $array
  54. * @param string $prepend
  55. *
  56. * @return array
  57. */
  58. function fulcrum_array_dot( $array, $prepend = '' ) {
  59. return Arr::dot( $array, $prepend );
  60. }
  61. }
  62. if ( ! function_exists( 'fulcrum_array_except' ) ) {
  63. /**
  64. * Get all of the given array except for a specified array of items.
  65. *
  66. * @param array $array
  67. * @param array|string $keys
  68. *
  69. * @return array
  70. */
  71. function fulcrum_array_except( $array, $keys ) {
  72. return Arr::except( $array, $keys );
  73. }
  74. }
  75. if ( ! function_exists( 'fulcrum_array_fetch' ) ) {
  76. /**
  77. * Fetch a flattened array of a nested array element.
  78. *
  79. * @param array $array
  80. * @param string $key
  81. *
  82. * @return array
  83. */
  84. function fulcrum_array_fetch( $array, $key ) {
  85. return Arr::fetch( $array, $key );
  86. }
  87. }
  88. if ( ! function_exists( 'fulcrum_array_first' ) ) {
  89. /**
  90. * Return the first element in an array passing a given truth test.
  91. *
  92. * @param array $array
  93. * @param \Closure $callback
  94. * @param mixed $default
  95. *
  96. * @return mixed
  97. */
  98. function fulcrum_array_first( $array, $callback, $default = null ) {
  99. return Arr::first( $array, $callback, $default );
  100. }
  101. }
  102. if ( ! function_exists( 'fulcrum_array_head' ) ) {
  103. /**
  104. * Get the first element of an array. Useful for method chaining.
  105. *
  106. * @since 1.0.0
  107. *
  108. * @param array $array
  109. *
  110. * @return mixed
  111. */
  112. function fulcrum_array_head( $array ) {
  113. return reset( $array );
  114. }
  115. }
  116. if ( ! function_exists( 'fulcrum_is_array' ) ) {
  117. /**
  118. * Checks if the element within the array is a valid array - uses key dot notation.
  119. *
  120. * @since 1.0.0
  121. *
  122. * @param array|mixed $array
  123. * @param string $key
  124. * @param bool|true $valid_if_not_empty
  125. *
  126. * @return bool
  127. */
  128. function fulcrum_is_array( $array, $key = '', $valid_if_not_empty = true ) {
  129. return Arr::is_array( $array, $key, $valid_if_not_empty );
  130. }
  131. }
  132. if ( ! function_exists( 'fulcrum_array_last' ) ) {
  133. /**
  134. * Return the last element in an array passing a given truth test.
  135. *
  136. * @param array $array
  137. * @param \Closure $callback
  138. * @param mixed $default
  139. *
  140. * @return mixed
  141. */
  142. function fulcrum_array_last( $array, $callback, $default = null ) {
  143. return Arr::last( $array, $callback, $default );
  144. }
  145. }
  146. if ( ! function_exists( 'fulcrum_array_flatten' ) ) {
  147. /**
  148. * Flatten a multi-dimensional array into a single level.
  149. *
  150. * @param array $array
  151. *
  152. * @return array
  153. */
  154. function fulcrum_array_flatten( $array ) {
  155. return Arr::flatten( $array );
  156. }
  157. }
  158. if ( ! function_exists( 'fulcrum_array_forget' ) ) {
  159. /**
  160. * Remove one or many array items from a given array using "dot" notation.
  161. *
  162. * @param array $array
  163. * @param array|string $keys
  164. *
  165. * @return void
  166. */
  167. function fulcrum_array_forget( &$array, $keys ) {
  168. return Arr::forget( $array, $keys );
  169. }
  170. }
  171. if ( ! function_exists( 'fulcrum_array_get' ) ) {
  172. /**
  173. * Get an item from an array using "dot" notation.
  174. *
  175. * @param array $array
  176. * @param string $key
  177. * @param mixed $default
  178. *
  179. * @return mixed
  180. */
  181. function fulcrum_array_get( $array, $key, $default = null ) {
  182. return Arr::get( $array, $key, $default );
  183. }
  184. }
  185. if ( ! function_exists( 'fulcrum_array_has' ) ) {
  186. /**
  187. * Check if an item exists in an array using "dot" notation.
  188. *
  189. * @param array $array
  190. * @param string $key
  191. *
  192. * @return bool
  193. */
  194. function fulcrum_array_has( $array, $key ) {
  195. return Arr::has( $array, $key );
  196. }
  197. }
  198. if ( ! function_exists( 'fulcrum_array_only' ) ) {
  199. /**
  200. * Get a subset of the items from the given array.
  201. *
  202. * @param array $array
  203. * @param array|string $keys
  204. *
  205. * @return array
  206. */
  207. function fulcrum_array_only( $array, $keys ) {
  208. return Arr::only( $array, $keys );
  209. }
  210. }
  211. if ( ! function_exists( 'fulcrum_array_pluck' ) ) {
  212. /**
  213. * Pluck an array of values from an array.
  214. *
  215. * @param array $array
  216. * @param string $value
  217. * @param string $key
  218. *
  219. * @return array
  220. */
  221. function fulcrum_array_pluck( $array, $value, $key = null ) {
  222. return Arr::pluck( $array, $value, $key );
  223. }
  224. }
  225. if ( ! function_exists( 'fulcrum_array_pull' ) ) {
  226. /**
  227. * Get a value from the array, and remove it.
  228. *
  229. * @param array $array
  230. * @param string $key
  231. * @param mixed $default
  232. *
  233. * @return mixed
  234. */
  235. function fulcrum_array_pull( &$array, $key, $default = null ) {
  236. return Arr::pull( $array, $key, $default );
  237. }
  238. }
  239. if ( ! function_exists( 'fulcrum_array_set' ) ) {
  240. /**
  241. * Set an array item to a given value using "dot" notation.
  242. *
  243. * If no key is given to the method, the entire array will be replaced.
  244. *
  245. * @since 1.0.0
  246. *
  247. * @param array $array
  248. * @param string $key
  249. * @param mixed $value
  250. * @param bool $append When true, appends the value to the current value
  251. *
  252. * @return array
  253. */
  254. function fulcrum_array_set( &$array, $key, $value, $append = false ) {
  255. return Arr::set( $array, $key, $value, $append );
  256. }
  257. }
  258. if ( ! function_exists( 'fulcrum_array_sort' ) ) {
  259. /**
  260. * Sort the array using the given Closure.
  261. *
  262. * @param array $array
  263. * @param \Closure $callback
  264. *
  265. * @return array
  266. */
  267. function fulcrum_array_sort( $array, Closure $callback ) {
  268. return Arr::sort( $array, $callback );
  269. }
  270. }
  271. if ( ! function_exists( 'fulcrum_array_where' ) ) {
  272. /**
  273. * Filter the array using the given Closure.
  274. *
  275. * @param array $array
  276. * @param \Closure $callback
  277. *
  278. * @return array
  279. */
  280. function fulcrum_array_where( $array, Closure $callback ) {
  281. return Arr::where( $array, $callback );
  282. }
  283. }
  284. if ( ! function_exists( 'fulcrum_preg_replace_sub' ) ) {
  285. /**
  286. * Replace a given pattern with each value in the array in sequentially.
  287. *
  288. * @param string $pattern
  289. * @param array $replacements
  290. * @param string $subject
  291. *
  292. * @return string
  293. */
  294. function fulcrum_preg_replace_sub( $pattern, &$replacements, $subject ) {
  295. return preg_replace_callback( $pattern, function ( $match ) use ( &$replacements ) {
  296. return array_shift( $replacements );
  297. }, $subject );
  298. }
  299. }
  300. if ( ! function_exists( 'fulcrum_flatten_array_into_delimited_list' ) ) {
  301. /**
  302. * Flatten an array into a delimited list.
  303. *
  304. * @since 1.0.0
  305. *
  306. * @param array|string $to_be_converted Data to be converted.
  307. * @param string $list_delimiter Delimiter to be used.
  308. *
  309. * @return string
  310. */
  311. function fulcrum_flatten_array_into_delimited_list( $to_be_converted, $list_delimiter = ', ' ) {
  312. if ( is_array( $to_be_converted ) ) {
  313. return implode( $list_delimiter, $to_be_converted );
  314. }
  315. return $to_be_converted;
  316. }
  317. }