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