helpers.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. use Illuminate\Support\Arr;
  3. use Illuminate\Support\Collection;
  4. if (! function_exists('collect')) {
  5. /**
  6. * Create a collection from the given value.
  7. *
  8. * @template TKey of array-key
  9. * @template TValue
  10. *
  11. * @param \Illuminate\Contracts\Support\Arrayable<TKey, TValue>|iterable<TKey, TValue>|null $value
  12. * @return \Illuminate\Support\Collection<TKey, TValue>
  13. */
  14. function collect($value = []): Collection
  15. {
  16. return new Collection($value);
  17. }
  18. }
  19. if (! function_exists('data_fill')) {
  20. /**
  21. * Fill in data where it's missing.
  22. *
  23. * @param mixed $target
  24. * @param string|array $key
  25. * @param mixed $value
  26. * @return mixed
  27. */
  28. function data_fill(&$target, $key, $value)
  29. {
  30. return data_set($target, $key, $value, false);
  31. }
  32. }
  33. if (! function_exists('data_has')) {
  34. /**
  35. * Determine if a key / property exists on an array or object using "dot" notation.
  36. *
  37. * @param mixed $target
  38. * @param string|array|int|null $key
  39. * @return bool
  40. */
  41. function data_has($target, $key): bool
  42. {
  43. if (is_null($key) || $key === []) {
  44. return false;
  45. }
  46. $key = is_array($key) ? $key : explode('.', $key);
  47. foreach ($key as $segment) {
  48. if (Arr::accessible($target) && Arr::exists($target, $segment)) {
  49. $target = $target[$segment];
  50. } elseif (is_object($target) && property_exists($target, $segment)) {
  51. $target = $target->{$segment};
  52. } else {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. }
  59. if (! function_exists('data_get')) {
  60. /**
  61. * Get an item from an array or object using "dot" notation.
  62. *
  63. * @param mixed $target
  64. * @param string|array|int|null $key
  65. * @param mixed $default
  66. * @return mixed
  67. */
  68. function data_get($target, $key, $default = null)
  69. {
  70. if (is_null($key)) {
  71. return $target;
  72. }
  73. $key = is_array($key) ? $key : explode('.', $key);
  74. foreach ($key as $i => $segment) {
  75. unset($key[$i]);
  76. if (is_null($segment)) {
  77. return $target;
  78. }
  79. if ($segment === '*') {
  80. if ($target instanceof Collection) {
  81. $target = $target->all();
  82. } elseif (! is_iterable($target)) {
  83. return value($default);
  84. }
  85. $result = [];
  86. foreach ($target as $item) {
  87. $result[] = data_get($item, $key);
  88. }
  89. return in_array('*', $key) ? Arr::collapse($result) : $result;
  90. }
  91. $segment = match ($segment) {
  92. '\*' => '*',
  93. '\{first}' => '{first}',
  94. '{first}' => array_key_first(Arr::from($target)),
  95. '\{last}' => '{last}',
  96. '{last}' => array_key_last(Arr::from($target)),
  97. default => $segment,
  98. };
  99. if (Arr::accessible($target) && Arr::exists($target, $segment)) {
  100. $target = $target[$segment];
  101. } elseif (is_object($target) && isset($target->{$segment})) {
  102. $target = $target->{$segment};
  103. } else {
  104. return value($default);
  105. }
  106. }
  107. return $target;
  108. }
  109. }
  110. if (! function_exists('data_set')) {
  111. /**
  112. * Set an item on an array or object using dot notation.
  113. *
  114. * @param mixed $target
  115. * @param string|array $key
  116. * @param mixed $value
  117. * @param bool $overwrite
  118. * @return mixed
  119. */
  120. function data_set(&$target, $key, $value, $overwrite = true)
  121. {
  122. $segments = is_array($key) ? $key : explode('.', $key);
  123. if (($segment = array_shift($segments)) === '*') {
  124. if (! Arr::accessible($target)) {
  125. $target = [];
  126. }
  127. if ($segments) {
  128. foreach ($target as &$inner) {
  129. data_set($inner, $segments, $value, $overwrite);
  130. }
  131. } elseif ($overwrite) {
  132. foreach ($target as &$inner) {
  133. $inner = $value;
  134. }
  135. }
  136. } elseif (Arr::accessible($target)) {
  137. if ($segments) {
  138. if (! Arr::exists($target, $segment)) {
  139. $target[$segment] = [];
  140. }
  141. data_set($target[$segment], $segments, $value, $overwrite);
  142. } elseif ($overwrite || ! Arr::exists($target, $segment)) {
  143. $target[$segment] = $value;
  144. }
  145. } elseif (is_object($target)) {
  146. if ($segments) {
  147. if (! isset($target->{$segment})) {
  148. $target->{$segment} = [];
  149. }
  150. data_set($target->{$segment}, $segments, $value, $overwrite);
  151. } elseif ($overwrite || ! isset($target->{$segment})) {
  152. $target->{$segment} = $value;
  153. }
  154. } else {
  155. $target = [];
  156. if ($segments) {
  157. data_set($target[$segment], $segments, $value, $overwrite);
  158. } elseif ($overwrite) {
  159. $target[$segment] = $value;
  160. }
  161. }
  162. return $target;
  163. }
  164. }
  165. if (! function_exists('data_forget')) {
  166. /**
  167. * Remove / unset an item from an array or object using "dot" notation.
  168. *
  169. * @param mixed $target
  170. * @param string|array|int|null $key
  171. * @return mixed
  172. */
  173. function data_forget(&$target, $key)
  174. {
  175. $segments = is_array($key) ? $key : explode('.', $key);
  176. if (($segment = array_shift($segments)) === '*' && Arr::accessible($target)) {
  177. if ($segments) {
  178. foreach ($target as &$inner) {
  179. data_forget($inner, $segments);
  180. }
  181. }
  182. } elseif (Arr::accessible($target)) {
  183. if ($segments && Arr::exists($target, $segment)) {
  184. data_forget($target[$segment], $segments);
  185. } else {
  186. Arr::forget($target, $segment);
  187. }
  188. } elseif (is_object($target)) {
  189. if ($segments && isset($target->{$segment})) {
  190. data_forget($target->{$segment}, $segments);
  191. } elseif (isset($target->{$segment})) {
  192. unset($target->{$segment});
  193. }
  194. }
  195. return $target;
  196. }
  197. }
  198. if (! function_exists('head')) {
  199. /**
  200. * Get the first element of an array. Useful for method chaining.
  201. *
  202. * @param array $array
  203. * @return mixed
  204. */
  205. function head($array)
  206. {
  207. return empty($array) ? false : array_first($array);
  208. }
  209. }
  210. if (! function_exists('last')) {
  211. /**
  212. * Get the last element from an array.
  213. *
  214. * @param array $array
  215. * @return mixed
  216. */
  217. function last($array)
  218. {
  219. return empty($array) ? false : array_last($array);
  220. }
  221. }
  222. if (! function_exists('value')) {
  223. /**
  224. * Return the default value of the given value.
  225. *
  226. * @template TValue
  227. * @template TArgs
  228. *
  229. * @param TValue|\Closure(TArgs): TValue $value
  230. * @param TArgs ...$args
  231. * @return TValue
  232. */
  233. function value($value, ...$args)
  234. {
  235. return $value instanceof Closure ? $value(...$args) : $value;
  236. }
  237. }
  238. if (! function_exists('when')) {
  239. /**
  240. * Return a value if the given condition is true.
  241. *
  242. * @param mixed $condition
  243. * @param \Closure|mixed $value
  244. * @param \Closure|mixed $default
  245. * @return mixed
  246. */
  247. function when($condition, $value, $default = null)
  248. {
  249. $condition = $condition instanceof Closure ? $condition() : $condition;
  250. if ($condition) {
  251. return value($value, $condition);
  252. }
  253. return value($default, $condition);
  254. }
  255. }