helpers.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. * @param mixed $value
  9. * @return \Illuminate\Support\Collection
  10. */
  11. function collect($value = null)
  12. {
  13. return new Collection($value);
  14. }
  15. }
  16. if (! function_exists('data_fill')) {
  17. /**
  18. * Fill in data where it's missing.
  19. *
  20. * @param mixed $target
  21. * @param string|array $key
  22. * @param mixed $value
  23. * @return mixed
  24. */
  25. function data_fill(&$target, $key, $value)
  26. {
  27. return data_set($target, $key, $value, false);
  28. }
  29. }
  30. if (! function_exists('data_get')) {
  31. /**
  32. * Get an item from an array or object using "dot" notation.
  33. *
  34. * @param mixed $target
  35. * @param string|array|int|null $key
  36. * @param mixed $default
  37. * @return mixed
  38. */
  39. function data_get($target, $key, $default = null)
  40. {
  41. if (is_null($key)) {
  42. return $target;
  43. }
  44. $key = is_array($key) ? $key : explode('.', $key);
  45. foreach ($key as $i => $segment) {
  46. unset($key[$i]);
  47. if (is_null($segment)) {
  48. return $target;
  49. }
  50. if ($segment === '*') {
  51. if ($target instanceof Collection) {
  52. $target = $target->all();
  53. } elseif (! is_array($target)) {
  54. return value($default);
  55. }
  56. $result = [];
  57. foreach ($target as $item) {
  58. $result[] = data_get($item, $key);
  59. }
  60. return in_array('*', $key) ? Arr::collapse($result) : $result;
  61. }
  62. if (Arr::accessible($target) && Arr::exists($target, $segment)) {
  63. $target = $target[$segment];
  64. } elseif (is_object($target) && isset($target->{$segment})) {
  65. $target = $target->{$segment};
  66. } else {
  67. return value($default);
  68. }
  69. }
  70. return $target;
  71. }
  72. }
  73. if (! function_exists('data_set')) {
  74. /**
  75. * Set an item on an array or object using dot notation.
  76. *
  77. * @param mixed $target
  78. * @param string|array $key
  79. * @param mixed $value
  80. * @param bool $overwrite
  81. * @return mixed
  82. */
  83. function data_set(&$target, $key, $value, $overwrite = true)
  84. {
  85. $segments = is_array($key) ? $key : explode('.', $key);
  86. if (($segment = array_shift($segments)) === '*') {
  87. if (! Arr::accessible($target)) {
  88. $target = [];
  89. }
  90. if ($segments) {
  91. foreach ($target as &$inner) {
  92. data_set($inner, $segments, $value, $overwrite);
  93. }
  94. } elseif ($overwrite) {
  95. foreach ($target as &$inner) {
  96. $inner = $value;
  97. }
  98. }
  99. } elseif (Arr::accessible($target)) {
  100. if ($segments) {
  101. if (! Arr::exists($target, $segment)) {
  102. $target[$segment] = [];
  103. }
  104. data_set($target[$segment], $segments, $value, $overwrite);
  105. } elseif ($overwrite || ! Arr::exists($target, $segment)) {
  106. $target[$segment] = $value;
  107. }
  108. } elseif (is_object($target)) {
  109. if ($segments) {
  110. if (! isset($target->{$segment})) {
  111. $target->{$segment} = [];
  112. }
  113. data_set($target->{$segment}, $segments, $value, $overwrite);
  114. } elseif ($overwrite || ! isset($target->{$segment})) {
  115. $target->{$segment} = $value;
  116. }
  117. } else {
  118. $target = [];
  119. if ($segments) {
  120. data_set($target[$segment], $segments, $value, $overwrite);
  121. } elseif ($overwrite) {
  122. $target[$segment] = $value;
  123. }
  124. }
  125. return $target;
  126. }
  127. }
  128. if (! function_exists('head')) {
  129. /**
  130. * Get the first element of an array. Useful for method chaining.
  131. *
  132. * @param array $array
  133. * @return mixed
  134. */
  135. function head($array)
  136. {
  137. return reset($array);
  138. }
  139. }
  140. if (! function_exists('last')) {
  141. /**
  142. * Get the last element from an array.
  143. *
  144. * @param array $array
  145. * @return mixed
  146. */
  147. function last($array)
  148. {
  149. return end($array);
  150. }
  151. }
  152. if (! function_exists('value')) {
  153. /**
  154. * Return the default value of the given value.
  155. *
  156. * @param mixed $value
  157. * @return mixed
  158. */
  159. function value($value, ...$args)
  160. {
  161. return $value instanceof Closure ? $value(...$args) : $value;
  162. }
  163. }