helpers.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 = [])
  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_get')) {
  34. /**
  35. * Get an item from an array or object using "dot" notation.
  36. *
  37. * @param mixed $target
  38. * @param string|array|int|null $key
  39. * @param mixed $default
  40. * @return mixed
  41. */
  42. function data_get($target, $key, $default = null)
  43. {
  44. if (is_null($key)) {
  45. return $target;
  46. }
  47. $key = is_array($key) ? $key : explode('.', $key);
  48. foreach ($key as $i => $segment) {
  49. unset($key[$i]);
  50. if (is_null($segment)) {
  51. return $target;
  52. }
  53. if ($segment === '*') {
  54. if ($target instanceof Collection) {
  55. $target = $target->all();
  56. } elseif (! is_iterable($target)) {
  57. return value($default);
  58. }
  59. $result = [];
  60. foreach ($target as $item) {
  61. $result[] = data_get($item, $key);
  62. }
  63. return in_array('*', $key) ? Arr::collapse($result) : $result;
  64. }
  65. $segment = match ($segment) {
  66. '\*' => '*',
  67. '\{first}' => '{first}',
  68. '{first}' => array_key_first(is_array($target) ? $target : collect($target)->all()),
  69. '\{last}' => '{last}',
  70. '{last}' => array_key_last(is_array($target) ? $target : collect($target)->all()),
  71. default => $segment,
  72. };
  73. if (Arr::accessible($target) && Arr::exists($target, $segment)) {
  74. $target = $target[$segment];
  75. } elseif (is_object($target) && isset($target->{$segment})) {
  76. $target = $target->{$segment};
  77. } else {
  78. return value($default);
  79. }
  80. }
  81. return $target;
  82. }
  83. }
  84. if (! function_exists('data_set')) {
  85. /**
  86. * Set an item on an array or object using dot notation.
  87. *
  88. * @param mixed $target
  89. * @param string|array $key
  90. * @param mixed $value
  91. * @param bool $overwrite
  92. * @return mixed
  93. */
  94. function data_set(&$target, $key, $value, $overwrite = true)
  95. {
  96. $segments = is_array($key) ? $key : explode('.', $key);
  97. if (($segment = array_shift($segments)) === '*') {
  98. if (! Arr::accessible($target)) {
  99. $target = [];
  100. }
  101. if ($segments) {
  102. foreach ($target as &$inner) {
  103. data_set($inner, $segments, $value, $overwrite);
  104. }
  105. } elseif ($overwrite) {
  106. foreach ($target as &$inner) {
  107. $inner = $value;
  108. }
  109. }
  110. } elseif (Arr::accessible($target)) {
  111. if ($segments) {
  112. if (! Arr::exists($target, $segment)) {
  113. $target[$segment] = [];
  114. }
  115. data_set($target[$segment], $segments, $value, $overwrite);
  116. } elseif ($overwrite || ! Arr::exists($target, $segment)) {
  117. $target[$segment] = $value;
  118. }
  119. } elseif (is_object($target)) {
  120. if ($segments) {
  121. if (! isset($target->{$segment})) {
  122. $target->{$segment} = [];
  123. }
  124. data_set($target->{$segment}, $segments, $value, $overwrite);
  125. } elseif ($overwrite || ! isset($target->{$segment})) {
  126. $target->{$segment} = $value;
  127. }
  128. } else {
  129. $target = [];
  130. if ($segments) {
  131. data_set($target[$segment], $segments, $value, $overwrite);
  132. } elseif ($overwrite) {
  133. $target[$segment] = $value;
  134. }
  135. }
  136. return $target;
  137. }
  138. }
  139. if (! function_exists('data_forget')) {
  140. /**
  141. * Remove / unset an item from an array or object using "dot" notation.
  142. *
  143. * @param mixed $target
  144. * @param string|array|int|null $key
  145. * @return mixed
  146. */
  147. function data_forget(&$target, $key)
  148. {
  149. $segments = is_array($key) ? $key : explode('.', $key);
  150. if (($segment = array_shift($segments)) === '*' && Arr::accessible($target)) {
  151. if ($segments) {
  152. foreach ($target as &$inner) {
  153. data_forget($inner, $segments);
  154. }
  155. }
  156. } elseif (Arr::accessible($target)) {
  157. if ($segments && Arr::exists($target, $segment)) {
  158. data_forget($target[$segment], $segments);
  159. } else {
  160. Arr::forget($target, $segment);
  161. }
  162. } elseif (is_object($target)) {
  163. if ($segments && isset($target->{$segment})) {
  164. data_forget($target->{$segment}, $segments);
  165. } elseif (isset($target->{$segment})) {
  166. unset($target->{$segment});
  167. }
  168. }
  169. return $target;
  170. }
  171. }
  172. if (! function_exists('head')) {
  173. /**
  174. * Get the first element of an array. Useful for method chaining.
  175. *
  176. * @param array $array
  177. * @return mixed
  178. */
  179. function head($array)
  180. {
  181. return reset($array);
  182. }
  183. }
  184. if (! function_exists('last')) {
  185. /**
  186. * Get the last element from an array.
  187. *
  188. * @param array $array
  189. * @return mixed
  190. */
  191. function last($array)
  192. {
  193. return end($array);
  194. }
  195. }
  196. if (! function_exists('value')) {
  197. /**
  198. * Return the default value of the given value.
  199. *
  200. * @template TValue
  201. * @template TArgs
  202. *
  203. * @param TValue|\Closure(TArgs): TValue $value
  204. * @param TArgs ...$args
  205. * @return TValue
  206. */
  207. function value($value, ...$args)
  208. {
  209. return $value instanceof Closure ? $value(...$args) : $value;
  210. }
  211. }