@@ -7,77 +7,77 @@ |
||
| 7 | 7 | |
| 8 | 8 | trait Macroable |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The registered string macros. |
|
| 12 | - * |
|
| 13 | - * @var array |
|
| 14 | - */ |
|
| 15 | - protected static $macros = []; |
|
| 10 | + /** |
|
| 11 | + * The registered string macros. |
|
| 12 | + * |
|
| 13 | + * @var array |
|
| 14 | + */ |
|
| 15 | + protected static $macros = []; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * Register a custom macro. |
|
| 19 | - * |
|
| 20 | - * @param string $name |
|
| 21 | - * @param callable $macro |
|
| 22 | - * @return void |
|
| 23 | - */ |
|
| 24 | - public static function macro($name, callable $macro) |
|
| 25 | - { |
|
| 26 | - static::$macros[$name] = $macro; |
|
| 27 | - } |
|
| 17 | + /** |
|
| 18 | + * Register a custom macro. |
|
| 19 | + * |
|
| 20 | + * @param string $name |
|
| 21 | + * @param callable $macro |
|
| 22 | + * @return void |
|
| 23 | + */ |
|
| 24 | + public static function macro($name, callable $macro) |
|
| 25 | + { |
|
| 26 | + static::$macros[$name] = $macro; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Checks if macro is registered. |
|
| 31 | - * |
|
| 32 | - * @param string $name |
|
| 33 | - * @return bool |
|
| 34 | - */ |
|
| 35 | - public static function hasMacro($name) |
|
| 36 | - { |
|
| 37 | - return isset(static::$macros[$name]); |
|
| 38 | - } |
|
| 29 | + /** |
|
| 30 | + * Checks if macro is registered. |
|
| 31 | + * |
|
| 32 | + * @param string $name |
|
| 33 | + * @return bool |
|
| 34 | + */ |
|
| 35 | + public static function hasMacro($name) |
|
| 36 | + { |
|
| 37 | + return isset(static::$macros[$name]); |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Dynamically handle calls to the class. |
|
| 42 | - * |
|
| 43 | - * @param string $method |
|
| 44 | - * @param array $parameters |
|
| 45 | - * @return mixed |
|
| 46 | - * |
|
| 47 | - * @throws \BadMethodCallException |
|
| 48 | - */ |
|
| 49 | - public static function __callStatic($method, $parameters) |
|
| 50 | - { |
|
| 51 | - if (! static::hasMacro($method)) { |
|
| 52 | - throw new BadMethodCallException("Method {$method} does not exist."); |
|
| 53 | - } |
|
| 40 | + /** |
|
| 41 | + * Dynamically handle calls to the class. |
|
| 42 | + * |
|
| 43 | + * @param string $method |
|
| 44 | + * @param array $parameters |
|
| 45 | + * @return mixed |
|
| 46 | + * |
|
| 47 | + * @throws \BadMethodCallException |
|
| 48 | + */ |
|
| 49 | + public static function __callStatic($method, $parameters) |
|
| 50 | + { |
|
| 51 | + if (! static::hasMacro($method)) { |
|
| 52 | + throw new BadMethodCallException("Method {$method} does not exist."); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - if (static::$macros[$method] instanceof Closure) { |
|
| 56 | - return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters); |
|
| 57 | - } |
|
| 55 | + if (static::$macros[$method] instanceof Closure) { |
|
| 56 | + return call_user_func_array(Closure::bind(static::$macros[$method], null, static::class), $parameters); |
|
| 57 | + } |
|
| 58 | 58 | |
| 59 | - return call_user_func_array(static::$macros[$method], $parameters); |
|
| 60 | - } |
|
| 59 | + return call_user_func_array(static::$macros[$method], $parameters); |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * Dynamically handle calls to the class. |
|
| 64 | - * |
|
| 65 | - * @param string $method |
|
| 66 | - * @param array $parameters |
|
| 67 | - * @return mixed |
|
| 68 | - * |
|
| 69 | - * @throws \BadMethodCallException |
|
| 70 | - */ |
|
| 71 | - public function __call($method, $parameters) |
|
| 72 | - { |
|
| 73 | - if (! static::hasMacro($method)) { |
|
| 74 | - throw new BadMethodCallException("Method {$method} does not exist."); |
|
| 75 | - } |
|
| 62 | + /** |
|
| 63 | + * Dynamically handle calls to the class. |
|
| 64 | + * |
|
| 65 | + * @param string $method |
|
| 66 | + * @param array $parameters |
|
| 67 | + * @return mixed |
|
| 68 | + * |
|
| 69 | + * @throws \BadMethodCallException |
|
| 70 | + */ |
|
| 71 | + public function __call($method, $parameters) |
|
| 72 | + { |
|
| 73 | + if (! static::hasMacro($method)) { |
|
| 74 | + throw new BadMethodCallException("Method {$method} does not exist."); |
|
| 75 | + } |
|
| 76 | 76 | |
| 77 | - if (static::$macros[$method] instanceof Closure) { |
|
| 78 | - return call_user_func_array(static::$macros[$method]->bindTo($this, static::class), $parameters); |
|
| 79 | - } |
|
| 77 | + if (static::$macros[$method] instanceof Closure) { |
|
| 78 | + return call_user_func_array(static::$macros[$method]->bindTo($this, static::class), $parameters); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - return call_user_func_array(static::$macros[$method], $parameters); |
|
| 82 | - } |
|
| 81 | + return call_user_func_array(static::$macros[$method], $parameters); |
|
| 82 | + } |
|
| 83 | 83 | } |
@@ -7,57 +7,57 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class HigherOrderCollectionProxy |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * The collection being operated on. |
|
| 12 | - * |
|
| 13 | - * @var \Illuminate\Support\Collection |
|
| 14 | - */ |
|
| 15 | - protected $collection; |
|
| 10 | + /** |
|
| 11 | + * The collection being operated on. |
|
| 12 | + * |
|
| 13 | + * @var \Illuminate\Support\Collection |
|
| 14 | + */ |
|
| 15 | + protected $collection; |
|
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * The method being proxied. |
|
| 19 | - * |
|
| 20 | - * @var string |
|
| 21 | - */ |
|
| 22 | - protected $method; |
|
| 17 | + /** |
|
| 18 | + * The method being proxied. |
|
| 19 | + * |
|
| 20 | + * @var string |
|
| 21 | + */ |
|
| 22 | + protected $method; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Create a new proxy instance. |
|
| 26 | - * |
|
| 27 | - * @param \Illuminate\Support\Collection $collection |
|
| 28 | - * @param string $method |
|
| 29 | - * @return void |
|
| 30 | - */ |
|
| 31 | - public function __construct(Collection $collection, $method) |
|
| 32 | - { |
|
| 33 | - $this->method = $method; |
|
| 34 | - $this->collection = $collection; |
|
| 35 | - } |
|
| 24 | + /** |
|
| 25 | + * Create a new proxy instance. |
|
| 26 | + * |
|
| 27 | + * @param \Illuminate\Support\Collection $collection |
|
| 28 | + * @param string $method |
|
| 29 | + * @return void |
|
| 30 | + */ |
|
| 31 | + public function __construct(Collection $collection, $method) |
|
| 32 | + { |
|
| 33 | + $this->method = $method; |
|
| 34 | + $this->collection = $collection; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Proxy accessing an attribute onto the collection items. |
|
| 39 | - * |
|
| 40 | - * @param string $key |
|
| 41 | - * @return mixed |
|
| 42 | - */ |
|
| 43 | - public function __get($key) |
|
| 44 | - { |
|
| 45 | - return $this->collection->{$this->method}(function ($value) use ($key) { |
|
| 46 | - return is_array($value) ? $value[$key] : $value->{$key}; |
|
| 47 | - }); |
|
| 48 | - } |
|
| 37 | + /** |
|
| 38 | + * Proxy accessing an attribute onto the collection items. |
|
| 39 | + * |
|
| 40 | + * @param string $key |
|
| 41 | + * @return mixed |
|
| 42 | + */ |
|
| 43 | + public function __get($key) |
|
| 44 | + { |
|
| 45 | + return $this->collection->{$this->method}(function ($value) use ($key) { |
|
| 46 | + return is_array($value) ? $value[$key] : $value->{$key}; |
|
| 47 | + }); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * Proxy a method call onto the collection items. |
|
| 52 | - * |
|
| 53 | - * @param string $method |
|
| 54 | - * @param array $parameters |
|
| 55 | - * @return mixed |
|
| 56 | - */ |
|
| 57 | - public function __call($method, $parameters) |
|
| 58 | - { |
|
| 59 | - return $this->collection->{$this->method}(function ($value) use ($method, $parameters) { |
|
| 60 | - return $value->{$method}(...$parameters); |
|
| 61 | - }); |
|
| 62 | - } |
|
| 50 | + /** |
|
| 51 | + * Proxy a method call onto the collection items. |
|
| 52 | + * |
|
| 53 | + * @param string $method |
|
| 54 | + * @param array $parameters |
|
| 55 | + * @return mixed |
|
| 56 | + */ |
|
| 57 | + public function __call($method, $parameters) |
|
| 58 | + { |
|
| 59 | + return $this->collection->{$this->method}(function ($value) use ($method, $parameters) { |
|
| 60 | + return $value->{$method}(...$parameters); |
|
| 61 | + }); |
|
| 62 | + } |
|
| 63 | 63 | } |
@@ -16,1640 +16,1640 @@ |
||
| 16 | 16 | |
| 17 | 17 | class Collection implements ArrayAccess, Arrayable, Countable, IteratorAggregate, Jsonable, JsonSerializable |
| 18 | 18 | { |
| 19 | - use Macroable; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * The items contained in the collection. |
|
| 23 | - * |
|
| 24 | - * @var array |
|
| 25 | - */ |
|
| 26 | - protected $items = []; |
|
| 27 | - |
|
| 28 | - /** |
|
| 29 | - * The methods that can be proxied. |
|
| 30 | - * |
|
| 31 | - * @var array |
|
| 32 | - */ |
|
| 33 | - protected static $proxies = [ |
|
| 34 | - 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap', |
|
| 35 | - 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', |
|
| 36 | - ]; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * Create a new collection. |
|
| 40 | - * |
|
| 41 | - * @param mixed $items |
|
| 42 | - * @return void |
|
| 43 | - */ |
|
| 44 | - public function __construct($items = []) |
|
| 45 | - { |
|
| 46 | - $this->items = $this->getArrayableItems($items); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Create a new collection instance if the value isn't one already. |
|
| 51 | - * |
|
| 52 | - * @param mixed $items |
|
| 53 | - * @return static |
|
| 54 | - */ |
|
| 55 | - public static function make($items = []) |
|
| 56 | - { |
|
| 57 | - return new static($items); |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Create a new collection by invoking the callback a given number of times. |
|
| 62 | - * |
|
| 63 | - * @param int $number |
|
| 64 | - * @param callable $callback |
|
| 65 | - * @return static |
|
| 66 | - */ |
|
| 67 | - public static function times($number, callable $callback = null) |
|
| 68 | - { |
|
| 69 | - if ($number < 1) { |
|
| 70 | - return new static; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - if (is_null($callback)) { |
|
| 74 | - return new static(range(1, $number)); |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - return (new static(range(1, $number)))->map($callback); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * Get all of the items in the collection. |
|
| 82 | - * |
|
| 83 | - * @return array |
|
| 84 | - */ |
|
| 85 | - public function all() |
|
| 86 | - { |
|
| 87 | - return $this->items; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get the average value of a given key. |
|
| 92 | - * |
|
| 93 | - * @param callable|string|null $callback |
|
| 94 | - * @return mixed |
|
| 95 | - */ |
|
| 96 | - public function avg($callback = null) |
|
| 97 | - { |
|
| 98 | - if ($count = $this->count()) { |
|
| 99 | - return $this->sum($callback) / $count; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Alias for the "avg" method. |
|
| 105 | - * |
|
| 106 | - * @param callable|string|null $callback |
|
| 107 | - * @return mixed |
|
| 108 | - */ |
|
| 109 | - public function average($callback = null) |
|
| 110 | - { |
|
| 111 | - return $this->avg($callback); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the median of a given key. |
|
| 116 | - * |
|
| 117 | - * @param null $key |
|
| 118 | - * @return mixed |
|
| 119 | - */ |
|
| 120 | - public function median($key = null) |
|
| 121 | - { |
|
| 122 | - $count = $this->count(); |
|
| 123 | - |
|
| 124 | - if ($count == 0) { |
|
| 125 | - return; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - $values = with(isset($key) ? $this->pluck($key) : $this) |
|
| 129 | - ->sort()->values(); |
|
| 130 | - |
|
| 131 | - $middle = (int) ($count / 2); |
|
| 132 | - |
|
| 133 | - if ($count % 2) { |
|
| 134 | - return $values->get($middle); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - return (new static([ |
|
| 138 | - $values->get($middle - 1), $values->get($middle), |
|
| 139 | - ]))->average(); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * Get the mode of a given key. |
|
| 144 | - * |
|
| 145 | - * @param mixed $key |
|
| 146 | - * @return array|null |
|
| 147 | - */ |
|
| 148 | - public function mode($key = null) |
|
| 149 | - { |
|
| 150 | - $count = $this->count(); |
|
| 151 | - |
|
| 152 | - if ($count == 0) { |
|
| 153 | - return; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - $collection = isset($key) ? $this->pluck($key) : $this; |
|
| 157 | - |
|
| 158 | - $counts = new self; |
|
| 159 | - |
|
| 160 | - $collection->each(function ($value) use ($counts) { |
|
| 161 | - $counts[$value] = isset($counts[$value]) ? $counts[$value] + 1 : 1; |
|
| 162 | - }); |
|
| 163 | - |
|
| 164 | - $sorted = $counts->sort(); |
|
| 165 | - |
|
| 166 | - $highestValue = $sorted->last(); |
|
| 167 | - |
|
| 168 | - return $sorted->filter(function ($value) use ($highestValue) { |
|
| 169 | - return $value == $highestValue; |
|
| 170 | - })->sort()->keys()->all(); |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * Collapse the collection of items into a single array. |
|
| 175 | - * |
|
| 176 | - * @return static |
|
| 177 | - */ |
|
| 178 | - public function collapse() |
|
| 179 | - { |
|
| 180 | - return new static(Arr::collapse($this->items)); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Determine if an item exists in the collection. |
|
| 185 | - * |
|
| 186 | - * @param mixed $key |
|
| 187 | - * @param mixed $operator |
|
| 188 | - * @param mixed $value |
|
| 189 | - * @return bool |
|
| 190 | - */ |
|
| 191 | - public function contains($key, $operator = null, $value = null) |
|
| 192 | - { |
|
| 193 | - if (func_num_args() == 1) { |
|
| 194 | - if ($this->useAsCallable($key)) { |
|
| 195 | - return ! is_null($this->first($key)); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - return in_array($key, $this->items); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - if (func_num_args() == 2) { |
|
| 202 | - $value = $operator; |
|
| 203 | - |
|
| 204 | - $operator = '='; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - return $this->contains($this->operatorForWhere($key, $operator, $value)); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - /** |
|
| 211 | - * Determine if an item exists in the collection using strict comparison. |
|
| 212 | - * |
|
| 213 | - * @param mixed $key |
|
| 214 | - * @param mixed $value |
|
| 215 | - * @return bool |
|
| 216 | - */ |
|
| 217 | - public function containsStrict($key, $value = null) |
|
| 218 | - { |
|
| 219 | - if (func_num_args() == 2) { |
|
| 220 | - return $this->contains(function ($item) use ($key, $value) { |
|
| 221 | - return data_get($item, $key) === $value; |
|
| 222 | - }); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - if ($this->useAsCallable($key)) { |
|
| 226 | - return ! is_null($this->first($key)); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return in_array($key, $this->items, true); |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * Cross join with the given lists, returning all possible permutations. |
|
| 234 | - * |
|
| 235 | - * @param mixed ...$lists |
|
| 236 | - * @return static |
|
| 237 | - */ |
|
| 238 | - public function crossJoin(...$lists) |
|
| 239 | - { |
|
| 240 | - return new static(Arr::crossJoin( |
|
| 241 | - $this->items, ...array_map([$this, 'getArrayableItems'], $lists) |
|
| 242 | - )); |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Get the items in the collection that are not present in the given items. |
|
| 247 | - * |
|
| 248 | - * @param mixed $items |
|
| 249 | - * @return static |
|
| 250 | - */ |
|
| 251 | - public function diff($items) |
|
| 252 | - { |
|
| 253 | - return new static(array_diff($this->items, $this->getArrayableItems($items))); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Get the items in the collection whose keys and values are not present in the given items. |
|
| 258 | - * |
|
| 259 | - * @param mixed $items |
|
| 260 | - * @return static |
|
| 261 | - */ |
|
| 262 | - public function diffAssoc($items) |
|
| 263 | - { |
|
| 264 | - return new static(array_diff_assoc($this->items, $this->getArrayableItems($items))); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - /** |
|
| 268 | - * Get the items in the collection whose keys are not present in the given items. |
|
| 269 | - * |
|
| 270 | - * @param mixed $items |
|
| 271 | - * @return static |
|
| 272 | - */ |
|
| 273 | - public function diffKeys($items) |
|
| 274 | - { |
|
| 275 | - return new static(array_diff_key($this->items, $this->getArrayableItems($items))); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Execute a callback over each item. |
|
| 280 | - * |
|
| 281 | - * @param callable $callback |
|
| 282 | - * @return $this |
|
| 283 | - */ |
|
| 284 | - public function each(callable $callback) |
|
| 285 | - { |
|
| 286 | - foreach ($this->items as $key => $item) { |
|
| 287 | - if ($callback($item, $key) === false) { |
|
| 288 | - break; |
|
| 289 | - } |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - return $this; |
|
| 293 | - } |
|
| 294 | - |
|
| 295 | - /** |
|
| 296 | - * Execute a callback over each nested chunk of items. |
|
| 297 | - * |
|
| 298 | - * @param callable $callback |
|
| 299 | - * @return static |
|
| 300 | - */ |
|
| 301 | - public function eachSpread(callable $callback) |
|
| 302 | - { |
|
| 303 | - return $this->each(function ($chunk) use ($callback) { |
|
| 304 | - return $callback(...$chunk); |
|
| 305 | - }); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - /** |
|
| 309 | - * Determine if all items in the collection pass the given test. |
|
| 310 | - * |
|
| 311 | - * @param string|callable $key |
|
| 312 | - * @param mixed $operator |
|
| 313 | - * @param mixed $value |
|
| 314 | - * @return bool |
|
| 315 | - */ |
|
| 316 | - public function every($key, $operator = null, $value = null) |
|
| 317 | - { |
|
| 318 | - if (func_num_args() == 1) { |
|
| 319 | - $callback = $this->valueRetriever($key); |
|
| 320 | - |
|
| 321 | - foreach ($this->items as $k => $v) { |
|
| 322 | - if (! $callback($v, $k)) { |
|
| 323 | - return false; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - |
|
| 327 | - return true; |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - if (func_num_args() == 2) { |
|
| 331 | - $value = $operator; |
|
| 332 | - |
|
| 333 | - $operator = '='; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - return $this->every($this->operatorForWhere($key, $operator, $value)); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Get all items except for those with the specified keys. |
|
| 341 | - * |
|
| 342 | - * @param mixed $keys |
|
| 343 | - * @return static |
|
| 344 | - */ |
|
| 345 | - public function except($keys) |
|
| 346 | - { |
|
| 347 | - $keys = is_array($keys) ? $keys : func_get_args(); |
|
| 348 | - |
|
| 349 | - return new static(Arr::except($this->items, $keys)); |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - /** |
|
| 353 | - * Run a filter over each of the items. |
|
| 354 | - * |
|
| 355 | - * @param callable|null $callback |
|
| 356 | - * @return static |
|
| 357 | - */ |
|
| 358 | - public function filter(callable $callback = null) |
|
| 359 | - { |
|
| 360 | - if ($callback) { |
|
| 361 | - return new static(Arr::where($this->items, $callback)); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - return new static(array_filter($this->items)); |
|
| 365 | - } |
|
| 366 | - |
|
| 367 | - /** |
|
| 368 | - * Apply the callback if the value is truthy. |
|
| 369 | - * |
|
| 370 | - * @param bool $value |
|
| 371 | - * @param callable $callback |
|
| 372 | - * @param callable $default |
|
| 373 | - * @return mixed |
|
| 374 | - */ |
|
| 375 | - public function when($value, callable $callback, callable $default = null) |
|
| 376 | - { |
|
| 377 | - if ($value) { |
|
| 378 | - return $callback($this); |
|
| 379 | - } elseif ($default) { |
|
| 380 | - return $default($this); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - return $this; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - /** |
|
| 387 | - * Apply the callback if the value is falsy. |
|
| 388 | - * |
|
| 389 | - * @param bool $value |
|
| 390 | - * @param callable $callback |
|
| 391 | - * @param callable $default |
|
| 392 | - * @return mixed |
|
| 393 | - */ |
|
| 394 | - public function unless($value, callable $callback, callable $default = null) |
|
| 395 | - { |
|
| 396 | - return $this->when(! $value, $callback, $default); |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Filter items by the given key value pair. |
|
| 401 | - * |
|
| 402 | - * @param string $key |
|
| 403 | - * @param mixed $operator |
|
| 404 | - * @param mixed $value |
|
| 405 | - * @return static |
|
| 406 | - */ |
|
| 407 | - public function where($key, $operator, $value = null) |
|
| 408 | - { |
|
| 409 | - if (func_num_args() == 2) { |
|
| 410 | - $value = $operator; |
|
| 411 | - |
|
| 412 | - $operator = '='; |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - return $this->filter($this->operatorForWhere($key, $operator, $value)); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * Get an operator checker callback. |
|
| 420 | - * |
|
| 421 | - * @param string $key |
|
| 422 | - * @param string $operator |
|
| 423 | - * @param mixed $value |
|
| 424 | - * @return \Closure |
|
| 425 | - */ |
|
| 426 | - protected function operatorForWhere($key, $operator, $value) |
|
| 427 | - { |
|
| 428 | - return function ($item) use ($key, $operator, $value) { |
|
| 429 | - $retrieved = data_get($item, $key); |
|
| 430 | - |
|
| 431 | - switch ($operator) { |
|
| 432 | - default: |
|
| 433 | - case '=': |
|
| 434 | - case '==': return $retrieved == $value; |
|
| 435 | - case '!=': |
|
| 436 | - case '<>': return $retrieved != $value; |
|
| 437 | - case '<': return $retrieved < $value; |
|
| 438 | - case '>': return $retrieved > $value; |
|
| 439 | - case '<=': return $retrieved <= $value; |
|
| 440 | - case '>=': return $retrieved >= $value; |
|
| 441 | - case '===': return $retrieved === $value; |
|
| 442 | - case '!==': return $retrieved !== $value; |
|
| 443 | - } |
|
| 444 | - }; |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - /** |
|
| 448 | - * Filter items by the given key value pair using strict comparison. |
|
| 449 | - * |
|
| 450 | - * @param string $key |
|
| 451 | - * @param mixed $value |
|
| 452 | - * @return static |
|
| 453 | - */ |
|
| 454 | - public function whereStrict($key, $value) |
|
| 455 | - { |
|
| 456 | - return $this->where($key, '===', $value); |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - /** |
|
| 460 | - * Filter items by the given key value pair. |
|
| 461 | - * |
|
| 462 | - * @param string $key |
|
| 463 | - * @param mixed $values |
|
| 464 | - * @param bool $strict |
|
| 465 | - * @return static |
|
| 466 | - */ |
|
| 467 | - public function whereIn($key, $values, $strict = false) |
|
| 468 | - { |
|
| 469 | - $values = $this->getArrayableItems($values); |
|
| 470 | - |
|
| 471 | - return $this->filter(function ($item) use ($key, $values, $strict) { |
|
| 472 | - return in_array(data_get($item, $key), $values, $strict); |
|
| 473 | - }); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - /** |
|
| 477 | - * Filter items by the given key value pair using strict comparison. |
|
| 478 | - * |
|
| 479 | - * @param string $key |
|
| 480 | - * @param mixed $values |
|
| 481 | - * @return static |
|
| 482 | - */ |
|
| 483 | - public function whereInStrict($key, $values) |
|
| 484 | - { |
|
| 485 | - return $this->whereIn($key, $values, true); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Filter items by the given key value pair. |
|
| 490 | - * |
|
| 491 | - * @param string $key |
|
| 492 | - * @param mixed $values |
|
| 493 | - * @param bool $strict |
|
| 494 | - * @return static |
|
| 495 | - */ |
|
| 496 | - public function whereNotIn($key, $values, $strict = false) |
|
| 497 | - { |
|
| 498 | - $values = $this->getArrayableItems($values); |
|
| 499 | - |
|
| 500 | - return $this->reject(function ($item) use ($key, $values, $strict) { |
|
| 501 | - return in_array(data_get($item, $key), $values, $strict); |
|
| 502 | - }); |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Filter items by the given key value pair using strict comparison. |
|
| 507 | - * |
|
| 508 | - * @param string $key |
|
| 509 | - * @param mixed $values |
|
| 510 | - * @return static |
|
| 511 | - */ |
|
| 512 | - public function whereNotInStrict($key, $values) |
|
| 513 | - { |
|
| 514 | - return $this->whereNotIn($key, $values, true); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - /** |
|
| 518 | - * Get the first item from the collection. |
|
| 519 | - * |
|
| 520 | - * @param callable|null $callback |
|
| 521 | - * @param mixed $default |
|
| 522 | - * @return mixed |
|
| 523 | - */ |
|
| 524 | - public function first(callable $callback = null, $default = null) |
|
| 525 | - { |
|
| 526 | - return Arr::first($this->items, $callback, $default); |
|
| 527 | - } |
|
| 528 | - |
|
| 529 | - /** |
|
| 530 | - * Get a flattened array of the items in the collection. |
|
| 531 | - * |
|
| 532 | - * @param int $depth |
|
| 533 | - * @return static |
|
| 534 | - */ |
|
| 535 | - public function flatten($depth = INF) |
|
| 536 | - { |
|
| 537 | - return new static(Arr::flatten($this->items, $depth)); |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - /** |
|
| 541 | - * Flip the items in the collection. |
|
| 542 | - * |
|
| 543 | - * @return static |
|
| 544 | - */ |
|
| 545 | - public function flip() |
|
| 546 | - { |
|
| 547 | - return new static(array_flip($this->items)); |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - /** |
|
| 551 | - * Remove an item from the collection by key. |
|
| 552 | - * |
|
| 553 | - * @param string|array $keys |
|
| 554 | - * @return $this |
|
| 555 | - */ |
|
| 556 | - public function forget($keys) |
|
| 557 | - { |
|
| 558 | - foreach ((array) $keys as $key) { |
|
| 559 | - $this->offsetUnset($key); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - return $this; |
|
| 563 | - } |
|
| 564 | - |
|
| 565 | - /** |
|
| 566 | - * Get an item from the collection by key. |
|
| 567 | - * |
|
| 568 | - * @param mixed $key |
|
| 569 | - * @param mixed $default |
|
| 570 | - * @return mixed |
|
| 571 | - */ |
|
| 572 | - public function get($key, $default = null) |
|
| 573 | - { |
|
| 574 | - if ($this->offsetExists($key)) { |
|
| 575 | - return $this->items[$key]; |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - return value($default); |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * Group an associative array by a field or using a callback. |
|
| 583 | - * |
|
| 584 | - * @param callable|string $groupBy |
|
| 585 | - * @param bool $preserveKeys |
|
| 586 | - * @return static |
|
| 587 | - */ |
|
| 588 | - public function groupBy($groupBy, $preserveKeys = false) |
|
| 589 | - { |
|
| 590 | - $groupBy = $this->valueRetriever($groupBy); |
|
| 591 | - |
|
| 592 | - $results = []; |
|
| 593 | - |
|
| 594 | - foreach ($this->items as $key => $value) { |
|
| 595 | - $groupKeys = $groupBy($value, $key); |
|
| 596 | - |
|
| 597 | - if (! is_array($groupKeys)) { |
|
| 598 | - $groupKeys = [$groupKeys]; |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - foreach ($groupKeys as $groupKey) { |
|
| 602 | - $groupKey = is_bool($groupKey) ? (int) $groupKey : $groupKey; |
|
| 603 | - |
|
| 604 | - if (! array_key_exists($groupKey, $results)) { |
|
| 605 | - $results[$groupKey] = new static; |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value); |
|
| 609 | - } |
|
| 610 | - } |
|
| 611 | - |
|
| 612 | - return new static($results); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Key an associative array by a field or using a callback. |
|
| 617 | - * |
|
| 618 | - * @param callable|string $keyBy |
|
| 619 | - * @return static |
|
| 620 | - */ |
|
| 621 | - public function keyBy($keyBy) |
|
| 622 | - { |
|
| 623 | - $keyBy = $this->valueRetriever($keyBy); |
|
| 624 | - |
|
| 625 | - $results = []; |
|
| 626 | - |
|
| 627 | - foreach ($this->items as $key => $item) { |
|
| 628 | - $resolvedKey = $keyBy($item, $key); |
|
| 629 | - |
|
| 630 | - if (is_object($resolvedKey)) { |
|
| 631 | - $resolvedKey = (string) $resolvedKey; |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - $results[$resolvedKey] = $item; |
|
| 635 | - } |
|
| 636 | - |
|
| 637 | - return new static($results); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * Determine if an item exists in the collection by key. |
|
| 642 | - * |
|
| 643 | - * @param mixed $key |
|
| 644 | - * @return bool |
|
| 645 | - */ |
|
| 646 | - public function has($key) |
|
| 647 | - { |
|
| 648 | - return $this->offsetExists($key); |
|
| 649 | - } |
|
| 650 | - |
|
| 651 | - /** |
|
| 652 | - * Concatenate values of a given key as a string. |
|
| 653 | - * |
|
| 654 | - * @param string $value |
|
| 655 | - * @param string $glue |
|
| 656 | - * @return string |
|
| 657 | - */ |
|
| 658 | - public function implode($value, $glue = null) |
|
| 659 | - { |
|
| 660 | - $first = $this->first(); |
|
| 661 | - |
|
| 662 | - if (is_array($first) || is_object($first)) { |
|
| 663 | - return implode($glue, $this->pluck($value)->all()); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - return implode($value, $this->items); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Intersect the collection with the given items. |
|
| 671 | - * |
|
| 672 | - * @param mixed $items |
|
| 673 | - * @return static |
|
| 674 | - */ |
|
| 675 | - public function intersect($items) |
|
| 676 | - { |
|
| 677 | - return new static(array_intersect($this->items, $this->getArrayableItems($items))); |
|
| 678 | - } |
|
| 679 | - |
|
| 680 | - /** |
|
| 681 | - * Intersect the collection with the given items by key. |
|
| 682 | - * |
|
| 683 | - * @param mixed $items |
|
| 684 | - * @return static |
|
| 685 | - */ |
|
| 686 | - public function intersectKey($items) |
|
| 687 | - { |
|
| 688 | - return new static(array_intersect_key($this->items, $this->getArrayableItems($items))); |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - /** |
|
| 692 | - * Determine if the collection is empty or not. |
|
| 693 | - * |
|
| 694 | - * @return bool |
|
| 695 | - */ |
|
| 696 | - public function isEmpty() |
|
| 697 | - { |
|
| 698 | - return empty($this->items); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Determine if the collection is not empty. |
|
| 703 | - * |
|
| 704 | - * @return bool |
|
| 705 | - */ |
|
| 706 | - public function isNotEmpty() |
|
| 707 | - { |
|
| 708 | - return ! $this->isEmpty(); |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - /** |
|
| 712 | - * Determine if the given value is callable, but not a string. |
|
| 713 | - * |
|
| 714 | - * @param mixed $value |
|
| 715 | - * @return bool |
|
| 716 | - */ |
|
| 717 | - protected function useAsCallable($value) |
|
| 718 | - { |
|
| 719 | - return ! is_string($value) && is_callable($value); |
|
| 720 | - } |
|
| 721 | - |
|
| 722 | - /** |
|
| 723 | - * Get the keys of the collection items. |
|
| 724 | - * |
|
| 725 | - * @return static |
|
| 726 | - */ |
|
| 727 | - public function keys() |
|
| 728 | - { |
|
| 729 | - return new static(array_keys($this->items)); |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - /** |
|
| 733 | - * Get the last item from the collection. |
|
| 734 | - * |
|
| 735 | - * @param callable|null $callback |
|
| 736 | - * @param mixed $default |
|
| 737 | - * @return mixed |
|
| 738 | - */ |
|
| 739 | - public function last(callable $callback = null, $default = null) |
|
| 740 | - { |
|
| 741 | - return Arr::last($this->items, $callback, $default); |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - /** |
|
| 745 | - * Get the values of a given key. |
|
| 746 | - * |
|
| 747 | - * @param string|array $value |
|
| 748 | - * @param string|null $key |
|
| 749 | - * @return static |
|
| 750 | - */ |
|
| 751 | - public function pluck($value, $key = null) |
|
| 752 | - { |
|
| 753 | - return new static(Arr::pluck($this->items, $value, $key)); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - /** |
|
| 757 | - * Run a map over each of the items. |
|
| 758 | - * |
|
| 759 | - * @param callable $callback |
|
| 760 | - * @return static |
|
| 761 | - */ |
|
| 762 | - public function map(callable $callback) |
|
| 763 | - { |
|
| 764 | - $keys = array_keys($this->items); |
|
| 765 | - |
|
| 766 | - $items = array_map($callback, $this->items, $keys); |
|
| 767 | - |
|
| 768 | - return new static(array_combine($keys, $items)); |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - /** |
|
| 772 | - * Run a map over each nested chunk of items. |
|
| 773 | - * |
|
| 774 | - * @param callable $callback |
|
| 775 | - * @return static |
|
| 776 | - */ |
|
| 777 | - public function mapSpread(callable $callback) |
|
| 778 | - { |
|
| 779 | - return $this->map(function ($chunk) use ($callback) { |
|
| 780 | - return $callback(...$chunk); |
|
| 781 | - }); |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - /** |
|
| 785 | - * Run a grouping map over the items. |
|
| 786 | - * |
|
| 787 | - * The callback should return an associative array with a single key/value pair. |
|
| 788 | - * |
|
| 789 | - * @param callable $callback |
|
| 790 | - * @return static |
|
| 791 | - */ |
|
| 792 | - public function mapToGroups(callable $callback) |
|
| 793 | - { |
|
| 794 | - $groups = $this->map($callback)->reduce(function ($groups, $pair) { |
|
| 795 | - $groups[key($pair)][] = reset($pair); |
|
| 796 | - |
|
| 797 | - return $groups; |
|
| 798 | - }, []); |
|
| 799 | - |
|
| 800 | - return (new static($groups))->map([$this, 'make']); |
|
| 801 | - } |
|
| 802 | - |
|
| 803 | - /** |
|
| 804 | - * Run an associative map over each of the items. |
|
| 805 | - * |
|
| 806 | - * The callback should return an associative array with a single key/value pair. |
|
| 807 | - * |
|
| 808 | - * @param callable $callback |
|
| 809 | - * @return static |
|
| 810 | - */ |
|
| 811 | - public function mapWithKeys(callable $callback) |
|
| 812 | - { |
|
| 813 | - $result = []; |
|
| 814 | - |
|
| 815 | - foreach ($this->items as $key => $value) { |
|
| 816 | - $assoc = $callback($value, $key); |
|
| 817 | - |
|
| 818 | - foreach ($assoc as $mapKey => $mapValue) { |
|
| 819 | - $result[$mapKey] = $mapValue; |
|
| 820 | - } |
|
| 821 | - } |
|
| 822 | - |
|
| 823 | - return new static($result); |
|
| 824 | - } |
|
| 825 | - |
|
| 826 | - /** |
|
| 827 | - * Map a collection and flatten the result by a single level. |
|
| 828 | - * |
|
| 829 | - * @param callable $callback |
|
| 830 | - * @return static |
|
| 831 | - */ |
|
| 832 | - public function flatMap(callable $callback) |
|
| 833 | - { |
|
| 834 | - return $this->map($callback)->collapse(); |
|
| 835 | - } |
|
| 836 | - |
|
| 837 | - /** |
|
| 838 | - * Get the max value of a given key. |
|
| 839 | - * |
|
| 840 | - * @param callable|string|null $callback |
|
| 841 | - * @return mixed |
|
| 842 | - */ |
|
| 843 | - public function max($callback = null) |
|
| 844 | - { |
|
| 845 | - $callback = $this->valueRetriever($callback); |
|
| 846 | - |
|
| 847 | - return $this->filter(function ($value) { |
|
| 848 | - return ! is_null($value); |
|
| 849 | - })->reduce(function ($result, $item) use ($callback) { |
|
| 850 | - $value = $callback($item); |
|
| 851 | - |
|
| 852 | - return is_null($result) || $value > $result ? $value : $result; |
|
| 853 | - }); |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - /** |
|
| 857 | - * Merge the collection with the given items. |
|
| 858 | - * |
|
| 859 | - * @param mixed $items |
|
| 860 | - * @return static |
|
| 861 | - */ |
|
| 862 | - public function merge($items) |
|
| 863 | - { |
|
| 864 | - return new static(array_merge($this->items, $this->getArrayableItems($items))); |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - /** |
|
| 868 | - * Create a collection by using this collection for keys and another for its values. |
|
| 869 | - * |
|
| 870 | - * @param mixed $values |
|
| 871 | - * @return static |
|
| 872 | - */ |
|
| 873 | - public function combine($values) |
|
| 874 | - { |
|
| 875 | - return new static(array_combine($this->all(), $this->getArrayableItems($values))); |
|
| 876 | - } |
|
| 877 | - |
|
| 878 | - /** |
|
| 879 | - * Union the collection with the given items. |
|
| 880 | - * |
|
| 881 | - * @param mixed $items |
|
| 882 | - * @return static |
|
| 883 | - */ |
|
| 884 | - public function union($items) |
|
| 885 | - { |
|
| 886 | - return new static($this->items + $this->getArrayableItems($items)); |
|
| 887 | - } |
|
| 888 | - |
|
| 889 | - /** |
|
| 890 | - * Get the min value of a given key. |
|
| 891 | - * |
|
| 892 | - * @param callable|string|null $callback |
|
| 893 | - * @return mixed |
|
| 894 | - */ |
|
| 895 | - public function min($callback = null) |
|
| 896 | - { |
|
| 897 | - $callback = $this->valueRetriever($callback); |
|
| 898 | - |
|
| 899 | - return $this->filter(function ($value) { |
|
| 900 | - return ! is_null($value); |
|
| 901 | - })->reduce(function ($result, $item) use ($callback) { |
|
| 902 | - $value = $callback($item); |
|
| 903 | - |
|
| 904 | - return is_null($result) || $value < $result ? $value : $result; |
|
| 905 | - }); |
|
| 906 | - } |
|
| 907 | - |
|
| 908 | - /** |
|
| 909 | - * Create a new collection consisting of every n-th element. |
|
| 910 | - * |
|
| 911 | - * @param int $step |
|
| 912 | - * @param int $offset |
|
| 913 | - * @return static |
|
| 914 | - */ |
|
| 915 | - public function nth($step, $offset = 0) |
|
| 916 | - { |
|
| 917 | - $new = []; |
|
| 918 | - |
|
| 919 | - $position = 0; |
|
| 920 | - |
|
| 921 | - foreach ($this->items as $item) { |
|
| 922 | - if ($position % $step === $offset) { |
|
| 923 | - $new[] = $item; |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - $position++; |
|
| 927 | - } |
|
| 928 | - |
|
| 929 | - return new static($new); |
|
| 930 | - } |
|
| 931 | - |
|
| 932 | - /** |
|
| 933 | - * Get the items with the specified keys. |
|
| 934 | - * |
|
| 935 | - * @param mixed $keys |
|
| 936 | - * @return static |
|
| 937 | - */ |
|
| 938 | - public function only($keys) |
|
| 939 | - { |
|
| 940 | - if (is_null($keys)) { |
|
| 941 | - return new static($this->items); |
|
| 942 | - } |
|
| 943 | - |
|
| 944 | - $keys = is_array($keys) ? $keys : func_get_args(); |
|
| 945 | - |
|
| 946 | - return new static(Arr::only($this->items, $keys)); |
|
| 947 | - } |
|
| 948 | - |
|
| 949 | - /** |
|
| 950 | - * "Paginate" the collection by slicing it into a smaller collection. |
|
| 951 | - * |
|
| 952 | - * @param int $page |
|
| 953 | - * @param int $perPage |
|
| 954 | - * @return static |
|
| 955 | - */ |
|
| 956 | - public function forPage($page, $perPage) |
|
| 957 | - { |
|
| 958 | - return $this->slice(($page - 1) * $perPage, $perPage); |
|
| 959 | - } |
|
| 960 | - |
|
| 961 | - /** |
|
| 962 | - * Partition the collection into two arrays using the given callback or key. |
|
| 963 | - * |
|
| 964 | - * @param callable|string $callback |
|
| 965 | - * @return static |
|
| 966 | - */ |
|
| 967 | - public function partition($callback) |
|
| 968 | - { |
|
| 969 | - $partitions = [new static, new static]; |
|
| 970 | - |
|
| 971 | - $callback = $this->valueRetriever($callback); |
|
| 972 | - |
|
| 973 | - foreach ($this->items as $key => $item) { |
|
| 974 | - $partitions[(int) ! $callback($item)][$key] = $item; |
|
| 975 | - } |
|
| 976 | - |
|
| 977 | - return new static($partitions); |
|
| 978 | - } |
|
| 979 | - |
|
| 980 | - /** |
|
| 981 | - * Pass the collection to the given callback and return the result. |
|
| 982 | - * |
|
| 983 | - * @param callable $callback |
|
| 984 | - * @return mixed |
|
| 985 | - */ |
|
| 986 | - public function pipe(callable $callback) |
|
| 987 | - { |
|
| 988 | - return $callback($this); |
|
| 989 | - } |
|
| 990 | - |
|
| 991 | - /** |
|
| 992 | - * Get and remove the last item from the collection. |
|
| 993 | - * |
|
| 994 | - * @return mixed |
|
| 995 | - */ |
|
| 996 | - public function pop() |
|
| 997 | - { |
|
| 998 | - return array_pop($this->items); |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - /** |
|
| 1002 | - * Push an item onto the beginning of the collection. |
|
| 1003 | - * |
|
| 1004 | - * @param mixed $value |
|
| 1005 | - * @param mixed $key |
|
| 1006 | - * @return $this |
|
| 1007 | - */ |
|
| 1008 | - public function prepend($value, $key = null) |
|
| 1009 | - { |
|
| 1010 | - $this->items = Arr::prepend($this->items, $value, $key); |
|
| 1011 | - |
|
| 1012 | - return $this; |
|
| 1013 | - } |
|
| 1014 | - |
|
| 1015 | - /** |
|
| 1016 | - * Push an item onto the end of the collection. |
|
| 1017 | - * |
|
| 1018 | - * @param mixed $value |
|
| 1019 | - * @return $this |
|
| 1020 | - */ |
|
| 1021 | - public function push($value) |
|
| 1022 | - { |
|
| 1023 | - $this->offsetSet(null, $value); |
|
| 1024 | - |
|
| 1025 | - return $this; |
|
| 1026 | - } |
|
| 1027 | - |
|
| 1028 | - /** |
|
| 1029 | - * Push all of the given items onto the collection. |
|
| 1030 | - * |
|
| 1031 | - * @param \Traversable $source |
|
| 1032 | - * @return self |
|
| 1033 | - */ |
|
| 1034 | - public function concat($source) |
|
| 1035 | - { |
|
| 1036 | - $result = new static($this); |
|
| 1037 | - |
|
| 1038 | - foreach ($source as $item) { |
|
| 1039 | - $result->push($item); |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - return $result; |
|
| 1043 | - } |
|
| 1044 | - |
|
| 1045 | - /** |
|
| 1046 | - * Get and remove an item from the collection. |
|
| 1047 | - * |
|
| 1048 | - * @param mixed $key |
|
| 1049 | - * @param mixed $default |
|
| 1050 | - * @return mixed |
|
| 1051 | - */ |
|
| 1052 | - public function pull($key, $default = null) |
|
| 1053 | - { |
|
| 1054 | - return Arr::pull($this->items, $key, $default); |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - /** |
|
| 1058 | - * Put an item in the collection by key. |
|
| 1059 | - * |
|
| 1060 | - * @param mixed $key |
|
| 1061 | - * @param mixed $value |
|
| 1062 | - * @return $this |
|
| 1063 | - */ |
|
| 1064 | - public function put($key, $value) |
|
| 1065 | - { |
|
| 1066 | - $this->offsetSet($key, $value); |
|
| 1067 | - |
|
| 1068 | - return $this; |
|
| 1069 | - } |
|
| 1070 | - |
|
| 1071 | - /** |
|
| 1072 | - * Get one or a specified number of items randomly from the collection. |
|
| 1073 | - * |
|
| 1074 | - * @param int|null $number |
|
| 1075 | - * @return mixed |
|
| 1076 | - * |
|
| 1077 | - * @throws \InvalidArgumentException |
|
| 1078 | - */ |
|
| 1079 | - public function random($number = null) |
|
| 1080 | - { |
|
| 1081 | - if (is_null($number)) { |
|
| 1082 | - return Arr::random($this->items); |
|
| 1083 | - } |
|
| 1084 | - |
|
| 1085 | - return new static(Arr::random($this->items, $number)); |
|
| 1086 | - } |
|
| 1087 | - |
|
| 1088 | - /** |
|
| 1089 | - * Reduce the collection to a single value. |
|
| 1090 | - * |
|
| 1091 | - * @param callable $callback |
|
| 1092 | - * @param mixed $initial |
|
| 1093 | - * @return mixed |
|
| 1094 | - */ |
|
| 1095 | - public function reduce(callable $callback, $initial = null) |
|
| 1096 | - { |
|
| 1097 | - return array_reduce($this->items, $callback, $initial); |
|
| 1098 | - } |
|
| 1099 | - |
|
| 1100 | - /** |
|
| 1101 | - * Create a collection of all elements that do not pass a given truth test. |
|
| 1102 | - * |
|
| 1103 | - * @param callable|mixed $callback |
|
| 1104 | - * @return static |
|
| 1105 | - */ |
|
| 1106 | - public function reject($callback) |
|
| 1107 | - { |
|
| 1108 | - if ($this->useAsCallable($callback)) { |
|
| 1109 | - return $this->filter(function ($value, $key) use ($callback) { |
|
| 1110 | - return ! $callback($value, $key); |
|
| 1111 | - }); |
|
| 1112 | - } |
|
| 1113 | - |
|
| 1114 | - return $this->filter(function ($item) use ($callback) { |
|
| 1115 | - return $item != $callback; |
|
| 1116 | - }); |
|
| 1117 | - } |
|
| 1118 | - |
|
| 1119 | - /** |
|
| 1120 | - * Reverse items order. |
|
| 1121 | - * |
|
| 1122 | - * @return static |
|
| 1123 | - */ |
|
| 1124 | - public function reverse() |
|
| 1125 | - { |
|
| 1126 | - return new static(array_reverse($this->items, true)); |
|
| 1127 | - } |
|
| 1128 | - |
|
| 1129 | - /** |
|
| 1130 | - * Search the collection for a given value and return the corresponding key if successful. |
|
| 1131 | - * |
|
| 1132 | - * @param mixed $value |
|
| 1133 | - * @param bool $strict |
|
| 1134 | - * @return mixed |
|
| 1135 | - */ |
|
| 1136 | - public function search($value, $strict = false) |
|
| 1137 | - { |
|
| 1138 | - if (! $this->useAsCallable($value)) { |
|
| 1139 | - return array_search($value, $this->items, $strict); |
|
| 1140 | - } |
|
| 1141 | - |
|
| 1142 | - foreach ($this->items as $key => $item) { |
|
| 1143 | - if (call_user_func($value, $item, $key)) { |
|
| 1144 | - return $key; |
|
| 1145 | - } |
|
| 1146 | - } |
|
| 1147 | - |
|
| 1148 | - return false; |
|
| 1149 | - } |
|
| 1150 | - |
|
| 1151 | - /** |
|
| 1152 | - * Get and remove the first item from the collection. |
|
| 1153 | - * |
|
| 1154 | - * @return mixed |
|
| 1155 | - */ |
|
| 1156 | - public function shift() |
|
| 1157 | - { |
|
| 1158 | - return array_shift($this->items); |
|
| 1159 | - } |
|
| 1160 | - |
|
| 1161 | - /** |
|
| 1162 | - * Shuffle the items in the collection. |
|
| 1163 | - * |
|
| 1164 | - * @param int $seed |
|
| 1165 | - * @return static |
|
| 1166 | - */ |
|
| 1167 | - public function shuffle($seed = null) |
|
| 1168 | - { |
|
| 1169 | - $items = $this->items; |
|
| 1170 | - |
|
| 1171 | - if (is_null($seed)) { |
|
| 1172 | - shuffle($items); |
|
| 1173 | - } else { |
|
| 1174 | - srand($seed); |
|
| 1175 | - |
|
| 1176 | - usort($items, function () { |
|
| 1177 | - return rand(-1, 1); |
|
| 1178 | - }); |
|
| 1179 | - } |
|
| 1180 | - |
|
| 1181 | - return new static($items); |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - /** |
|
| 1185 | - * Slice the underlying collection array. |
|
| 1186 | - * |
|
| 1187 | - * @param int $offset |
|
| 1188 | - * @param int $length |
|
| 1189 | - * @return static |
|
| 1190 | - */ |
|
| 1191 | - public function slice($offset, $length = null) |
|
| 1192 | - { |
|
| 1193 | - return new static(array_slice($this->items, $offset, $length, true)); |
|
| 1194 | - } |
|
| 1195 | - |
|
| 1196 | - /** |
|
| 1197 | - * Split a collection into a certain number of groups. |
|
| 1198 | - * |
|
| 1199 | - * @param int $numberOfGroups |
|
| 1200 | - * @return static |
|
| 1201 | - */ |
|
| 1202 | - public function split($numberOfGroups) |
|
| 1203 | - { |
|
| 1204 | - if ($this->isEmpty()) { |
|
| 1205 | - return new static; |
|
| 1206 | - } |
|
| 1207 | - |
|
| 1208 | - $groupSize = ceil($this->count() / $numberOfGroups); |
|
| 1209 | - |
|
| 1210 | - return $this->chunk($groupSize); |
|
| 1211 | - } |
|
| 1212 | - |
|
| 1213 | - /** |
|
| 1214 | - * Chunk the underlying collection array. |
|
| 1215 | - * |
|
| 1216 | - * @param int $size |
|
| 1217 | - * @return static |
|
| 1218 | - */ |
|
| 1219 | - public function chunk($size) |
|
| 1220 | - { |
|
| 1221 | - if ($size <= 0) { |
|
| 1222 | - return new static; |
|
| 1223 | - } |
|
| 1224 | - |
|
| 1225 | - $chunks = []; |
|
| 1226 | - |
|
| 1227 | - foreach (array_chunk($this->items, $size, true) as $chunk) { |
|
| 1228 | - $chunks[] = new static($chunk); |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - return new static($chunks); |
|
| 1232 | - } |
|
| 1233 | - |
|
| 1234 | - /** |
|
| 1235 | - * Sort through each item with a callback. |
|
| 1236 | - * |
|
| 1237 | - * @param callable|null $callback |
|
| 1238 | - * @return static |
|
| 1239 | - */ |
|
| 1240 | - public function sort(callable $callback = null) |
|
| 1241 | - { |
|
| 1242 | - $items = $this->items; |
|
| 1243 | - |
|
| 1244 | - $callback |
|
| 1245 | - ? uasort($items, $callback) |
|
| 1246 | - : asort($items); |
|
| 1247 | - |
|
| 1248 | - return new static($items); |
|
| 1249 | - } |
|
| 1250 | - |
|
| 1251 | - /** |
|
| 1252 | - * Sort the collection using the given callback. |
|
| 1253 | - * |
|
| 1254 | - * @param callable|string $callback |
|
| 1255 | - * @param int $options |
|
| 1256 | - * @param bool $descending |
|
| 1257 | - * @return static |
|
| 1258 | - */ |
|
| 1259 | - public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
| 1260 | - { |
|
| 1261 | - $results = []; |
|
| 1262 | - |
|
| 1263 | - $callback = $this->valueRetriever($callback); |
|
| 1264 | - |
|
| 1265 | - // First we will loop through the items and get the comparator from a callback |
|
| 1266 | - // function which we were given. Then, we will sort the returned values and |
|
| 1267 | - // and grab the corresponding values for the sorted keys from this array. |
|
| 1268 | - foreach ($this->items as $key => $value) { |
|
| 1269 | - $results[$key] = $callback($value, $key); |
|
| 1270 | - } |
|
| 1271 | - |
|
| 1272 | - $descending ? arsort($results, $options) |
|
| 1273 | - : asort($results, $options); |
|
| 1274 | - |
|
| 1275 | - // Once we have sorted all of the keys in the array, we will loop through them |
|
| 1276 | - // and grab the corresponding model so we can set the underlying items list |
|
| 1277 | - // to the sorted version. Then we'll just return the collection instance. |
|
| 1278 | - foreach (array_keys($results) as $key) { |
|
| 1279 | - $results[$key] = $this->items[$key]; |
|
| 1280 | - } |
|
| 1281 | - |
|
| 1282 | - return new static($results); |
|
| 1283 | - } |
|
| 1284 | - |
|
| 1285 | - /** |
|
| 1286 | - * Sort the collection in descending order using the given callback. |
|
| 1287 | - * |
|
| 1288 | - * @param callable|string $callback |
|
| 1289 | - * @param int $options |
|
| 1290 | - * @return static |
|
| 1291 | - */ |
|
| 1292 | - public function sortByDesc($callback, $options = SORT_REGULAR) |
|
| 1293 | - { |
|
| 1294 | - return $this->sortBy($callback, $options, true); |
|
| 1295 | - } |
|
| 1296 | - |
|
| 1297 | - /** |
|
| 1298 | - * Splice a portion of the underlying collection array. |
|
| 1299 | - * |
|
| 1300 | - * @param int $offset |
|
| 1301 | - * @param int|null $length |
|
| 1302 | - * @param mixed $replacement |
|
| 1303 | - * @return static |
|
| 1304 | - */ |
|
| 1305 | - public function splice($offset, $length = null, $replacement = []) |
|
| 1306 | - { |
|
| 1307 | - if (func_num_args() == 1) { |
|
| 1308 | - return new static(array_splice($this->items, $offset)); |
|
| 1309 | - } |
|
| 1310 | - |
|
| 1311 | - return new static(array_splice($this->items, $offset, $length, $replacement)); |
|
| 1312 | - } |
|
| 1313 | - |
|
| 1314 | - /** |
|
| 1315 | - * Get the sum of the given values. |
|
| 1316 | - * |
|
| 1317 | - * @param callable|string|null $callback |
|
| 1318 | - * @return mixed |
|
| 1319 | - */ |
|
| 1320 | - public function sum($callback = null) |
|
| 1321 | - { |
|
| 1322 | - if (is_null($callback)) { |
|
| 1323 | - return array_sum($this->items); |
|
| 1324 | - } |
|
| 1325 | - |
|
| 1326 | - $callback = $this->valueRetriever($callback); |
|
| 1327 | - |
|
| 1328 | - return $this->reduce(function ($result, $item) use ($callback) { |
|
| 1329 | - return $result + $callback($item); |
|
| 1330 | - }, 0); |
|
| 1331 | - } |
|
| 1332 | - |
|
| 1333 | - /** |
|
| 1334 | - * Take the first or last {$limit} items. |
|
| 1335 | - * |
|
| 1336 | - * @param int $limit |
|
| 1337 | - * @return static |
|
| 1338 | - */ |
|
| 1339 | - public function take($limit) |
|
| 1340 | - { |
|
| 1341 | - if ($limit < 0) { |
|
| 1342 | - return $this->slice($limit, abs($limit)); |
|
| 1343 | - } |
|
| 1344 | - |
|
| 1345 | - return $this->slice(0, $limit); |
|
| 1346 | - } |
|
| 1347 | - |
|
| 1348 | - /** |
|
| 1349 | - * Pass the collection to the given callback and then return it. |
|
| 1350 | - * |
|
| 1351 | - * @param callable $callback |
|
| 1352 | - * @return $this |
|
| 1353 | - */ |
|
| 1354 | - public function tap(callable $callback) |
|
| 1355 | - { |
|
| 1356 | - $callback(new static($this->items)); |
|
| 1357 | - |
|
| 1358 | - return $this; |
|
| 1359 | - } |
|
| 1360 | - |
|
| 1361 | - /** |
|
| 1362 | - * Transform each item in the collection using a callback. |
|
| 1363 | - * |
|
| 1364 | - * @param callable $callback |
|
| 1365 | - * @return $this |
|
| 1366 | - */ |
|
| 1367 | - public function transform(callable $callback) |
|
| 1368 | - { |
|
| 1369 | - $this->items = $this->map($callback)->all(); |
|
| 1370 | - |
|
| 1371 | - return $this; |
|
| 1372 | - } |
|
| 1373 | - |
|
| 1374 | - /** |
|
| 1375 | - * Return only unique items from the collection array. |
|
| 1376 | - * |
|
| 1377 | - * @param string|callable|null $key |
|
| 1378 | - * @param bool $strict |
|
| 1379 | - * @return static |
|
| 1380 | - */ |
|
| 1381 | - public function unique($key = null, $strict = false) |
|
| 1382 | - { |
|
| 1383 | - if (is_null($key)) { |
|
| 1384 | - return new static(array_unique($this->items, SORT_REGULAR)); |
|
| 1385 | - } |
|
| 1386 | - |
|
| 1387 | - $callback = $this->valueRetriever($key); |
|
| 1388 | - |
|
| 1389 | - $exists = []; |
|
| 1390 | - |
|
| 1391 | - return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) { |
|
| 1392 | - if (in_array($id = $callback($item, $key), $exists, $strict)) { |
|
| 1393 | - return true; |
|
| 1394 | - } |
|
| 1395 | - |
|
| 1396 | - $exists[] = $id; |
|
| 1397 | - }); |
|
| 1398 | - } |
|
| 1399 | - |
|
| 1400 | - /** |
|
| 1401 | - * Return only unique items from the collection array using strict comparison. |
|
| 1402 | - * |
|
| 1403 | - * @param string|callable|null $key |
|
| 1404 | - * @return static |
|
| 1405 | - */ |
|
| 1406 | - public function uniqueStrict($key = null) |
|
| 1407 | - { |
|
| 1408 | - return $this->unique($key, true); |
|
| 1409 | - } |
|
| 1410 | - |
|
| 1411 | - /** |
|
| 1412 | - * Reset the keys on the underlying array. |
|
| 1413 | - * |
|
| 1414 | - * @return static |
|
| 1415 | - */ |
|
| 1416 | - public function values() |
|
| 1417 | - { |
|
| 1418 | - return new static(array_values($this->items)); |
|
| 1419 | - } |
|
| 1420 | - |
|
| 1421 | - /** |
|
| 1422 | - * Get a value retrieving callback. |
|
| 1423 | - * |
|
| 1424 | - * @param string $value |
|
| 1425 | - * @return callable |
|
| 1426 | - */ |
|
| 1427 | - protected function valueRetriever($value) |
|
| 1428 | - { |
|
| 1429 | - if ($this->useAsCallable($value)) { |
|
| 1430 | - return $value; |
|
| 1431 | - } |
|
| 1432 | - |
|
| 1433 | - return function ($item) use ($value) { |
|
| 1434 | - return data_get($item, $value); |
|
| 1435 | - }; |
|
| 1436 | - } |
|
| 1437 | - |
|
| 1438 | - /** |
|
| 1439 | - * Zip the collection together with one or more arrays. |
|
| 1440 | - * |
|
| 1441 | - * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
|
| 1442 | - * => [[1, 4], [2, 5], [3, 6]] |
|
| 1443 | - * |
|
| 1444 | - * @param mixed ...$items |
|
| 1445 | - * @return static |
|
| 1446 | - */ |
|
| 1447 | - public function zip($items) |
|
| 1448 | - { |
|
| 1449 | - $arrayableItems = array_map(function ($items) { |
|
| 1450 | - return $this->getArrayableItems($items); |
|
| 1451 | - }, func_get_args()); |
|
| 1452 | - |
|
| 1453 | - $params = array_merge([function () { |
|
| 1454 | - return new static(func_get_args()); |
|
| 1455 | - }, $this->items], $arrayableItems); |
|
| 1456 | - |
|
| 1457 | - return new static(call_user_func_array('array_map', $params)); |
|
| 1458 | - } |
|
| 1459 | - |
|
| 1460 | - /** |
|
| 1461 | - * Get the collection of items as a plain array. |
|
| 1462 | - * |
|
| 1463 | - * @return array |
|
| 1464 | - */ |
|
| 1465 | - public function toArray() |
|
| 1466 | - { |
|
| 1467 | - return array_map(function ($value) { |
|
| 1468 | - return $value instanceof Arrayable ? $value->toArray() : $value; |
|
| 1469 | - }, $this->items); |
|
| 1470 | - } |
|
| 1471 | - |
|
| 1472 | - /** |
|
| 1473 | - * Convert the object into something JSON serializable. |
|
| 1474 | - * |
|
| 1475 | - * @return array |
|
| 1476 | - */ |
|
| 1477 | - public function jsonSerialize() |
|
| 1478 | - { |
|
| 1479 | - return array_map(function ($value) { |
|
| 1480 | - if ($value instanceof JsonSerializable) { |
|
| 1481 | - return $value->jsonSerialize(); |
|
| 1482 | - } elseif ($value instanceof Jsonable) { |
|
| 1483 | - return json_decode($value->toJson(), true); |
|
| 1484 | - } elseif ($value instanceof Arrayable) { |
|
| 1485 | - return $value->toArray(); |
|
| 1486 | - } else { |
|
| 1487 | - return $value; |
|
| 1488 | - } |
|
| 1489 | - }, $this->items); |
|
| 1490 | - } |
|
| 1491 | - |
|
| 1492 | - /** |
|
| 1493 | - * Get the collection of items as JSON. |
|
| 1494 | - * |
|
| 1495 | - * @param int $options |
|
| 1496 | - * @return string |
|
| 1497 | - */ |
|
| 1498 | - public function toJson($options = 0) |
|
| 1499 | - { |
|
| 1500 | - return json_encode($this->jsonSerialize(), $options); |
|
| 1501 | - } |
|
| 1502 | - |
|
| 1503 | - /** |
|
| 1504 | - * Get an iterator for the items. |
|
| 1505 | - * |
|
| 1506 | - * @return \ArrayIterator |
|
| 1507 | - */ |
|
| 1508 | - public function getIterator() |
|
| 1509 | - { |
|
| 1510 | - return new ArrayIterator($this->items); |
|
| 1511 | - } |
|
| 1512 | - |
|
| 1513 | - /** |
|
| 1514 | - * Get a CachingIterator instance. |
|
| 1515 | - * |
|
| 1516 | - * @param int $flags |
|
| 1517 | - * @return \CachingIterator |
|
| 1518 | - */ |
|
| 1519 | - public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
| 1520 | - { |
|
| 1521 | - return new CachingIterator($this->getIterator(), $flags); |
|
| 1522 | - } |
|
| 1523 | - |
|
| 1524 | - /** |
|
| 1525 | - * Count the number of items in the collection. |
|
| 1526 | - * |
|
| 1527 | - * @return int |
|
| 1528 | - */ |
|
| 1529 | - public function count() |
|
| 1530 | - { |
|
| 1531 | - return count($this->items); |
|
| 1532 | - } |
|
| 1533 | - |
|
| 1534 | - /** |
|
| 1535 | - * Get a base Support collection instance from this collection. |
|
| 1536 | - * |
|
| 1537 | - * @return \Illuminate\Support\Collection |
|
| 1538 | - */ |
|
| 1539 | - public function toBase() |
|
| 1540 | - { |
|
| 1541 | - return new self($this); |
|
| 1542 | - } |
|
| 1543 | - |
|
| 1544 | - /** |
|
| 1545 | - * Determine if an item exists at an offset. |
|
| 1546 | - * |
|
| 1547 | - * @param mixed $key |
|
| 1548 | - * @return bool |
|
| 1549 | - */ |
|
| 1550 | - public function offsetExists($key) |
|
| 1551 | - { |
|
| 1552 | - return array_key_exists($key, $this->items); |
|
| 1553 | - } |
|
| 1554 | - |
|
| 1555 | - /** |
|
| 1556 | - * Get an item at a given offset. |
|
| 1557 | - * |
|
| 1558 | - * @param mixed $key |
|
| 1559 | - * @return mixed |
|
| 1560 | - */ |
|
| 1561 | - public function offsetGet($key) |
|
| 1562 | - { |
|
| 1563 | - return $this->items[$key]; |
|
| 1564 | - } |
|
| 1565 | - |
|
| 1566 | - /** |
|
| 1567 | - * Set the item at a given offset. |
|
| 1568 | - * |
|
| 1569 | - * @param mixed $key |
|
| 1570 | - * @param mixed $value |
|
| 1571 | - * @return void |
|
| 1572 | - */ |
|
| 1573 | - public function offsetSet($key, $value) |
|
| 1574 | - { |
|
| 1575 | - if (is_null($key)) { |
|
| 1576 | - $this->items[] = $value; |
|
| 1577 | - } else { |
|
| 1578 | - $this->items[$key] = $value; |
|
| 1579 | - } |
|
| 1580 | - } |
|
| 1581 | - |
|
| 1582 | - /** |
|
| 1583 | - * Unset the item at a given offset. |
|
| 1584 | - * |
|
| 1585 | - * @param string $key |
|
| 1586 | - * @return void |
|
| 1587 | - */ |
|
| 1588 | - public function offsetUnset($key) |
|
| 1589 | - { |
|
| 1590 | - unset($this->items[$key]); |
|
| 1591 | - } |
|
| 1592 | - |
|
| 1593 | - /** |
|
| 1594 | - * Convert the collection to its string representation. |
|
| 1595 | - * |
|
| 1596 | - * @return string |
|
| 1597 | - */ |
|
| 1598 | - public function __toString() |
|
| 1599 | - { |
|
| 1600 | - return $this->toJson(); |
|
| 1601 | - } |
|
| 1602 | - |
|
| 1603 | - /** |
|
| 1604 | - * Results array of items from Collection or Arrayable. |
|
| 1605 | - * |
|
| 1606 | - * @param mixed $items |
|
| 1607 | - * @return array |
|
| 1608 | - */ |
|
| 1609 | - protected function getArrayableItems($items) |
|
| 1610 | - { |
|
| 1611 | - if (is_array($items)) { |
|
| 1612 | - return $items; |
|
| 1613 | - } elseif ($items instanceof self) { |
|
| 1614 | - return $items->all(); |
|
| 1615 | - } elseif ($items instanceof Arrayable) { |
|
| 1616 | - return $items->toArray(); |
|
| 1617 | - } elseif ($items instanceof Jsonable) { |
|
| 1618 | - return json_decode($items->toJson(), true); |
|
| 1619 | - } elseif ($items instanceof JsonSerializable) { |
|
| 1620 | - return $items->jsonSerialize(); |
|
| 1621 | - } elseif ($items instanceof Traversable) { |
|
| 1622 | - return iterator_to_array($items); |
|
| 1623 | - } |
|
| 1624 | - |
|
| 1625 | - return (array) $items; |
|
| 1626 | - } |
|
| 1627 | - |
|
| 1628 | - /** |
|
| 1629 | - * Add a method to the list of proxied methods. |
|
| 1630 | - * |
|
| 1631 | - * @param string $method |
|
| 1632 | - * @return void |
|
| 1633 | - */ |
|
| 1634 | - public static function proxy($method) |
|
| 1635 | - { |
|
| 1636 | - static::$proxies[] = $method; |
|
| 1637 | - } |
|
| 1638 | - |
|
| 1639 | - /** |
|
| 1640 | - * Dynamically access collection proxies. |
|
| 1641 | - * |
|
| 1642 | - * @param string $key |
|
| 1643 | - * @return mixed |
|
| 1644 | - * |
|
| 1645 | - * @throws \Exception |
|
| 1646 | - */ |
|
| 1647 | - public function __get($key) |
|
| 1648 | - { |
|
| 1649 | - if (! in_array($key, static::$proxies)) { |
|
| 1650 | - throw new Exception("Property [{$key}] does not exist on this collection instance."); |
|
| 1651 | - } |
|
| 1652 | - |
|
| 1653 | - return new HigherOrderCollectionProxy($this, $key); |
|
| 1654 | - } |
|
| 19 | + use Macroable; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * The items contained in the collection. |
|
| 23 | + * |
|
| 24 | + * @var array |
|
| 25 | + */ |
|
| 26 | + protected $items = []; |
|
| 27 | + |
|
| 28 | + /** |
|
| 29 | + * The methods that can be proxied. |
|
| 30 | + * |
|
| 31 | + * @var array |
|
| 32 | + */ |
|
| 33 | + protected static $proxies = [ |
|
| 34 | + 'average', 'avg', 'contains', 'each', 'every', 'filter', 'first', 'flatMap', |
|
| 35 | + 'map', 'partition', 'reject', 'sortBy', 'sortByDesc', 'sum', |
|
| 36 | + ]; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * Create a new collection. |
|
| 40 | + * |
|
| 41 | + * @param mixed $items |
|
| 42 | + * @return void |
|
| 43 | + */ |
|
| 44 | + public function __construct($items = []) |
|
| 45 | + { |
|
| 46 | + $this->items = $this->getArrayableItems($items); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Create a new collection instance if the value isn't one already. |
|
| 51 | + * |
|
| 52 | + * @param mixed $items |
|
| 53 | + * @return static |
|
| 54 | + */ |
|
| 55 | + public static function make($items = []) |
|
| 56 | + { |
|
| 57 | + return new static($items); |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Create a new collection by invoking the callback a given number of times. |
|
| 62 | + * |
|
| 63 | + * @param int $number |
|
| 64 | + * @param callable $callback |
|
| 65 | + * @return static |
|
| 66 | + */ |
|
| 67 | + public static function times($number, callable $callback = null) |
|
| 68 | + { |
|
| 69 | + if ($number < 1) { |
|
| 70 | + return new static; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + if (is_null($callback)) { |
|
| 74 | + return new static(range(1, $number)); |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + return (new static(range(1, $number)))->map($callback); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * Get all of the items in the collection. |
|
| 82 | + * |
|
| 83 | + * @return array |
|
| 84 | + */ |
|
| 85 | + public function all() |
|
| 86 | + { |
|
| 87 | + return $this->items; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get the average value of a given key. |
|
| 92 | + * |
|
| 93 | + * @param callable|string|null $callback |
|
| 94 | + * @return mixed |
|
| 95 | + */ |
|
| 96 | + public function avg($callback = null) |
|
| 97 | + { |
|
| 98 | + if ($count = $this->count()) { |
|
| 99 | + return $this->sum($callback) / $count; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Alias for the "avg" method. |
|
| 105 | + * |
|
| 106 | + * @param callable|string|null $callback |
|
| 107 | + * @return mixed |
|
| 108 | + */ |
|
| 109 | + public function average($callback = null) |
|
| 110 | + { |
|
| 111 | + return $this->avg($callback); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the median of a given key. |
|
| 116 | + * |
|
| 117 | + * @param null $key |
|
| 118 | + * @return mixed |
|
| 119 | + */ |
|
| 120 | + public function median($key = null) |
|
| 121 | + { |
|
| 122 | + $count = $this->count(); |
|
| 123 | + |
|
| 124 | + if ($count == 0) { |
|
| 125 | + return; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + $values = with(isset($key) ? $this->pluck($key) : $this) |
|
| 129 | + ->sort()->values(); |
|
| 130 | + |
|
| 131 | + $middle = (int) ($count / 2); |
|
| 132 | + |
|
| 133 | + if ($count % 2) { |
|
| 134 | + return $values->get($middle); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + return (new static([ |
|
| 138 | + $values->get($middle - 1), $values->get($middle), |
|
| 139 | + ]))->average(); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * Get the mode of a given key. |
|
| 144 | + * |
|
| 145 | + * @param mixed $key |
|
| 146 | + * @return array|null |
|
| 147 | + */ |
|
| 148 | + public function mode($key = null) |
|
| 149 | + { |
|
| 150 | + $count = $this->count(); |
|
| 151 | + |
|
| 152 | + if ($count == 0) { |
|
| 153 | + return; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + $collection = isset($key) ? $this->pluck($key) : $this; |
|
| 157 | + |
|
| 158 | + $counts = new self; |
|
| 159 | + |
|
| 160 | + $collection->each(function ($value) use ($counts) { |
|
| 161 | + $counts[$value] = isset($counts[$value]) ? $counts[$value] + 1 : 1; |
|
| 162 | + }); |
|
| 163 | + |
|
| 164 | + $sorted = $counts->sort(); |
|
| 165 | + |
|
| 166 | + $highestValue = $sorted->last(); |
|
| 167 | + |
|
| 168 | + return $sorted->filter(function ($value) use ($highestValue) { |
|
| 169 | + return $value == $highestValue; |
|
| 170 | + })->sort()->keys()->all(); |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * Collapse the collection of items into a single array. |
|
| 175 | + * |
|
| 176 | + * @return static |
|
| 177 | + */ |
|
| 178 | + public function collapse() |
|
| 179 | + { |
|
| 180 | + return new static(Arr::collapse($this->items)); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Determine if an item exists in the collection. |
|
| 185 | + * |
|
| 186 | + * @param mixed $key |
|
| 187 | + * @param mixed $operator |
|
| 188 | + * @param mixed $value |
|
| 189 | + * @return bool |
|
| 190 | + */ |
|
| 191 | + public function contains($key, $operator = null, $value = null) |
|
| 192 | + { |
|
| 193 | + if (func_num_args() == 1) { |
|
| 194 | + if ($this->useAsCallable($key)) { |
|
| 195 | + return ! is_null($this->first($key)); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + return in_array($key, $this->items); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + if (func_num_args() == 2) { |
|
| 202 | + $value = $operator; |
|
| 203 | + |
|
| 204 | + $operator = '='; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + return $this->contains($this->operatorForWhere($key, $operator, $value)); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + /** |
|
| 211 | + * Determine if an item exists in the collection using strict comparison. |
|
| 212 | + * |
|
| 213 | + * @param mixed $key |
|
| 214 | + * @param mixed $value |
|
| 215 | + * @return bool |
|
| 216 | + */ |
|
| 217 | + public function containsStrict($key, $value = null) |
|
| 218 | + { |
|
| 219 | + if (func_num_args() == 2) { |
|
| 220 | + return $this->contains(function ($item) use ($key, $value) { |
|
| 221 | + return data_get($item, $key) === $value; |
|
| 222 | + }); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + if ($this->useAsCallable($key)) { |
|
| 226 | + return ! is_null($this->first($key)); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return in_array($key, $this->items, true); |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * Cross join with the given lists, returning all possible permutations. |
|
| 234 | + * |
|
| 235 | + * @param mixed ...$lists |
|
| 236 | + * @return static |
|
| 237 | + */ |
|
| 238 | + public function crossJoin(...$lists) |
|
| 239 | + { |
|
| 240 | + return new static(Arr::crossJoin( |
|
| 241 | + $this->items, ...array_map([$this, 'getArrayableItems'], $lists) |
|
| 242 | + )); |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Get the items in the collection that are not present in the given items. |
|
| 247 | + * |
|
| 248 | + * @param mixed $items |
|
| 249 | + * @return static |
|
| 250 | + */ |
|
| 251 | + public function diff($items) |
|
| 252 | + { |
|
| 253 | + return new static(array_diff($this->items, $this->getArrayableItems($items))); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Get the items in the collection whose keys and values are not present in the given items. |
|
| 258 | + * |
|
| 259 | + * @param mixed $items |
|
| 260 | + * @return static |
|
| 261 | + */ |
|
| 262 | + public function diffAssoc($items) |
|
| 263 | + { |
|
| 264 | + return new static(array_diff_assoc($this->items, $this->getArrayableItems($items))); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + /** |
|
| 268 | + * Get the items in the collection whose keys are not present in the given items. |
|
| 269 | + * |
|
| 270 | + * @param mixed $items |
|
| 271 | + * @return static |
|
| 272 | + */ |
|
| 273 | + public function diffKeys($items) |
|
| 274 | + { |
|
| 275 | + return new static(array_diff_key($this->items, $this->getArrayableItems($items))); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Execute a callback over each item. |
|
| 280 | + * |
|
| 281 | + * @param callable $callback |
|
| 282 | + * @return $this |
|
| 283 | + */ |
|
| 284 | + public function each(callable $callback) |
|
| 285 | + { |
|
| 286 | + foreach ($this->items as $key => $item) { |
|
| 287 | + if ($callback($item, $key) === false) { |
|
| 288 | + break; |
|
| 289 | + } |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + return $this; |
|
| 293 | + } |
|
| 294 | + |
|
| 295 | + /** |
|
| 296 | + * Execute a callback over each nested chunk of items. |
|
| 297 | + * |
|
| 298 | + * @param callable $callback |
|
| 299 | + * @return static |
|
| 300 | + */ |
|
| 301 | + public function eachSpread(callable $callback) |
|
| 302 | + { |
|
| 303 | + return $this->each(function ($chunk) use ($callback) { |
|
| 304 | + return $callback(...$chunk); |
|
| 305 | + }); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + /** |
|
| 309 | + * Determine if all items in the collection pass the given test. |
|
| 310 | + * |
|
| 311 | + * @param string|callable $key |
|
| 312 | + * @param mixed $operator |
|
| 313 | + * @param mixed $value |
|
| 314 | + * @return bool |
|
| 315 | + */ |
|
| 316 | + public function every($key, $operator = null, $value = null) |
|
| 317 | + { |
|
| 318 | + if (func_num_args() == 1) { |
|
| 319 | + $callback = $this->valueRetriever($key); |
|
| 320 | + |
|
| 321 | + foreach ($this->items as $k => $v) { |
|
| 322 | + if (! $callback($v, $k)) { |
|
| 323 | + return false; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + |
|
| 327 | + return true; |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + if (func_num_args() == 2) { |
|
| 331 | + $value = $operator; |
|
| 332 | + |
|
| 333 | + $operator = '='; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + return $this->every($this->operatorForWhere($key, $operator, $value)); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Get all items except for those with the specified keys. |
|
| 341 | + * |
|
| 342 | + * @param mixed $keys |
|
| 343 | + * @return static |
|
| 344 | + */ |
|
| 345 | + public function except($keys) |
|
| 346 | + { |
|
| 347 | + $keys = is_array($keys) ? $keys : func_get_args(); |
|
| 348 | + |
|
| 349 | + return new static(Arr::except($this->items, $keys)); |
|
| 350 | + } |
|
| 351 | + |
|
| 352 | + /** |
|
| 353 | + * Run a filter over each of the items. |
|
| 354 | + * |
|
| 355 | + * @param callable|null $callback |
|
| 356 | + * @return static |
|
| 357 | + */ |
|
| 358 | + public function filter(callable $callback = null) |
|
| 359 | + { |
|
| 360 | + if ($callback) { |
|
| 361 | + return new static(Arr::where($this->items, $callback)); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + return new static(array_filter($this->items)); |
|
| 365 | + } |
|
| 366 | + |
|
| 367 | + /** |
|
| 368 | + * Apply the callback if the value is truthy. |
|
| 369 | + * |
|
| 370 | + * @param bool $value |
|
| 371 | + * @param callable $callback |
|
| 372 | + * @param callable $default |
|
| 373 | + * @return mixed |
|
| 374 | + */ |
|
| 375 | + public function when($value, callable $callback, callable $default = null) |
|
| 376 | + { |
|
| 377 | + if ($value) { |
|
| 378 | + return $callback($this); |
|
| 379 | + } elseif ($default) { |
|
| 380 | + return $default($this); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + return $this; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + /** |
|
| 387 | + * Apply the callback if the value is falsy. |
|
| 388 | + * |
|
| 389 | + * @param bool $value |
|
| 390 | + * @param callable $callback |
|
| 391 | + * @param callable $default |
|
| 392 | + * @return mixed |
|
| 393 | + */ |
|
| 394 | + public function unless($value, callable $callback, callable $default = null) |
|
| 395 | + { |
|
| 396 | + return $this->when(! $value, $callback, $default); |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Filter items by the given key value pair. |
|
| 401 | + * |
|
| 402 | + * @param string $key |
|
| 403 | + * @param mixed $operator |
|
| 404 | + * @param mixed $value |
|
| 405 | + * @return static |
|
| 406 | + */ |
|
| 407 | + public function where($key, $operator, $value = null) |
|
| 408 | + { |
|
| 409 | + if (func_num_args() == 2) { |
|
| 410 | + $value = $operator; |
|
| 411 | + |
|
| 412 | + $operator = '='; |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + return $this->filter($this->operatorForWhere($key, $operator, $value)); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * Get an operator checker callback. |
|
| 420 | + * |
|
| 421 | + * @param string $key |
|
| 422 | + * @param string $operator |
|
| 423 | + * @param mixed $value |
|
| 424 | + * @return \Closure |
|
| 425 | + */ |
|
| 426 | + protected function operatorForWhere($key, $operator, $value) |
|
| 427 | + { |
|
| 428 | + return function ($item) use ($key, $operator, $value) { |
|
| 429 | + $retrieved = data_get($item, $key); |
|
| 430 | + |
|
| 431 | + switch ($operator) { |
|
| 432 | + default: |
|
| 433 | + case '=': |
|
| 434 | + case '==': return $retrieved == $value; |
|
| 435 | + case '!=': |
|
| 436 | + case '<>': return $retrieved != $value; |
|
| 437 | + case '<': return $retrieved < $value; |
|
| 438 | + case '>': return $retrieved > $value; |
|
| 439 | + case '<=': return $retrieved <= $value; |
|
| 440 | + case '>=': return $retrieved >= $value; |
|
| 441 | + case '===': return $retrieved === $value; |
|
| 442 | + case '!==': return $retrieved !== $value; |
|
| 443 | + } |
|
| 444 | + }; |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + /** |
|
| 448 | + * Filter items by the given key value pair using strict comparison. |
|
| 449 | + * |
|
| 450 | + * @param string $key |
|
| 451 | + * @param mixed $value |
|
| 452 | + * @return static |
|
| 453 | + */ |
|
| 454 | + public function whereStrict($key, $value) |
|
| 455 | + { |
|
| 456 | + return $this->where($key, '===', $value); |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + /** |
|
| 460 | + * Filter items by the given key value pair. |
|
| 461 | + * |
|
| 462 | + * @param string $key |
|
| 463 | + * @param mixed $values |
|
| 464 | + * @param bool $strict |
|
| 465 | + * @return static |
|
| 466 | + */ |
|
| 467 | + public function whereIn($key, $values, $strict = false) |
|
| 468 | + { |
|
| 469 | + $values = $this->getArrayableItems($values); |
|
| 470 | + |
|
| 471 | + return $this->filter(function ($item) use ($key, $values, $strict) { |
|
| 472 | + return in_array(data_get($item, $key), $values, $strict); |
|
| 473 | + }); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + /** |
|
| 477 | + * Filter items by the given key value pair using strict comparison. |
|
| 478 | + * |
|
| 479 | + * @param string $key |
|
| 480 | + * @param mixed $values |
|
| 481 | + * @return static |
|
| 482 | + */ |
|
| 483 | + public function whereInStrict($key, $values) |
|
| 484 | + { |
|
| 485 | + return $this->whereIn($key, $values, true); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Filter items by the given key value pair. |
|
| 490 | + * |
|
| 491 | + * @param string $key |
|
| 492 | + * @param mixed $values |
|
| 493 | + * @param bool $strict |
|
| 494 | + * @return static |
|
| 495 | + */ |
|
| 496 | + public function whereNotIn($key, $values, $strict = false) |
|
| 497 | + { |
|
| 498 | + $values = $this->getArrayableItems($values); |
|
| 499 | + |
|
| 500 | + return $this->reject(function ($item) use ($key, $values, $strict) { |
|
| 501 | + return in_array(data_get($item, $key), $values, $strict); |
|
| 502 | + }); |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Filter items by the given key value pair using strict comparison. |
|
| 507 | + * |
|
| 508 | + * @param string $key |
|
| 509 | + * @param mixed $values |
|
| 510 | + * @return static |
|
| 511 | + */ |
|
| 512 | + public function whereNotInStrict($key, $values) |
|
| 513 | + { |
|
| 514 | + return $this->whereNotIn($key, $values, true); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + /** |
|
| 518 | + * Get the first item from the collection. |
|
| 519 | + * |
|
| 520 | + * @param callable|null $callback |
|
| 521 | + * @param mixed $default |
|
| 522 | + * @return mixed |
|
| 523 | + */ |
|
| 524 | + public function first(callable $callback = null, $default = null) |
|
| 525 | + { |
|
| 526 | + return Arr::first($this->items, $callback, $default); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + /** |
|
| 530 | + * Get a flattened array of the items in the collection. |
|
| 531 | + * |
|
| 532 | + * @param int $depth |
|
| 533 | + * @return static |
|
| 534 | + */ |
|
| 535 | + public function flatten($depth = INF) |
|
| 536 | + { |
|
| 537 | + return new static(Arr::flatten($this->items, $depth)); |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + /** |
|
| 541 | + * Flip the items in the collection. |
|
| 542 | + * |
|
| 543 | + * @return static |
|
| 544 | + */ |
|
| 545 | + public function flip() |
|
| 546 | + { |
|
| 547 | + return new static(array_flip($this->items)); |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + /** |
|
| 551 | + * Remove an item from the collection by key. |
|
| 552 | + * |
|
| 553 | + * @param string|array $keys |
|
| 554 | + * @return $this |
|
| 555 | + */ |
|
| 556 | + public function forget($keys) |
|
| 557 | + { |
|
| 558 | + foreach ((array) $keys as $key) { |
|
| 559 | + $this->offsetUnset($key); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + return $this; |
|
| 563 | + } |
|
| 564 | + |
|
| 565 | + /** |
|
| 566 | + * Get an item from the collection by key. |
|
| 567 | + * |
|
| 568 | + * @param mixed $key |
|
| 569 | + * @param mixed $default |
|
| 570 | + * @return mixed |
|
| 571 | + */ |
|
| 572 | + public function get($key, $default = null) |
|
| 573 | + { |
|
| 574 | + if ($this->offsetExists($key)) { |
|
| 575 | + return $this->items[$key]; |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + return value($default); |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * Group an associative array by a field or using a callback. |
|
| 583 | + * |
|
| 584 | + * @param callable|string $groupBy |
|
| 585 | + * @param bool $preserveKeys |
|
| 586 | + * @return static |
|
| 587 | + */ |
|
| 588 | + public function groupBy($groupBy, $preserveKeys = false) |
|
| 589 | + { |
|
| 590 | + $groupBy = $this->valueRetriever($groupBy); |
|
| 591 | + |
|
| 592 | + $results = []; |
|
| 593 | + |
|
| 594 | + foreach ($this->items as $key => $value) { |
|
| 595 | + $groupKeys = $groupBy($value, $key); |
|
| 596 | + |
|
| 597 | + if (! is_array($groupKeys)) { |
|
| 598 | + $groupKeys = [$groupKeys]; |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + foreach ($groupKeys as $groupKey) { |
|
| 602 | + $groupKey = is_bool($groupKey) ? (int) $groupKey : $groupKey; |
|
| 603 | + |
|
| 604 | + if (! array_key_exists($groupKey, $results)) { |
|
| 605 | + $results[$groupKey] = new static; |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + $results[$groupKey]->offsetSet($preserveKeys ? $key : null, $value); |
|
| 609 | + } |
|
| 610 | + } |
|
| 611 | + |
|
| 612 | + return new static($results); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Key an associative array by a field or using a callback. |
|
| 617 | + * |
|
| 618 | + * @param callable|string $keyBy |
|
| 619 | + * @return static |
|
| 620 | + */ |
|
| 621 | + public function keyBy($keyBy) |
|
| 622 | + { |
|
| 623 | + $keyBy = $this->valueRetriever($keyBy); |
|
| 624 | + |
|
| 625 | + $results = []; |
|
| 626 | + |
|
| 627 | + foreach ($this->items as $key => $item) { |
|
| 628 | + $resolvedKey = $keyBy($item, $key); |
|
| 629 | + |
|
| 630 | + if (is_object($resolvedKey)) { |
|
| 631 | + $resolvedKey = (string) $resolvedKey; |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + $results[$resolvedKey] = $item; |
|
| 635 | + } |
|
| 636 | + |
|
| 637 | + return new static($results); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * Determine if an item exists in the collection by key. |
|
| 642 | + * |
|
| 643 | + * @param mixed $key |
|
| 644 | + * @return bool |
|
| 645 | + */ |
|
| 646 | + public function has($key) |
|
| 647 | + { |
|
| 648 | + return $this->offsetExists($key); |
|
| 649 | + } |
|
| 650 | + |
|
| 651 | + /** |
|
| 652 | + * Concatenate values of a given key as a string. |
|
| 653 | + * |
|
| 654 | + * @param string $value |
|
| 655 | + * @param string $glue |
|
| 656 | + * @return string |
|
| 657 | + */ |
|
| 658 | + public function implode($value, $glue = null) |
|
| 659 | + { |
|
| 660 | + $first = $this->first(); |
|
| 661 | + |
|
| 662 | + if (is_array($first) || is_object($first)) { |
|
| 663 | + return implode($glue, $this->pluck($value)->all()); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + return implode($value, $this->items); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Intersect the collection with the given items. |
|
| 671 | + * |
|
| 672 | + * @param mixed $items |
|
| 673 | + * @return static |
|
| 674 | + */ |
|
| 675 | + public function intersect($items) |
|
| 676 | + { |
|
| 677 | + return new static(array_intersect($this->items, $this->getArrayableItems($items))); |
|
| 678 | + } |
|
| 679 | + |
|
| 680 | + /** |
|
| 681 | + * Intersect the collection with the given items by key. |
|
| 682 | + * |
|
| 683 | + * @param mixed $items |
|
| 684 | + * @return static |
|
| 685 | + */ |
|
| 686 | + public function intersectKey($items) |
|
| 687 | + { |
|
| 688 | + return new static(array_intersect_key($this->items, $this->getArrayableItems($items))); |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + /** |
|
| 692 | + * Determine if the collection is empty or not. |
|
| 693 | + * |
|
| 694 | + * @return bool |
|
| 695 | + */ |
|
| 696 | + public function isEmpty() |
|
| 697 | + { |
|
| 698 | + return empty($this->items); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Determine if the collection is not empty. |
|
| 703 | + * |
|
| 704 | + * @return bool |
|
| 705 | + */ |
|
| 706 | + public function isNotEmpty() |
|
| 707 | + { |
|
| 708 | + return ! $this->isEmpty(); |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + /** |
|
| 712 | + * Determine if the given value is callable, but not a string. |
|
| 713 | + * |
|
| 714 | + * @param mixed $value |
|
| 715 | + * @return bool |
|
| 716 | + */ |
|
| 717 | + protected function useAsCallable($value) |
|
| 718 | + { |
|
| 719 | + return ! is_string($value) && is_callable($value); |
|
| 720 | + } |
|
| 721 | + |
|
| 722 | + /** |
|
| 723 | + * Get the keys of the collection items. |
|
| 724 | + * |
|
| 725 | + * @return static |
|
| 726 | + */ |
|
| 727 | + public function keys() |
|
| 728 | + { |
|
| 729 | + return new static(array_keys($this->items)); |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + /** |
|
| 733 | + * Get the last item from the collection. |
|
| 734 | + * |
|
| 735 | + * @param callable|null $callback |
|
| 736 | + * @param mixed $default |
|
| 737 | + * @return mixed |
|
| 738 | + */ |
|
| 739 | + public function last(callable $callback = null, $default = null) |
|
| 740 | + { |
|
| 741 | + return Arr::last($this->items, $callback, $default); |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + /** |
|
| 745 | + * Get the values of a given key. |
|
| 746 | + * |
|
| 747 | + * @param string|array $value |
|
| 748 | + * @param string|null $key |
|
| 749 | + * @return static |
|
| 750 | + */ |
|
| 751 | + public function pluck($value, $key = null) |
|
| 752 | + { |
|
| 753 | + return new static(Arr::pluck($this->items, $value, $key)); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + /** |
|
| 757 | + * Run a map over each of the items. |
|
| 758 | + * |
|
| 759 | + * @param callable $callback |
|
| 760 | + * @return static |
|
| 761 | + */ |
|
| 762 | + public function map(callable $callback) |
|
| 763 | + { |
|
| 764 | + $keys = array_keys($this->items); |
|
| 765 | + |
|
| 766 | + $items = array_map($callback, $this->items, $keys); |
|
| 767 | + |
|
| 768 | + return new static(array_combine($keys, $items)); |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + /** |
|
| 772 | + * Run a map over each nested chunk of items. |
|
| 773 | + * |
|
| 774 | + * @param callable $callback |
|
| 775 | + * @return static |
|
| 776 | + */ |
|
| 777 | + public function mapSpread(callable $callback) |
|
| 778 | + { |
|
| 779 | + return $this->map(function ($chunk) use ($callback) { |
|
| 780 | + return $callback(...$chunk); |
|
| 781 | + }); |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + /** |
|
| 785 | + * Run a grouping map over the items. |
|
| 786 | + * |
|
| 787 | + * The callback should return an associative array with a single key/value pair. |
|
| 788 | + * |
|
| 789 | + * @param callable $callback |
|
| 790 | + * @return static |
|
| 791 | + */ |
|
| 792 | + public function mapToGroups(callable $callback) |
|
| 793 | + { |
|
| 794 | + $groups = $this->map($callback)->reduce(function ($groups, $pair) { |
|
| 795 | + $groups[key($pair)][] = reset($pair); |
|
| 796 | + |
|
| 797 | + return $groups; |
|
| 798 | + }, []); |
|
| 799 | + |
|
| 800 | + return (new static($groups))->map([$this, 'make']); |
|
| 801 | + } |
|
| 802 | + |
|
| 803 | + /** |
|
| 804 | + * Run an associative map over each of the items. |
|
| 805 | + * |
|
| 806 | + * The callback should return an associative array with a single key/value pair. |
|
| 807 | + * |
|
| 808 | + * @param callable $callback |
|
| 809 | + * @return static |
|
| 810 | + */ |
|
| 811 | + public function mapWithKeys(callable $callback) |
|
| 812 | + { |
|
| 813 | + $result = []; |
|
| 814 | + |
|
| 815 | + foreach ($this->items as $key => $value) { |
|
| 816 | + $assoc = $callback($value, $key); |
|
| 817 | + |
|
| 818 | + foreach ($assoc as $mapKey => $mapValue) { |
|
| 819 | + $result[$mapKey] = $mapValue; |
|
| 820 | + } |
|
| 821 | + } |
|
| 822 | + |
|
| 823 | + return new static($result); |
|
| 824 | + } |
|
| 825 | + |
|
| 826 | + /** |
|
| 827 | + * Map a collection and flatten the result by a single level. |
|
| 828 | + * |
|
| 829 | + * @param callable $callback |
|
| 830 | + * @return static |
|
| 831 | + */ |
|
| 832 | + public function flatMap(callable $callback) |
|
| 833 | + { |
|
| 834 | + return $this->map($callback)->collapse(); |
|
| 835 | + } |
|
| 836 | + |
|
| 837 | + /** |
|
| 838 | + * Get the max value of a given key. |
|
| 839 | + * |
|
| 840 | + * @param callable|string|null $callback |
|
| 841 | + * @return mixed |
|
| 842 | + */ |
|
| 843 | + public function max($callback = null) |
|
| 844 | + { |
|
| 845 | + $callback = $this->valueRetriever($callback); |
|
| 846 | + |
|
| 847 | + return $this->filter(function ($value) { |
|
| 848 | + return ! is_null($value); |
|
| 849 | + })->reduce(function ($result, $item) use ($callback) { |
|
| 850 | + $value = $callback($item); |
|
| 851 | + |
|
| 852 | + return is_null($result) || $value > $result ? $value : $result; |
|
| 853 | + }); |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + /** |
|
| 857 | + * Merge the collection with the given items. |
|
| 858 | + * |
|
| 859 | + * @param mixed $items |
|
| 860 | + * @return static |
|
| 861 | + */ |
|
| 862 | + public function merge($items) |
|
| 863 | + { |
|
| 864 | + return new static(array_merge($this->items, $this->getArrayableItems($items))); |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + /** |
|
| 868 | + * Create a collection by using this collection for keys and another for its values. |
|
| 869 | + * |
|
| 870 | + * @param mixed $values |
|
| 871 | + * @return static |
|
| 872 | + */ |
|
| 873 | + public function combine($values) |
|
| 874 | + { |
|
| 875 | + return new static(array_combine($this->all(), $this->getArrayableItems($values))); |
|
| 876 | + } |
|
| 877 | + |
|
| 878 | + /** |
|
| 879 | + * Union the collection with the given items. |
|
| 880 | + * |
|
| 881 | + * @param mixed $items |
|
| 882 | + * @return static |
|
| 883 | + */ |
|
| 884 | + public function union($items) |
|
| 885 | + { |
|
| 886 | + return new static($this->items + $this->getArrayableItems($items)); |
|
| 887 | + } |
|
| 888 | + |
|
| 889 | + /** |
|
| 890 | + * Get the min value of a given key. |
|
| 891 | + * |
|
| 892 | + * @param callable|string|null $callback |
|
| 893 | + * @return mixed |
|
| 894 | + */ |
|
| 895 | + public function min($callback = null) |
|
| 896 | + { |
|
| 897 | + $callback = $this->valueRetriever($callback); |
|
| 898 | + |
|
| 899 | + return $this->filter(function ($value) { |
|
| 900 | + return ! is_null($value); |
|
| 901 | + })->reduce(function ($result, $item) use ($callback) { |
|
| 902 | + $value = $callback($item); |
|
| 903 | + |
|
| 904 | + return is_null($result) || $value < $result ? $value : $result; |
|
| 905 | + }); |
|
| 906 | + } |
|
| 907 | + |
|
| 908 | + /** |
|
| 909 | + * Create a new collection consisting of every n-th element. |
|
| 910 | + * |
|
| 911 | + * @param int $step |
|
| 912 | + * @param int $offset |
|
| 913 | + * @return static |
|
| 914 | + */ |
|
| 915 | + public function nth($step, $offset = 0) |
|
| 916 | + { |
|
| 917 | + $new = []; |
|
| 918 | + |
|
| 919 | + $position = 0; |
|
| 920 | + |
|
| 921 | + foreach ($this->items as $item) { |
|
| 922 | + if ($position % $step === $offset) { |
|
| 923 | + $new[] = $item; |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + $position++; |
|
| 927 | + } |
|
| 928 | + |
|
| 929 | + return new static($new); |
|
| 930 | + } |
|
| 931 | + |
|
| 932 | + /** |
|
| 933 | + * Get the items with the specified keys. |
|
| 934 | + * |
|
| 935 | + * @param mixed $keys |
|
| 936 | + * @return static |
|
| 937 | + */ |
|
| 938 | + public function only($keys) |
|
| 939 | + { |
|
| 940 | + if (is_null($keys)) { |
|
| 941 | + return new static($this->items); |
|
| 942 | + } |
|
| 943 | + |
|
| 944 | + $keys = is_array($keys) ? $keys : func_get_args(); |
|
| 945 | + |
|
| 946 | + return new static(Arr::only($this->items, $keys)); |
|
| 947 | + } |
|
| 948 | + |
|
| 949 | + /** |
|
| 950 | + * "Paginate" the collection by slicing it into a smaller collection. |
|
| 951 | + * |
|
| 952 | + * @param int $page |
|
| 953 | + * @param int $perPage |
|
| 954 | + * @return static |
|
| 955 | + */ |
|
| 956 | + public function forPage($page, $perPage) |
|
| 957 | + { |
|
| 958 | + return $this->slice(($page - 1) * $perPage, $perPage); |
|
| 959 | + } |
|
| 960 | + |
|
| 961 | + /** |
|
| 962 | + * Partition the collection into two arrays using the given callback or key. |
|
| 963 | + * |
|
| 964 | + * @param callable|string $callback |
|
| 965 | + * @return static |
|
| 966 | + */ |
|
| 967 | + public function partition($callback) |
|
| 968 | + { |
|
| 969 | + $partitions = [new static, new static]; |
|
| 970 | + |
|
| 971 | + $callback = $this->valueRetriever($callback); |
|
| 972 | + |
|
| 973 | + foreach ($this->items as $key => $item) { |
|
| 974 | + $partitions[(int) ! $callback($item)][$key] = $item; |
|
| 975 | + } |
|
| 976 | + |
|
| 977 | + return new static($partitions); |
|
| 978 | + } |
|
| 979 | + |
|
| 980 | + /** |
|
| 981 | + * Pass the collection to the given callback and return the result. |
|
| 982 | + * |
|
| 983 | + * @param callable $callback |
|
| 984 | + * @return mixed |
|
| 985 | + */ |
|
| 986 | + public function pipe(callable $callback) |
|
| 987 | + { |
|
| 988 | + return $callback($this); |
|
| 989 | + } |
|
| 990 | + |
|
| 991 | + /** |
|
| 992 | + * Get and remove the last item from the collection. |
|
| 993 | + * |
|
| 994 | + * @return mixed |
|
| 995 | + */ |
|
| 996 | + public function pop() |
|
| 997 | + { |
|
| 998 | + return array_pop($this->items); |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + /** |
|
| 1002 | + * Push an item onto the beginning of the collection. |
|
| 1003 | + * |
|
| 1004 | + * @param mixed $value |
|
| 1005 | + * @param mixed $key |
|
| 1006 | + * @return $this |
|
| 1007 | + */ |
|
| 1008 | + public function prepend($value, $key = null) |
|
| 1009 | + { |
|
| 1010 | + $this->items = Arr::prepend($this->items, $value, $key); |
|
| 1011 | + |
|
| 1012 | + return $this; |
|
| 1013 | + } |
|
| 1014 | + |
|
| 1015 | + /** |
|
| 1016 | + * Push an item onto the end of the collection. |
|
| 1017 | + * |
|
| 1018 | + * @param mixed $value |
|
| 1019 | + * @return $this |
|
| 1020 | + */ |
|
| 1021 | + public function push($value) |
|
| 1022 | + { |
|
| 1023 | + $this->offsetSet(null, $value); |
|
| 1024 | + |
|
| 1025 | + return $this; |
|
| 1026 | + } |
|
| 1027 | + |
|
| 1028 | + /** |
|
| 1029 | + * Push all of the given items onto the collection. |
|
| 1030 | + * |
|
| 1031 | + * @param \Traversable $source |
|
| 1032 | + * @return self |
|
| 1033 | + */ |
|
| 1034 | + public function concat($source) |
|
| 1035 | + { |
|
| 1036 | + $result = new static($this); |
|
| 1037 | + |
|
| 1038 | + foreach ($source as $item) { |
|
| 1039 | + $result->push($item); |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + return $result; |
|
| 1043 | + } |
|
| 1044 | + |
|
| 1045 | + /** |
|
| 1046 | + * Get and remove an item from the collection. |
|
| 1047 | + * |
|
| 1048 | + * @param mixed $key |
|
| 1049 | + * @param mixed $default |
|
| 1050 | + * @return mixed |
|
| 1051 | + */ |
|
| 1052 | + public function pull($key, $default = null) |
|
| 1053 | + { |
|
| 1054 | + return Arr::pull($this->items, $key, $default); |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + /** |
|
| 1058 | + * Put an item in the collection by key. |
|
| 1059 | + * |
|
| 1060 | + * @param mixed $key |
|
| 1061 | + * @param mixed $value |
|
| 1062 | + * @return $this |
|
| 1063 | + */ |
|
| 1064 | + public function put($key, $value) |
|
| 1065 | + { |
|
| 1066 | + $this->offsetSet($key, $value); |
|
| 1067 | + |
|
| 1068 | + return $this; |
|
| 1069 | + } |
|
| 1070 | + |
|
| 1071 | + /** |
|
| 1072 | + * Get one or a specified number of items randomly from the collection. |
|
| 1073 | + * |
|
| 1074 | + * @param int|null $number |
|
| 1075 | + * @return mixed |
|
| 1076 | + * |
|
| 1077 | + * @throws \InvalidArgumentException |
|
| 1078 | + */ |
|
| 1079 | + public function random($number = null) |
|
| 1080 | + { |
|
| 1081 | + if (is_null($number)) { |
|
| 1082 | + return Arr::random($this->items); |
|
| 1083 | + } |
|
| 1084 | + |
|
| 1085 | + return new static(Arr::random($this->items, $number)); |
|
| 1086 | + } |
|
| 1087 | + |
|
| 1088 | + /** |
|
| 1089 | + * Reduce the collection to a single value. |
|
| 1090 | + * |
|
| 1091 | + * @param callable $callback |
|
| 1092 | + * @param mixed $initial |
|
| 1093 | + * @return mixed |
|
| 1094 | + */ |
|
| 1095 | + public function reduce(callable $callback, $initial = null) |
|
| 1096 | + { |
|
| 1097 | + return array_reduce($this->items, $callback, $initial); |
|
| 1098 | + } |
|
| 1099 | + |
|
| 1100 | + /** |
|
| 1101 | + * Create a collection of all elements that do not pass a given truth test. |
|
| 1102 | + * |
|
| 1103 | + * @param callable|mixed $callback |
|
| 1104 | + * @return static |
|
| 1105 | + */ |
|
| 1106 | + public function reject($callback) |
|
| 1107 | + { |
|
| 1108 | + if ($this->useAsCallable($callback)) { |
|
| 1109 | + return $this->filter(function ($value, $key) use ($callback) { |
|
| 1110 | + return ! $callback($value, $key); |
|
| 1111 | + }); |
|
| 1112 | + } |
|
| 1113 | + |
|
| 1114 | + return $this->filter(function ($item) use ($callback) { |
|
| 1115 | + return $item != $callback; |
|
| 1116 | + }); |
|
| 1117 | + } |
|
| 1118 | + |
|
| 1119 | + /** |
|
| 1120 | + * Reverse items order. |
|
| 1121 | + * |
|
| 1122 | + * @return static |
|
| 1123 | + */ |
|
| 1124 | + public function reverse() |
|
| 1125 | + { |
|
| 1126 | + return new static(array_reverse($this->items, true)); |
|
| 1127 | + } |
|
| 1128 | + |
|
| 1129 | + /** |
|
| 1130 | + * Search the collection for a given value and return the corresponding key if successful. |
|
| 1131 | + * |
|
| 1132 | + * @param mixed $value |
|
| 1133 | + * @param bool $strict |
|
| 1134 | + * @return mixed |
|
| 1135 | + */ |
|
| 1136 | + public function search($value, $strict = false) |
|
| 1137 | + { |
|
| 1138 | + if (! $this->useAsCallable($value)) { |
|
| 1139 | + return array_search($value, $this->items, $strict); |
|
| 1140 | + } |
|
| 1141 | + |
|
| 1142 | + foreach ($this->items as $key => $item) { |
|
| 1143 | + if (call_user_func($value, $item, $key)) { |
|
| 1144 | + return $key; |
|
| 1145 | + } |
|
| 1146 | + } |
|
| 1147 | + |
|
| 1148 | + return false; |
|
| 1149 | + } |
|
| 1150 | + |
|
| 1151 | + /** |
|
| 1152 | + * Get and remove the first item from the collection. |
|
| 1153 | + * |
|
| 1154 | + * @return mixed |
|
| 1155 | + */ |
|
| 1156 | + public function shift() |
|
| 1157 | + { |
|
| 1158 | + return array_shift($this->items); |
|
| 1159 | + } |
|
| 1160 | + |
|
| 1161 | + /** |
|
| 1162 | + * Shuffle the items in the collection. |
|
| 1163 | + * |
|
| 1164 | + * @param int $seed |
|
| 1165 | + * @return static |
|
| 1166 | + */ |
|
| 1167 | + public function shuffle($seed = null) |
|
| 1168 | + { |
|
| 1169 | + $items = $this->items; |
|
| 1170 | + |
|
| 1171 | + if (is_null($seed)) { |
|
| 1172 | + shuffle($items); |
|
| 1173 | + } else { |
|
| 1174 | + srand($seed); |
|
| 1175 | + |
|
| 1176 | + usort($items, function () { |
|
| 1177 | + return rand(-1, 1); |
|
| 1178 | + }); |
|
| 1179 | + } |
|
| 1180 | + |
|
| 1181 | + return new static($items); |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + /** |
|
| 1185 | + * Slice the underlying collection array. |
|
| 1186 | + * |
|
| 1187 | + * @param int $offset |
|
| 1188 | + * @param int $length |
|
| 1189 | + * @return static |
|
| 1190 | + */ |
|
| 1191 | + public function slice($offset, $length = null) |
|
| 1192 | + { |
|
| 1193 | + return new static(array_slice($this->items, $offset, $length, true)); |
|
| 1194 | + } |
|
| 1195 | + |
|
| 1196 | + /** |
|
| 1197 | + * Split a collection into a certain number of groups. |
|
| 1198 | + * |
|
| 1199 | + * @param int $numberOfGroups |
|
| 1200 | + * @return static |
|
| 1201 | + */ |
|
| 1202 | + public function split($numberOfGroups) |
|
| 1203 | + { |
|
| 1204 | + if ($this->isEmpty()) { |
|
| 1205 | + return new static; |
|
| 1206 | + } |
|
| 1207 | + |
|
| 1208 | + $groupSize = ceil($this->count() / $numberOfGroups); |
|
| 1209 | + |
|
| 1210 | + return $this->chunk($groupSize); |
|
| 1211 | + } |
|
| 1212 | + |
|
| 1213 | + /** |
|
| 1214 | + * Chunk the underlying collection array. |
|
| 1215 | + * |
|
| 1216 | + * @param int $size |
|
| 1217 | + * @return static |
|
| 1218 | + */ |
|
| 1219 | + public function chunk($size) |
|
| 1220 | + { |
|
| 1221 | + if ($size <= 0) { |
|
| 1222 | + return new static; |
|
| 1223 | + } |
|
| 1224 | + |
|
| 1225 | + $chunks = []; |
|
| 1226 | + |
|
| 1227 | + foreach (array_chunk($this->items, $size, true) as $chunk) { |
|
| 1228 | + $chunks[] = new static($chunk); |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + return new static($chunks); |
|
| 1232 | + } |
|
| 1233 | + |
|
| 1234 | + /** |
|
| 1235 | + * Sort through each item with a callback. |
|
| 1236 | + * |
|
| 1237 | + * @param callable|null $callback |
|
| 1238 | + * @return static |
|
| 1239 | + */ |
|
| 1240 | + public function sort(callable $callback = null) |
|
| 1241 | + { |
|
| 1242 | + $items = $this->items; |
|
| 1243 | + |
|
| 1244 | + $callback |
|
| 1245 | + ? uasort($items, $callback) |
|
| 1246 | + : asort($items); |
|
| 1247 | + |
|
| 1248 | + return new static($items); |
|
| 1249 | + } |
|
| 1250 | + |
|
| 1251 | + /** |
|
| 1252 | + * Sort the collection using the given callback. |
|
| 1253 | + * |
|
| 1254 | + * @param callable|string $callback |
|
| 1255 | + * @param int $options |
|
| 1256 | + * @param bool $descending |
|
| 1257 | + * @return static |
|
| 1258 | + */ |
|
| 1259 | + public function sortBy($callback, $options = SORT_REGULAR, $descending = false) |
|
| 1260 | + { |
|
| 1261 | + $results = []; |
|
| 1262 | + |
|
| 1263 | + $callback = $this->valueRetriever($callback); |
|
| 1264 | + |
|
| 1265 | + // First we will loop through the items and get the comparator from a callback |
|
| 1266 | + // function which we were given. Then, we will sort the returned values and |
|
| 1267 | + // and grab the corresponding values for the sorted keys from this array. |
|
| 1268 | + foreach ($this->items as $key => $value) { |
|
| 1269 | + $results[$key] = $callback($value, $key); |
|
| 1270 | + } |
|
| 1271 | + |
|
| 1272 | + $descending ? arsort($results, $options) |
|
| 1273 | + : asort($results, $options); |
|
| 1274 | + |
|
| 1275 | + // Once we have sorted all of the keys in the array, we will loop through them |
|
| 1276 | + // and grab the corresponding model so we can set the underlying items list |
|
| 1277 | + // to the sorted version. Then we'll just return the collection instance. |
|
| 1278 | + foreach (array_keys($results) as $key) { |
|
| 1279 | + $results[$key] = $this->items[$key]; |
|
| 1280 | + } |
|
| 1281 | + |
|
| 1282 | + return new static($results); |
|
| 1283 | + } |
|
| 1284 | + |
|
| 1285 | + /** |
|
| 1286 | + * Sort the collection in descending order using the given callback. |
|
| 1287 | + * |
|
| 1288 | + * @param callable|string $callback |
|
| 1289 | + * @param int $options |
|
| 1290 | + * @return static |
|
| 1291 | + */ |
|
| 1292 | + public function sortByDesc($callback, $options = SORT_REGULAR) |
|
| 1293 | + { |
|
| 1294 | + return $this->sortBy($callback, $options, true); |
|
| 1295 | + } |
|
| 1296 | + |
|
| 1297 | + /** |
|
| 1298 | + * Splice a portion of the underlying collection array. |
|
| 1299 | + * |
|
| 1300 | + * @param int $offset |
|
| 1301 | + * @param int|null $length |
|
| 1302 | + * @param mixed $replacement |
|
| 1303 | + * @return static |
|
| 1304 | + */ |
|
| 1305 | + public function splice($offset, $length = null, $replacement = []) |
|
| 1306 | + { |
|
| 1307 | + if (func_num_args() == 1) { |
|
| 1308 | + return new static(array_splice($this->items, $offset)); |
|
| 1309 | + } |
|
| 1310 | + |
|
| 1311 | + return new static(array_splice($this->items, $offset, $length, $replacement)); |
|
| 1312 | + } |
|
| 1313 | + |
|
| 1314 | + /** |
|
| 1315 | + * Get the sum of the given values. |
|
| 1316 | + * |
|
| 1317 | + * @param callable|string|null $callback |
|
| 1318 | + * @return mixed |
|
| 1319 | + */ |
|
| 1320 | + public function sum($callback = null) |
|
| 1321 | + { |
|
| 1322 | + if (is_null($callback)) { |
|
| 1323 | + return array_sum($this->items); |
|
| 1324 | + } |
|
| 1325 | + |
|
| 1326 | + $callback = $this->valueRetriever($callback); |
|
| 1327 | + |
|
| 1328 | + return $this->reduce(function ($result, $item) use ($callback) { |
|
| 1329 | + return $result + $callback($item); |
|
| 1330 | + }, 0); |
|
| 1331 | + } |
|
| 1332 | + |
|
| 1333 | + /** |
|
| 1334 | + * Take the first or last {$limit} items. |
|
| 1335 | + * |
|
| 1336 | + * @param int $limit |
|
| 1337 | + * @return static |
|
| 1338 | + */ |
|
| 1339 | + public function take($limit) |
|
| 1340 | + { |
|
| 1341 | + if ($limit < 0) { |
|
| 1342 | + return $this->slice($limit, abs($limit)); |
|
| 1343 | + } |
|
| 1344 | + |
|
| 1345 | + return $this->slice(0, $limit); |
|
| 1346 | + } |
|
| 1347 | + |
|
| 1348 | + /** |
|
| 1349 | + * Pass the collection to the given callback and then return it. |
|
| 1350 | + * |
|
| 1351 | + * @param callable $callback |
|
| 1352 | + * @return $this |
|
| 1353 | + */ |
|
| 1354 | + public function tap(callable $callback) |
|
| 1355 | + { |
|
| 1356 | + $callback(new static($this->items)); |
|
| 1357 | + |
|
| 1358 | + return $this; |
|
| 1359 | + } |
|
| 1360 | + |
|
| 1361 | + /** |
|
| 1362 | + * Transform each item in the collection using a callback. |
|
| 1363 | + * |
|
| 1364 | + * @param callable $callback |
|
| 1365 | + * @return $this |
|
| 1366 | + */ |
|
| 1367 | + public function transform(callable $callback) |
|
| 1368 | + { |
|
| 1369 | + $this->items = $this->map($callback)->all(); |
|
| 1370 | + |
|
| 1371 | + return $this; |
|
| 1372 | + } |
|
| 1373 | + |
|
| 1374 | + /** |
|
| 1375 | + * Return only unique items from the collection array. |
|
| 1376 | + * |
|
| 1377 | + * @param string|callable|null $key |
|
| 1378 | + * @param bool $strict |
|
| 1379 | + * @return static |
|
| 1380 | + */ |
|
| 1381 | + public function unique($key = null, $strict = false) |
|
| 1382 | + { |
|
| 1383 | + if (is_null($key)) { |
|
| 1384 | + return new static(array_unique($this->items, SORT_REGULAR)); |
|
| 1385 | + } |
|
| 1386 | + |
|
| 1387 | + $callback = $this->valueRetriever($key); |
|
| 1388 | + |
|
| 1389 | + $exists = []; |
|
| 1390 | + |
|
| 1391 | + return $this->reject(function ($item, $key) use ($callback, $strict, &$exists) { |
|
| 1392 | + if (in_array($id = $callback($item, $key), $exists, $strict)) { |
|
| 1393 | + return true; |
|
| 1394 | + } |
|
| 1395 | + |
|
| 1396 | + $exists[] = $id; |
|
| 1397 | + }); |
|
| 1398 | + } |
|
| 1399 | + |
|
| 1400 | + /** |
|
| 1401 | + * Return only unique items from the collection array using strict comparison. |
|
| 1402 | + * |
|
| 1403 | + * @param string|callable|null $key |
|
| 1404 | + * @return static |
|
| 1405 | + */ |
|
| 1406 | + public function uniqueStrict($key = null) |
|
| 1407 | + { |
|
| 1408 | + return $this->unique($key, true); |
|
| 1409 | + } |
|
| 1410 | + |
|
| 1411 | + /** |
|
| 1412 | + * Reset the keys on the underlying array. |
|
| 1413 | + * |
|
| 1414 | + * @return static |
|
| 1415 | + */ |
|
| 1416 | + public function values() |
|
| 1417 | + { |
|
| 1418 | + return new static(array_values($this->items)); |
|
| 1419 | + } |
|
| 1420 | + |
|
| 1421 | + /** |
|
| 1422 | + * Get a value retrieving callback. |
|
| 1423 | + * |
|
| 1424 | + * @param string $value |
|
| 1425 | + * @return callable |
|
| 1426 | + */ |
|
| 1427 | + protected function valueRetriever($value) |
|
| 1428 | + { |
|
| 1429 | + if ($this->useAsCallable($value)) { |
|
| 1430 | + return $value; |
|
| 1431 | + } |
|
| 1432 | + |
|
| 1433 | + return function ($item) use ($value) { |
|
| 1434 | + return data_get($item, $value); |
|
| 1435 | + }; |
|
| 1436 | + } |
|
| 1437 | + |
|
| 1438 | + /** |
|
| 1439 | + * Zip the collection together with one or more arrays. |
|
| 1440 | + * |
|
| 1441 | + * e.g. new Collection([1, 2, 3])->zip([4, 5, 6]); |
|
| 1442 | + * => [[1, 4], [2, 5], [3, 6]] |
|
| 1443 | + * |
|
| 1444 | + * @param mixed ...$items |
|
| 1445 | + * @return static |
|
| 1446 | + */ |
|
| 1447 | + public function zip($items) |
|
| 1448 | + { |
|
| 1449 | + $arrayableItems = array_map(function ($items) { |
|
| 1450 | + return $this->getArrayableItems($items); |
|
| 1451 | + }, func_get_args()); |
|
| 1452 | + |
|
| 1453 | + $params = array_merge([function () { |
|
| 1454 | + return new static(func_get_args()); |
|
| 1455 | + }, $this->items], $arrayableItems); |
|
| 1456 | + |
|
| 1457 | + return new static(call_user_func_array('array_map', $params)); |
|
| 1458 | + } |
|
| 1459 | + |
|
| 1460 | + /** |
|
| 1461 | + * Get the collection of items as a plain array. |
|
| 1462 | + * |
|
| 1463 | + * @return array |
|
| 1464 | + */ |
|
| 1465 | + public function toArray() |
|
| 1466 | + { |
|
| 1467 | + return array_map(function ($value) { |
|
| 1468 | + return $value instanceof Arrayable ? $value->toArray() : $value; |
|
| 1469 | + }, $this->items); |
|
| 1470 | + } |
|
| 1471 | + |
|
| 1472 | + /** |
|
| 1473 | + * Convert the object into something JSON serializable. |
|
| 1474 | + * |
|
| 1475 | + * @return array |
|
| 1476 | + */ |
|
| 1477 | + public function jsonSerialize() |
|
| 1478 | + { |
|
| 1479 | + return array_map(function ($value) { |
|
| 1480 | + if ($value instanceof JsonSerializable) { |
|
| 1481 | + return $value->jsonSerialize(); |
|
| 1482 | + } elseif ($value instanceof Jsonable) { |
|
| 1483 | + return json_decode($value->toJson(), true); |
|
| 1484 | + } elseif ($value instanceof Arrayable) { |
|
| 1485 | + return $value->toArray(); |
|
| 1486 | + } else { |
|
| 1487 | + return $value; |
|
| 1488 | + } |
|
| 1489 | + }, $this->items); |
|
| 1490 | + } |
|
| 1491 | + |
|
| 1492 | + /** |
|
| 1493 | + * Get the collection of items as JSON. |
|
| 1494 | + * |
|
| 1495 | + * @param int $options |
|
| 1496 | + * @return string |
|
| 1497 | + */ |
|
| 1498 | + public function toJson($options = 0) |
|
| 1499 | + { |
|
| 1500 | + return json_encode($this->jsonSerialize(), $options); |
|
| 1501 | + } |
|
| 1502 | + |
|
| 1503 | + /** |
|
| 1504 | + * Get an iterator for the items. |
|
| 1505 | + * |
|
| 1506 | + * @return \ArrayIterator |
|
| 1507 | + */ |
|
| 1508 | + public function getIterator() |
|
| 1509 | + { |
|
| 1510 | + return new ArrayIterator($this->items); |
|
| 1511 | + } |
|
| 1512 | + |
|
| 1513 | + /** |
|
| 1514 | + * Get a CachingIterator instance. |
|
| 1515 | + * |
|
| 1516 | + * @param int $flags |
|
| 1517 | + * @return \CachingIterator |
|
| 1518 | + */ |
|
| 1519 | + public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
| 1520 | + { |
|
| 1521 | + return new CachingIterator($this->getIterator(), $flags); |
|
| 1522 | + } |
|
| 1523 | + |
|
| 1524 | + /** |
|
| 1525 | + * Count the number of items in the collection. |
|
| 1526 | + * |
|
| 1527 | + * @return int |
|
| 1528 | + */ |
|
| 1529 | + public function count() |
|
| 1530 | + { |
|
| 1531 | + return count($this->items); |
|
| 1532 | + } |
|
| 1533 | + |
|
| 1534 | + /** |
|
| 1535 | + * Get a base Support collection instance from this collection. |
|
| 1536 | + * |
|
| 1537 | + * @return \Illuminate\Support\Collection |
|
| 1538 | + */ |
|
| 1539 | + public function toBase() |
|
| 1540 | + { |
|
| 1541 | + return new self($this); |
|
| 1542 | + } |
|
| 1543 | + |
|
| 1544 | + /** |
|
| 1545 | + * Determine if an item exists at an offset. |
|
| 1546 | + * |
|
| 1547 | + * @param mixed $key |
|
| 1548 | + * @return bool |
|
| 1549 | + */ |
|
| 1550 | + public function offsetExists($key) |
|
| 1551 | + { |
|
| 1552 | + return array_key_exists($key, $this->items); |
|
| 1553 | + } |
|
| 1554 | + |
|
| 1555 | + /** |
|
| 1556 | + * Get an item at a given offset. |
|
| 1557 | + * |
|
| 1558 | + * @param mixed $key |
|
| 1559 | + * @return mixed |
|
| 1560 | + */ |
|
| 1561 | + public function offsetGet($key) |
|
| 1562 | + { |
|
| 1563 | + return $this->items[$key]; |
|
| 1564 | + } |
|
| 1565 | + |
|
| 1566 | + /** |
|
| 1567 | + * Set the item at a given offset. |
|
| 1568 | + * |
|
| 1569 | + * @param mixed $key |
|
| 1570 | + * @param mixed $value |
|
| 1571 | + * @return void |
|
| 1572 | + */ |
|
| 1573 | + public function offsetSet($key, $value) |
|
| 1574 | + { |
|
| 1575 | + if (is_null($key)) { |
|
| 1576 | + $this->items[] = $value; |
|
| 1577 | + } else { |
|
| 1578 | + $this->items[$key] = $value; |
|
| 1579 | + } |
|
| 1580 | + } |
|
| 1581 | + |
|
| 1582 | + /** |
|
| 1583 | + * Unset the item at a given offset. |
|
| 1584 | + * |
|
| 1585 | + * @param string $key |
|
| 1586 | + * @return void |
|
| 1587 | + */ |
|
| 1588 | + public function offsetUnset($key) |
|
| 1589 | + { |
|
| 1590 | + unset($this->items[$key]); |
|
| 1591 | + } |
|
| 1592 | + |
|
| 1593 | + /** |
|
| 1594 | + * Convert the collection to its string representation. |
|
| 1595 | + * |
|
| 1596 | + * @return string |
|
| 1597 | + */ |
|
| 1598 | + public function __toString() |
|
| 1599 | + { |
|
| 1600 | + return $this->toJson(); |
|
| 1601 | + } |
|
| 1602 | + |
|
| 1603 | + /** |
|
| 1604 | + * Results array of items from Collection or Arrayable. |
|
| 1605 | + * |
|
| 1606 | + * @param mixed $items |
|
| 1607 | + * @return array |
|
| 1608 | + */ |
|
| 1609 | + protected function getArrayableItems($items) |
|
| 1610 | + { |
|
| 1611 | + if (is_array($items)) { |
|
| 1612 | + return $items; |
|
| 1613 | + } elseif ($items instanceof self) { |
|
| 1614 | + return $items->all(); |
|
| 1615 | + } elseif ($items instanceof Arrayable) { |
|
| 1616 | + return $items->toArray(); |
|
| 1617 | + } elseif ($items instanceof Jsonable) { |
|
| 1618 | + return json_decode($items->toJson(), true); |
|
| 1619 | + } elseif ($items instanceof JsonSerializable) { |
|
| 1620 | + return $items->jsonSerialize(); |
|
| 1621 | + } elseif ($items instanceof Traversable) { |
|
| 1622 | + return iterator_to_array($items); |
|
| 1623 | + } |
|
| 1624 | + |
|
| 1625 | + return (array) $items; |
|
| 1626 | + } |
|
| 1627 | + |
|
| 1628 | + /** |
|
| 1629 | + * Add a method to the list of proxied methods. |
|
| 1630 | + * |
|
| 1631 | + * @param string $method |
|
| 1632 | + * @return void |
|
| 1633 | + */ |
|
| 1634 | + public static function proxy($method) |
|
| 1635 | + { |
|
| 1636 | + static::$proxies[] = $method; |
|
| 1637 | + } |
|
| 1638 | + |
|
| 1639 | + /** |
|
| 1640 | + * Dynamically access collection proxies. |
|
| 1641 | + * |
|
| 1642 | + * @param string $key |
|
| 1643 | + * @return mixed |
|
| 1644 | + * |
|
| 1645 | + * @throws \Exception |
|
| 1646 | + */ |
|
| 1647 | + public function __get($key) |
|
| 1648 | + { |
|
| 1649 | + if (! in_array($key, static::$proxies)) { |
|
| 1650 | + throw new Exception("Property [{$key}] does not exist on this collection instance."); |
|
| 1651 | + } |
|
| 1652 | + |
|
| 1653 | + return new HigherOrderCollectionProxy($this, $key); |
|
| 1654 | + } |
|
| 1655 | 1655 | } |
@@ -7,27 +7,27 @@ |
||
| 7 | 7 | */ |
| 8 | 8 | class Input extends Facade |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Get an item from the input data. |
|
| 12 | - * |
|
| 13 | - * This method is used for all request verbs (GET, POST, PUT, and DELETE) |
|
| 14 | - * |
|
| 15 | - * @param string $key |
|
| 16 | - * @param mixed $default |
|
| 17 | - * @return mixed |
|
| 18 | - */ |
|
| 19 | - public static function get($key = null, $default = null) |
|
| 20 | - { |
|
| 21 | - return static::$app['request']->input($key, $default); |
|
| 22 | - } |
|
| 10 | + /** |
|
| 11 | + * Get an item from the input data. |
|
| 12 | + * |
|
| 13 | + * This method is used for all request verbs (GET, POST, PUT, and DELETE) |
|
| 14 | + * |
|
| 15 | + * @param string $key |
|
| 16 | + * @param mixed $default |
|
| 17 | + * @return mixed |
|
| 18 | + */ |
|
| 19 | + public static function get($key = null, $default = null) |
|
| 20 | + { |
|
| 21 | + return static::$app['request']->input($key, $default); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * Get the registered name of the component. |
|
| 26 | - * |
|
| 27 | - * @return string |
|
| 28 | - */ |
|
| 29 | - protected static function getFacadeAccessor() |
|
| 30 | - { |
|
| 31 | - return 'request'; |
|
| 32 | - } |
|
| 24 | + /** |
|
| 25 | + * Get the registered name of the component. |
|
| 26 | + * |
|
| 27 | + * @return string |
|
| 28 | + */ |
|
| 29 | + protected static function getFacadeAccessor() |
|
| 30 | + { |
|
| 31 | + return 'request'; |
|
| 32 | + } |
|
| 33 | 33 | } |
@@ -8,595 +8,595 @@ |
||
| 8 | 8 | |
| 9 | 9 | class Arr |
| 10 | 10 | { |
| 11 | - use Macroable; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * Determine whether the given value is array accessible. |
|
| 15 | - * |
|
| 16 | - * @param mixed $value |
|
| 17 | - * @return bool |
|
| 18 | - */ |
|
| 19 | - public static function accessible($value) |
|
| 20 | - { |
|
| 21 | - return is_array($value) || $value instanceof ArrayAccess; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Add an element to an array using "dot" notation if it doesn't exist. |
|
| 26 | - * |
|
| 27 | - * @param array $array |
|
| 28 | - * @param string $key |
|
| 29 | - * @param mixed $value |
|
| 30 | - * @return array |
|
| 31 | - */ |
|
| 32 | - public static function add($array, $key, $value) |
|
| 33 | - { |
|
| 34 | - if (is_null(static::get($array, $key))) { |
|
| 35 | - static::set($array, $key, $value); |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - return $array; |
|
| 39 | - } |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * Collapse an array of arrays into a single array. |
|
| 43 | - * |
|
| 44 | - * @param array $array |
|
| 45 | - * @return array |
|
| 46 | - */ |
|
| 47 | - public static function collapse($array) |
|
| 48 | - { |
|
| 49 | - $results = []; |
|
| 50 | - |
|
| 51 | - foreach ($array as $values) { |
|
| 52 | - if ($values instanceof Collection) { |
|
| 53 | - $values = $values->all(); |
|
| 54 | - } elseif (! is_array($values)) { |
|
| 55 | - continue; |
|
| 56 | - } |
|
| 57 | - |
|
| 58 | - $results = array_merge($results, $values); |
|
| 59 | - } |
|
| 60 | - |
|
| 61 | - return $results; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Cross join the given arrays, returning all possible permutations. |
|
| 66 | - * |
|
| 67 | - * @param array ...$arrays |
|
| 68 | - * @return array |
|
| 69 | - */ |
|
| 70 | - public static function crossJoin(...$arrays) |
|
| 71 | - { |
|
| 72 | - $results = [[]]; |
|
| 73 | - |
|
| 74 | - foreach ($arrays as $index => $array) { |
|
| 75 | - $append = []; |
|
| 76 | - |
|
| 77 | - foreach ($results as $product) { |
|
| 78 | - foreach ($array as $item) { |
|
| 79 | - $product[$index] = $item; |
|
| 80 | - |
|
| 81 | - $append[] = $product; |
|
| 82 | - } |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $results = $append; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - return $results; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Divide an array into two arrays. One with keys and the other with values. |
|
| 93 | - * |
|
| 94 | - * @param array $array |
|
| 95 | - * @return array |
|
| 96 | - */ |
|
| 97 | - public static function divide($array) |
|
| 98 | - { |
|
| 99 | - return [array_keys($array), array_values($array)]; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Flatten a multi-dimensional associative array with dots. |
|
| 104 | - * |
|
| 105 | - * @param array $array |
|
| 106 | - * @param string $prepend |
|
| 107 | - * @return array |
|
| 108 | - */ |
|
| 109 | - public static function dot($array, $prepend = '') |
|
| 110 | - { |
|
| 111 | - $results = []; |
|
| 112 | - |
|
| 113 | - foreach ($array as $key => $value) { |
|
| 114 | - if (is_array($value) && ! empty($value)) { |
|
| 115 | - $results = array_merge($results, static::dot($value, $prepend.$key.'.')); |
|
| 116 | - } else { |
|
| 117 | - $results[$prepend.$key] = $value; |
|
| 118 | - } |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - return $results; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Get all of the given array except for a specified array of items. |
|
| 126 | - * |
|
| 127 | - * @param array $array |
|
| 128 | - * @param array|string $keys |
|
| 129 | - * @return array |
|
| 130 | - */ |
|
| 131 | - public static function except($array, $keys) |
|
| 132 | - { |
|
| 133 | - static::forget($array, $keys); |
|
| 134 | - |
|
| 135 | - return $array; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - /** |
|
| 139 | - * Determine if the given key exists in the provided array. |
|
| 140 | - * |
|
| 141 | - * @param \ArrayAccess|array $array |
|
| 142 | - * @param string|int $key |
|
| 143 | - * @return bool |
|
| 144 | - */ |
|
| 145 | - public static function exists($array, $key) |
|
| 146 | - { |
|
| 147 | - if ($array instanceof ArrayAccess) { |
|
| 148 | - return $array->offsetExists($key); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - return array_key_exists($key, $array); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Return the first element in an array passing a given truth test. |
|
| 156 | - * |
|
| 157 | - * @param array $array |
|
| 158 | - * @param callable|null $callback |
|
| 159 | - * @param mixed $default |
|
| 160 | - * @return mixed |
|
| 161 | - */ |
|
| 162 | - public static function first($array, callable $callback = null, $default = null) |
|
| 163 | - { |
|
| 164 | - if (is_null($callback)) { |
|
| 165 | - if (empty($array)) { |
|
| 166 | - return value($default); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - foreach ($array as $item) { |
|
| 170 | - return $item; |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - foreach ($array as $key => $value) { |
|
| 175 | - if (call_user_func($callback, $value, $key)) { |
|
| 176 | - return $value; |
|
| 177 | - } |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - return value($default); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Return the last element in an array passing a given truth test. |
|
| 185 | - * |
|
| 186 | - * @param array $array |
|
| 187 | - * @param callable|null $callback |
|
| 188 | - * @param mixed $default |
|
| 189 | - * @return mixed |
|
| 190 | - */ |
|
| 191 | - public static function last($array, callable $callback = null, $default = null) |
|
| 192 | - { |
|
| 193 | - if (is_null($callback)) { |
|
| 194 | - return empty($array) ? value($default) : end($array); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return static::first(array_reverse($array, true), $callback, $default); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Flatten a multi-dimensional array into a single level. |
|
| 202 | - * |
|
| 203 | - * @param array $array |
|
| 204 | - * @param int $depth |
|
| 205 | - * @return array |
|
| 206 | - */ |
|
| 207 | - public static function flatten($array, $depth = INF) |
|
| 208 | - { |
|
| 209 | - return array_reduce($array, function ($result, $item) use ($depth) { |
|
| 210 | - $item = $item instanceof Collection ? $item->all() : $item; |
|
| 211 | - |
|
| 212 | - if (! is_array($item)) { |
|
| 213 | - return array_merge($result, [$item]); |
|
| 214 | - } elseif ($depth === 1) { |
|
| 215 | - return array_merge($result, array_values($item)); |
|
| 216 | - } else { |
|
| 217 | - return array_merge($result, static::flatten($item, $depth - 1)); |
|
| 218 | - } |
|
| 219 | - }, []); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Remove one or many array items from a given array using "dot" notation. |
|
| 224 | - * |
|
| 225 | - * @param array $array |
|
| 226 | - * @param array|string $keys |
|
| 227 | - * @return void |
|
| 228 | - */ |
|
| 229 | - public static function forget(&$array, $keys) |
|
| 230 | - { |
|
| 231 | - $original = &$array; |
|
| 232 | - |
|
| 233 | - $keys = (array) $keys; |
|
| 234 | - |
|
| 235 | - if (count($keys) === 0) { |
|
| 236 | - return; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - foreach ($keys as $key) { |
|
| 240 | - // if the exact key exists in the top-level, remove it |
|
| 241 | - if (static::exists($array, $key)) { |
|
| 242 | - unset($array[$key]); |
|
| 243 | - |
|
| 244 | - continue; |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - $parts = explode('.', $key); |
|
| 248 | - |
|
| 249 | - // clean up before each pass |
|
| 250 | - $array = &$original; |
|
| 251 | - |
|
| 252 | - while (count($parts) > 1) { |
|
| 253 | - $part = array_shift($parts); |
|
| 254 | - |
|
| 255 | - if (isset($array[$part]) && is_array($array[$part])) { |
|
| 256 | - $array = &$array[$part]; |
|
| 257 | - } else { |
|
| 258 | - continue 2; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - unset($array[array_shift($parts)]); |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Get an item from an array using "dot" notation. |
|
| 268 | - * |
|
| 269 | - * @param \ArrayAccess|array $array |
|
| 270 | - * @param string $key |
|
| 271 | - * @param mixed $default |
|
| 272 | - * @return mixed |
|
| 273 | - */ |
|
| 274 | - public static function get($array, $key, $default = null) |
|
| 275 | - { |
|
| 276 | - if (! static::accessible($array)) { |
|
| 277 | - return value($default); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - if (is_null($key)) { |
|
| 281 | - return $array; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - if (static::exists($array, $key)) { |
|
| 285 | - return $array[$key]; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - foreach (explode('.', $key) as $segment) { |
|
| 289 | - if (static::accessible($array) && static::exists($array, $segment)) { |
|
| 290 | - $array = $array[$segment]; |
|
| 291 | - } else { |
|
| 292 | - return value($default); |
|
| 293 | - } |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - return $array; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - /** |
|
| 300 | - * Check if an item or items exist in an array using "dot" notation. |
|
| 301 | - * |
|
| 302 | - * @param \ArrayAccess|array $array |
|
| 303 | - * @param string|array $keys |
|
| 304 | - * @return bool |
|
| 305 | - */ |
|
| 306 | - public static function has($array, $keys) |
|
| 307 | - { |
|
| 308 | - if (is_null($keys)) { |
|
| 309 | - return false; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - $keys = (array) $keys; |
|
| 313 | - |
|
| 314 | - if (! $array) { |
|
| 315 | - return false; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - if ($keys === []) { |
|
| 319 | - return false; |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - foreach ($keys as $key) { |
|
| 323 | - $subKeyArray = $array; |
|
| 324 | - |
|
| 325 | - if (static::exists($array, $key)) { |
|
| 326 | - continue; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - foreach (explode('.', $key) as $segment) { |
|
| 330 | - if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) { |
|
| 331 | - $subKeyArray = $subKeyArray[$segment]; |
|
| 332 | - } else { |
|
| 333 | - return false; |
|
| 334 | - } |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - return true; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - /** |
|
| 342 | - * Determines if an array is associative. |
|
| 343 | - * |
|
| 344 | - * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
|
| 345 | - * |
|
| 346 | - * @param array $array |
|
| 347 | - * @return bool |
|
| 348 | - */ |
|
| 349 | - public static function isAssoc(array $array) |
|
| 350 | - { |
|
| 351 | - $keys = array_keys($array); |
|
| 352 | - |
|
| 353 | - return array_keys($keys) !== $keys; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * Get a subset of the items from the given array. |
|
| 358 | - * |
|
| 359 | - * @param array $array |
|
| 360 | - * @param array|string $keys |
|
| 361 | - * @return array |
|
| 362 | - */ |
|
| 363 | - public static function only($array, $keys) |
|
| 364 | - { |
|
| 365 | - return array_intersect_key($array, array_flip((array) $keys)); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Pluck an array of values from an array. |
|
| 370 | - * |
|
| 371 | - * @param array $array |
|
| 372 | - * @param string|array $value |
|
| 373 | - * @param string|array|null $key |
|
| 374 | - * @return array |
|
| 375 | - */ |
|
| 376 | - public static function pluck($array, $value, $key = null) |
|
| 377 | - { |
|
| 378 | - $results = []; |
|
| 379 | - |
|
| 380 | - list($value, $key) = static::explodePluckParameters($value, $key); |
|
| 381 | - |
|
| 382 | - foreach ($array as $item) { |
|
| 383 | - $itemValue = data_get($item, $value); |
|
| 384 | - |
|
| 385 | - // If the key is "null", we will just append the value to the array and keep |
|
| 386 | - // looping. Otherwise we will key the array using the value of the key we |
|
| 387 | - // received from the developer. Then we'll return the final array form. |
|
| 388 | - if (is_null($key)) { |
|
| 389 | - $results[] = $itemValue; |
|
| 390 | - } else { |
|
| 391 | - $itemKey = data_get($item, $key); |
|
| 392 | - |
|
| 393 | - if (is_object($itemKey) && method_exists($itemKey, '__toString')) { |
|
| 394 | - $itemKey = (string) $itemKey; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - $results[$itemKey] = $itemValue; |
|
| 398 | - } |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - return $results; |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - /** |
|
| 405 | - * Explode the "value" and "key" arguments passed to "pluck". |
|
| 406 | - * |
|
| 407 | - * @param string|array $value |
|
| 408 | - * @param string|array|null $key |
|
| 409 | - * @return array |
|
| 410 | - */ |
|
| 411 | - protected static function explodePluckParameters($value, $key) |
|
| 412 | - { |
|
| 413 | - $value = is_string($value) ? explode('.', $value) : $value; |
|
| 414 | - |
|
| 415 | - $key = is_null($key) || is_array($key) ? $key : explode('.', $key); |
|
| 416 | - |
|
| 417 | - return [$value, $key]; |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - /** |
|
| 421 | - * Push an item onto the beginning of an array. |
|
| 422 | - * |
|
| 423 | - * @param array $array |
|
| 424 | - * @param mixed $value |
|
| 425 | - * @param mixed $key |
|
| 426 | - * @return array |
|
| 427 | - */ |
|
| 428 | - public static function prepend($array, $value, $key = null) |
|
| 429 | - { |
|
| 430 | - if (is_null($key)) { |
|
| 431 | - array_unshift($array, $value); |
|
| 432 | - } else { |
|
| 433 | - $array = [$key => $value] + $array; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - return $array; |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - /** |
|
| 440 | - * Get a value from the array, and remove it. |
|
| 441 | - * |
|
| 442 | - * @param array $array |
|
| 443 | - * @param string $key |
|
| 444 | - * @param mixed $default |
|
| 445 | - * @return mixed |
|
| 446 | - */ |
|
| 447 | - public static function pull(&$array, $key, $default = null) |
|
| 448 | - { |
|
| 449 | - $value = static::get($array, $key, $default); |
|
| 450 | - |
|
| 451 | - static::forget($array, $key); |
|
| 452 | - |
|
| 453 | - return $value; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Get one or a specified number of random values from an array. |
|
| 458 | - * |
|
| 459 | - * @param array $array |
|
| 460 | - * @param int|null $number |
|
| 461 | - * @return mixed |
|
| 462 | - * |
|
| 463 | - * @throws \InvalidArgumentException |
|
| 464 | - */ |
|
| 465 | - public static function random($array, $number = null) |
|
| 466 | - { |
|
| 467 | - $requested = is_null($number) ? 1 : $number; |
|
| 468 | - |
|
| 469 | - $count = count($array); |
|
| 470 | - |
|
| 471 | - if ($requested > $count) { |
|
| 472 | - throw new InvalidArgumentException( |
|
| 473 | - "You requested {$requested} items, but there are only {$count} items available." |
|
| 474 | - ); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - if (is_null($number)) { |
|
| 478 | - return $array[array_rand($array)]; |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - if ((int) $number === 0) { |
|
| 482 | - return []; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - $keys = array_rand($array, $number); |
|
| 486 | - |
|
| 487 | - $results = []; |
|
| 488 | - |
|
| 489 | - foreach ((array) $keys as $key) { |
|
| 490 | - $results[] = $array[$key]; |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - return $results; |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Set an array item to a given value using "dot" notation. |
|
| 498 | - * |
|
| 499 | - * If no key is given to the method, the entire array will be replaced. |
|
| 500 | - * |
|
| 501 | - * @param array $array |
|
| 502 | - * @param string $key |
|
| 503 | - * @param mixed $value |
|
| 504 | - * @return array |
|
| 505 | - */ |
|
| 506 | - public static function set(&$array, $key, $value) |
|
| 507 | - { |
|
| 508 | - if (is_null($key)) { |
|
| 509 | - return $array = $value; |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - $keys = explode('.', $key); |
|
| 513 | - |
|
| 514 | - while (count($keys) > 1) { |
|
| 515 | - $key = array_shift($keys); |
|
| 516 | - |
|
| 517 | - // If the key doesn't exist at this depth, we will just create an empty array |
|
| 518 | - // to hold the next value, allowing us to create the arrays to hold final |
|
| 519 | - // values at the correct depth. Then we'll keep digging into the array. |
|
| 520 | - if (! isset($array[$key]) || ! is_array($array[$key])) { |
|
| 521 | - $array[$key] = []; |
|
| 522 | - } |
|
| 523 | - |
|
| 524 | - $array = &$array[$key]; |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - $array[array_shift($keys)] = $value; |
|
| 528 | - |
|
| 529 | - return $array; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - /** |
|
| 533 | - * Shuffle the given array and return the result. |
|
| 534 | - * |
|
| 535 | - * @param array $array |
|
| 536 | - * @return array |
|
| 537 | - */ |
|
| 538 | - public static function shuffle($array) |
|
| 539 | - { |
|
| 540 | - shuffle($array); |
|
| 541 | - |
|
| 542 | - return $array; |
|
| 543 | - } |
|
| 544 | - |
|
| 545 | - /** |
|
| 546 | - * Sort the array using the given callback or "dot" notation. |
|
| 547 | - * |
|
| 548 | - * @param array $array |
|
| 549 | - * @param callable|string $callback |
|
| 550 | - * @return array |
|
| 551 | - */ |
|
| 552 | - public static function sort($array, $callback) |
|
| 553 | - { |
|
| 554 | - return Collection::make($array)->sortBy($callback)->all(); |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Recursively sort an array by keys and values. |
|
| 559 | - * |
|
| 560 | - * @param array $array |
|
| 561 | - * @return array |
|
| 562 | - */ |
|
| 563 | - public static function sortRecursive($array) |
|
| 564 | - { |
|
| 565 | - foreach ($array as &$value) { |
|
| 566 | - if (is_array($value)) { |
|
| 567 | - $value = static::sortRecursive($value); |
|
| 568 | - } |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - if (static::isAssoc($array)) { |
|
| 572 | - ksort($array); |
|
| 573 | - } else { |
|
| 574 | - sort($array); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - return $array; |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - /** |
|
| 581 | - * Filter the array using the given callback. |
|
| 582 | - * |
|
| 583 | - * @param array $array |
|
| 584 | - * @param callable $callback |
|
| 585 | - * @return array |
|
| 586 | - */ |
|
| 587 | - public static function where($array, callable $callback) |
|
| 588 | - { |
|
| 589 | - return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); |
|
| 590 | - } |
|
| 591 | - |
|
| 592 | - /** |
|
| 593 | - * If the given value is not an array, wrap it in one. |
|
| 594 | - * |
|
| 595 | - * @param mixed $value |
|
| 596 | - * @return array |
|
| 597 | - */ |
|
| 598 | - public static function wrap($value) |
|
| 599 | - { |
|
| 600 | - return ! is_array($value) ? [$value] : $value; |
|
| 601 | - } |
|
| 11 | + use Macroable; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * Determine whether the given value is array accessible. |
|
| 15 | + * |
|
| 16 | + * @param mixed $value |
|
| 17 | + * @return bool |
|
| 18 | + */ |
|
| 19 | + public static function accessible($value) |
|
| 20 | + { |
|
| 21 | + return is_array($value) || $value instanceof ArrayAccess; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Add an element to an array using "dot" notation if it doesn't exist. |
|
| 26 | + * |
|
| 27 | + * @param array $array |
|
| 28 | + * @param string $key |
|
| 29 | + * @param mixed $value |
|
| 30 | + * @return array |
|
| 31 | + */ |
|
| 32 | + public static function add($array, $key, $value) |
|
| 33 | + { |
|
| 34 | + if (is_null(static::get($array, $key))) { |
|
| 35 | + static::set($array, $key, $value); |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + return $array; |
|
| 39 | + } |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * Collapse an array of arrays into a single array. |
|
| 43 | + * |
|
| 44 | + * @param array $array |
|
| 45 | + * @return array |
|
| 46 | + */ |
|
| 47 | + public static function collapse($array) |
|
| 48 | + { |
|
| 49 | + $results = []; |
|
| 50 | + |
|
| 51 | + foreach ($array as $values) { |
|
| 52 | + if ($values instanceof Collection) { |
|
| 53 | + $values = $values->all(); |
|
| 54 | + } elseif (! is_array($values)) { |
|
| 55 | + continue; |
|
| 56 | + } |
|
| 57 | + |
|
| 58 | + $results = array_merge($results, $values); |
|
| 59 | + } |
|
| 60 | + |
|
| 61 | + return $results; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Cross join the given arrays, returning all possible permutations. |
|
| 66 | + * |
|
| 67 | + * @param array ...$arrays |
|
| 68 | + * @return array |
|
| 69 | + */ |
|
| 70 | + public static function crossJoin(...$arrays) |
|
| 71 | + { |
|
| 72 | + $results = [[]]; |
|
| 73 | + |
|
| 74 | + foreach ($arrays as $index => $array) { |
|
| 75 | + $append = []; |
|
| 76 | + |
|
| 77 | + foreach ($results as $product) { |
|
| 78 | + foreach ($array as $item) { |
|
| 79 | + $product[$index] = $item; |
|
| 80 | + |
|
| 81 | + $append[] = $product; |
|
| 82 | + } |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $results = $append; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + return $results; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Divide an array into two arrays. One with keys and the other with values. |
|
| 93 | + * |
|
| 94 | + * @param array $array |
|
| 95 | + * @return array |
|
| 96 | + */ |
|
| 97 | + public static function divide($array) |
|
| 98 | + { |
|
| 99 | + return [array_keys($array), array_values($array)]; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Flatten a multi-dimensional associative array with dots. |
|
| 104 | + * |
|
| 105 | + * @param array $array |
|
| 106 | + * @param string $prepend |
|
| 107 | + * @return array |
|
| 108 | + */ |
|
| 109 | + public static function dot($array, $prepend = '') |
|
| 110 | + { |
|
| 111 | + $results = []; |
|
| 112 | + |
|
| 113 | + foreach ($array as $key => $value) { |
|
| 114 | + if (is_array($value) && ! empty($value)) { |
|
| 115 | + $results = array_merge($results, static::dot($value, $prepend.$key.'.')); |
|
| 116 | + } else { |
|
| 117 | + $results[$prepend.$key] = $value; |
|
| 118 | + } |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + return $results; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Get all of the given array except for a specified array of items. |
|
| 126 | + * |
|
| 127 | + * @param array $array |
|
| 128 | + * @param array|string $keys |
|
| 129 | + * @return array |
|
| 130 | + */ |
|
| 131 | + public static function except($array, $keys) |
|
| 132 | + { |
|
| 133 | + static::forget($array, $keys); |
|
| 134 | + |
|
| 135 | + return $array; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + /** |
|
| 139 | + * Determine if the given key exists in the provided array. |
|
| 140 | + * |
|
| 141 | + * @param \ArrayAccess|array $array |
|
| 142 | + * @param string|int $key |
|
| 143 | + * @return bool |
|
| 144 | + */ |
|
| 145 | + public static function exists($array, $key) |
|
| 146 | + { |
|
| 147 | + if ($array instanceof ArrayAccess) { |
|
| 148 | + return $array->offsetExists($key); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + return array_key_exists($key, $array); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Return the first element in an array passing a given truth test. |
|
| 156 | + * |
|
| 157 | + * @param array $array |
|
| 158 | + * @param callable|null $callback |
|
| 159 | + * @param mixed $default |
|
| 160 | + * @return mixed |
|
| 161 | + */ |
|
| 162 | + public static function first($array, callable $callback = null, $default = null) |
|
| 163 | + { |
|
| 164 | + if (is_null($callback)) { |
|
| 165 | + if (empty($array)) { |
|
| 166 | + return value($default); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + foreach ($array as $item) { |
|
| 170 | + return $item; |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + foreach ($array as $key => $value) { |
|
| 175 | + if (call_user_func($callback, $value, $key)) { |
|
| 176 | + return $value; |
|
| 177 | + } |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + return value($default); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Return the last element in an array passing a given truth test. |
|
| 185 | + * |
|
| 186 | + * @param array $array |
|
| 187 | + * @param callable|null $callback |
|
| 188 | + * @param mixed $default |
|
| 189 | + * @return mixed |
|
| 190 | + */ |
|
| 191 | + public static function last($array, callable $callback = null, $default = null) |
|
| 192 | + { |
|
| 193 | + if (is_null($callback)) { |
|
| 194 | + return empty($array) ? value($default) : end($array); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return static::first(array_reverse($array, true), $callback, $default); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Flatten a multi-dimensional array into a single level. |
|
| 202 | + * |
|
| 203 | + * @param array $array |
|
| 204 | + * @param int $depth |
|
| 205 | + * @return array |
|
| 206 | + */ |
|
| 207 | + public static function flatten($array, $depth = INF) |
|
| 208 | + { |
|
| 209 | + return array_reduce($array, function ($result, $item) use ($depth) { |
|
| 210 | + $item = $item instanceof Collection ? $item->all() : $item; |
|
| 211 | + |
|
| 212 | + if (! is_array($item)) { |
|
| 213 | + return array_merge($result, [$item]); |
|
| 214 | + } elseif ($depth === 1) { |
|
| 215 | + return array_merge($result, array_values($item)); |
|
| 216 | + } else { |
|
| 217 | + return array_merge($result, static::flatten($item, $depth - 1)); |
|
| 218 | + } |
|
| 219 | + }, []); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Remove one or many array items from a given array using "dot" notation. |
|
| 224 | + * |
|
| 225 | + * @param array $array |
|
| 226 | + * @param array|string $keys |
|
| 227 | + * @return void |
|
| 228 | + */ |
|
| 229 | + public static function forget(&$array, $keys) |
|
| 230 | + { |
|
| 231 | + $original = &$array; |
|
| 232 | + |
|
| 233 | + $keys = (array) $keys; |
|
| 234 | + |
|
| 235 | + if (count($keys) === 0) { |
|
| 236 | + return; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + foreach ($keys as $key) { |
|
| 240 | + // if the exact key exists in the top-level, remove it |
|
| 241 | + if (static::exists($array, $key)) { |
|
| 242 | + unset($array[$key]); |
|
| 243 | + |
|
| 244 | + continue; |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + $parts = explode('.', $key); |
|
| 248 | + |
|
| 249 | + // clean up before each pass |
|
| 250 | + $array = &$original; |
|
| 251 | + |
|
| 252 | + while (count($parts) > 1) { |
|
| 253 | + $part = array_shift($parts); |
|
| 254 | + |
|
| 255 | + if (isset($array[$part]) && is_array($array[$part])) { |
|
| 256 | + $array = &$array[$part]; |
|
| 257 | + } else { |
|
| 258 | + continue 2; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + unset($array[array_shift($parts)]); |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Get an item from an array using "dot" notation. |
|
| 268 | + * |
|
| 269 | + * @param \ArrayAccess|array $array |
|
| 270 | + * @param string $key |
|
| 271 | + * @param mixed $default |
|
| 272 | + * @return mixed |
|
| 273 | + */ |
|
| 274 | + public static function get($array, $key, $default = null) |
|
| 275 | + { |
|
| 276 | + if (! static::accessible($array)) { |
|
| 277 | + return value($default); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + if (is_null($key)) { |
|
| 281 | + return $array; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + if (static::exists($array, $key)) { |
|
| 285 | + return $array[$key]; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + foreach (explode('.', $key) as $segment) { |
|
| 289 | + if (static::accessible($array) && static::exists($array, $segment)) { |
|
| 290 | + $array = $array[$segment]; |
|
| 291 | + } else { |
|
| 292 | + return value($default); |
|
| 293 | + } |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + return $array; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + /** |
|
| 300 | + * Check if an item or items exist in an array using "dot" notation. |
|
| 301 | + * |
|
| 302 | + * @param \ArrayAccess|array $array |
|
| 303 | + * @param string|array $keys |
|
| 304 | + * @return bool |
|
| 305 | + */ |
|
| 306 | + public static function has($array, $keys) |
|
| 307 | + { |
|
| 308 | + if (is_null($keys)) { |
|
| 309 | + return false; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + $keys = (array) $keys; |
|
| 313 | + |
|
| 314 | + if (! $array) { |
|
| 315 | + return false; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + if ($keys === []) { |
|
| 319 | + return false; |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + foreach ($keys as $key) { |
|
| 323 | + $subKeyArray = $array; |
|
| 324 | + |
|
| 325 | + if (static::exists($array, $key)) { |
|
| 326 | + continue; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + foreach (explode('.', $key) as $segment) { |
|
| 330 | + if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) { |
|
| 331 | + $subKeyArray = $subKeyArray[$segment]; |
|
| 332 | + } else { |
|
| 333 | + return false; |
|
| 334 | + } |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + return true; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + /** |
|
| 342 | + * Determines if an array is associative. |
|
| 343 | + * |
|
| 344 | + * An array is "associative" if it doesn't have sequential numerical keys beginning with zero. |
|
| 345 | + * |
|
| 346 | + * @param array $array |
|
| 347 | + * @return bool |
|
| 348 | + */ |
|
| 349 | + public static function isAssoc(array $array) |
|
| 350 | + { |
|
| 351 | + $keys = array_keys($array); |
|
| 352 | + |
|
| 353 | + return array_keys($keys) !== $keys; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * Get a subset of the items from the given array. |
|
| 358 | + * |
|
| 359 | + * @param array $array |
|
| 360 | + * @param array|string $keys |
|
| 361 | + * @return array |
|
| 362 | + */ |
|
| 363 | + public static function only($array, $keys) |
|
| 364 | + { |
|
| 365 | + return array_intersect_key($array, array_flip((array) $keys)); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Pluck an array of values from an array. |
|
| 370 | + * |
|
| 371 | + * @param array $array |
|
| 372 | + * @param string|array $value |
|
| 373 | + * @param string|array|null $key |
|
| 374 | + * @return array |
|
| 375 | + */ |
|
| 376 | + public static function pluck($array, $value, $key = null) |
|
| 377 | + { |
|
| 378 | + $results = []; |
|
| 379 | + |
|
| 380 | + list($value, $key) = static::explodePluckParameters($value, $key); |
|
| 381 | + |
|
| 382 | + foreach ($array as $item) { |
|
| 383 | + $itemValue = data_get($item, $value); |
|
| 384 | + |
|
| 385 | + // If the key is "null", we will just append the value to the array and keep |
|
| 386 | + // looping. Otherwise we will key the array using the value of the key we |
|
| 387 | + // received from the developer. Then we'll return the final array form. |
|
| 388 | + if (is_null($key)) { |
|
| 389 | + $results[] = $itemValue; |
|
| 390 | + } else { |
|
| 391 | + $itemKey = data_get($item, $key); |
|
| 392 | + |
|
| 393 | + if (is_object($itemKey) && method_exists($itemKey, '__toString')) { |
|
| 394 | + $itemKey = (string) $itemKey; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + $results[$itemKey] = $itemValue; |
|
| 398 | + } |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + return $results; |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + /** |
|
| 405 | + * Explode the "value" and "key" arguments passed to "pluck". |
|
| 406 | + * |
|
| 407 | + * @param string|array $value |
|
| 408 | + * @param string|array|null $key |
|
| 409 | + * @return array |
|
| 410 | + */ |
|
| 411 | + protected static function explodePluckParameters($value, $key) |
|
| 412 | + { |
|
| 413 | + $value = is_string($value) ? explode('.', $value) : $value; |
|
| 414 | + |
|
| 415 | + $key = is_null($key) || is_array($key) ? $key : explode('.', $key); |
|
| 416 | + |
|
| 417 | + return [$value, $key]; |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + /** |
|
| 421 | + * Push an item onto the beginning of an array. |
|
| 422 | + * |
|
| 423 | + * @param array $array |
|
| 424 | + * @param mixed $value |
|
| 425 | + * @param mixed $key |
|
| 426 | + * @return array |
|
| 427 | + */ |
|
| 428 | + public static function prepend($array, $value, $key = null) |
|
| 429 | + { |
|
| 430 | + if (is_null($key)) { |
|
| 431 | + array_unshift($array, $value); |
|
| 432 | + } else { |
|
| 433 | + $array = [$key => $value] + $array; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + return $array; |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + /** |
|
| 440 | + * Get a value from the array, and remove it. |
|
| 441 | + * |
|
| 442 | + * @param array $array |
|
| 443 | + * @param string $key |
|
| 444 | + * @param mixed $default |
|
| 445 | + * @return mixed |
|
| 446 | + */ |
|
| 447 | + public static function pull(&$array, $key, $default = null) |
|
| 448 | + { |
|
| 449 | + $value = static::get($array, $key, $default); |
|
| 450 | + |
|
| 451 | + static::forget($array, $key); |
|
| 452 | + |
|
| 453 | + return $value; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Get one or a specified number of random values from an array. |
|
| 458 | + * |
|
| 459 | + * @param array $array |
|
| 460 | + * @param int|null $number |
|
| 461 | + * @return mixed |
|
| 462 | + * |
|
| 463 | + * @throws \InvalidArgumentException |
|
| 464 | + */ |
|
| 465 | + public static function random($array, $number = null) |
|
| 466 | + { |
|
| 467 | + $requested = is_null($number) ? 1 : $number; |
|
| 468 | + |
|
| 469 | + $count = count($array); |
|
| 470 | + |
|
| 471 | + if ($requested > $count) { |
|
| 472 | + throw new InvalidArgumentException( |
|
| 473 | + "You requested {$requested} items, but there are only {$count} items available." |
|
| 474 | + ); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + if (is_null($number)) { |
|
| 478 | + return $array[array_rand($array)]; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + if ((int) $number === 0) { |
|
| 482 | + return []; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + $keys = array_rand($array, $number); |
|
| 486 | + |
|
| 487 | + $results = []; |
|
| 488 | + |
|
| 489 | + foreach ((array) $keys as $key) { |
|
| 490 | + $results[] = $array[$key]; |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + return $results; |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Set an array item to a given value using "dot" notation. |
|
| 498 | + * |
|
| 499 | + * If no key is given to the method, the entire array will be replaced. |
|
| 500 | + * |
|
| 501 | + * @param array $array |
|
| 502 | + * @param string $key |
|
| 503 | + * @param mixed $value |
|
| 504 | + * @return array |
|
| 505 | + */ |
|
| 506 | + public static function set(&$array, $key, $value) |
|
| 507 | + { |
|
| 508 | + if (is_null($key)) { |
|
| 509 | + return $array = $value; |
|
| 510 | + } |
|
| 511 | + |
|
| 512 | + $keys = explode('.', $key); |
|
| 513 | + |
|
| 514 | + while (count($keys) > 1) { |
|
| 515 | + $key = array_shift($keys); |
|
| 516 | + |
|
| 517 | + // If the key doesn't exist at this depth, we will just create an empty array |
|
| 518 | + // to hold the next value, allowing us to create the arrays to hold final |
|
| 519 | + // values at the correct depth. Then we'll keep digging into the array. |
|
| 520 | + if (! isset($array[$key]) || ! is_array($array[$key])) { |
|
| 521 | + $array[$key] = []; |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + $array = &$array[$key]; |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + $array[array_shift($keys)] = $value; |
|
| 528 | + |
|
| 529 | + return $array; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + /** |
|
| 533 | + * Shuffle the given array and return the result. |
|
| 534 | + * |
|
| 535 | + * @param array $array |
|
| 536 | + * @return array |
|
| 537 | + */ |
|
| 538 | + public static function shuffle($array) |
|
| 539 | + { |
|
| 540 | + shuffle($array); |
|
| 541 | + |
|
| 542 | + return $array; |
|
| 543 | + } |
|
| 544 | + |
|
| 545 | + /** |
|
| 546 | + * Sort the array using the given callback or "dot" notation. |
|
| 547 | + * |
|
| 548 | + * @param array $array |
|
| 549 | + * @param callable|string $callback |
|
| 550 | + * @return array |
|
| 551 | + */ |
|
| 552 | + public static function sort($array, $callback) |
|
| 553 | + { |
|
| 554 | + return Collection::make($array)->sortBy($callback)->all(); |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Recursively sort an array by keys and values. |
|
| 559 | + * |
|
| 560 | + * @param array $array |
|
| 561 | + * @return array |
|
| 562 | + */ |
|
| 563 | + public static function sortRecursive($array) |
|
| 564 | + { |
|
| 565 | + foreach ($array as &$value) { |
|
| 566 | + if (is_array($value)) { |
|
| 567 | + $value = static::sortRecursive($value); |
|
| 568 | + } |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + if (static::isAssoc($array)) { |
|
| 572 | + ksort($array); |
|
| 573 | + } else { |
|
| 574 | + sort($array); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + return $array; |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + /** |
|
| 581 | + * Filter the array using the given callback. |
|
| 582 | + * |
|
| 583 | + * @param array $array |
|
| 584 | + * @param callable $callback |
|
| 585 | + * @return array |
|
| 586 | + */ |
|
| 587 | + public static function where($array, callable $callback) |
|
| 588 | + { |
|
| 589 | + return array_filter($array, $callback, ARRAY_FILTER_USE_BOTH); |
|
| 590 | + } |
|
| 591 | + |
|
| 592 | + /** |
|
| 593 | + * If the given value is not an array, wrap it in one. |
|
| 594 | + * |
|
| 595 | + * @param mixed $value |
|
| 596 | + * @return array |
|
| 597 | + */ |
|
| 598 | + public static function wrap($value) |
|
| 599 | + { |
|
| 600 | + return ! is_array($value) ? [$value] : $value; |
|
| 601 | + } |
|
| 602 | 602 | } |
@@ -6,24 +6,24 @@ |
||
| 6 | 6 | |
| 7 | 7 | class HtmlDumper extends SymfonyHtmlDumper |
| 8 | 8 | { |
| 9 | - /** |
|
| 10 | - * Colour definitions for output. |
|
| 11 | - * |
|
| 12 | - * @var array |
|
| 13 | - */ |
|
| 14 | - protected $styles = [ |
|
| 15 | - 'default' => 'background-color:#fff; color:#222; line-height:1.2em; font-weight:normal; font:12px Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:100000', |
|
| 16 | - 'num' => 'color:#a71d5d', |
|
| 17 | - 'const' => 'color:#795da3', |
|
| 18 | - 'str' => 'color:#df5000', |
|
| 19 | - 'cchr' => 'color:#222', |
|
| 20 | - 'note' => 'color:#a71d5d', |
|
| 21 | - 'ref' => 'color:#a0a0a0', |
|
| 22 | - 'public' => 'color:#795da3', |
|
| 23 | - 'protected' => 'color:#795da3', |
|
| 24 | - 'private' => 'color:#795da3', |
|
| 25 | - 'meta' => 'color:#b729d9', |
|
| 26 | - 'key' => 'color:#df5000', |
|
| 27 | - 'index' => 'color:#a71d5d', |
|
| 28 | - ]; |
|
| 9 | + /** |
|
| 10 | + * Colour definitions for output. |
|
| 11 | + * |
|
| 12 | + * @var array |
|
| 13 | + */ |
|
| 14 | + protected $styles = [ |
|
| 15 | + 'default' => 'background-color:#fff; color:#222; line-height:1.2em; font-weight:normal; font:12px Monaco, Consolas, monospace; word-wrap: break-word; white-space: pre-wrap; position:relative; z-index:100000', |
|
| 16 | + 'num' => 'color:#a71d5d', |
|
| 17 | + 'const' => 'color:#795da3', |
|
| 18 | + 'str' => 'color:#df5000', |
|
| 19 | + 'cchr' => 'color:#222', |
|
| 20 | + 'note' => 'color:#a71d5d', |
|
| 21 | + 'ref' => 'color:#a0a0a0', |
|
| 22 | + 'public' => 'color:#795da3', |
|
| 23 | + 'protected' => 'color:#795da3', |
|
| 24 | + 'private' => 'color:#795da3', |
|
| 25 | + 'meta' => 'color:#b729d9', |
|
| 26 | + 'key' => 'color:#df5000', |
|
| 27 | + 'index' => 'color:#a71d5d', |
|
| 28 | + ]; |
|
| 29 | 29 | } |
@@ -7,20 +7,20 @@ |
||
| 7 | 7 | |
| 8 | 8 | class Dumper |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * Dump a value with elegance. |
|
| 12 | - * |
|
| 13 | - * @param mixed $value |
|
| 14 | - * @return void |
|
| 15 | - */ |
|
| 16 | - public function dump($value) |
|
| 17 | - { |
|
| 18 | - if (class_exists(CliDumper::class)) { |
|
| 19 | - $dumper = 'cli' === PHP_SAPI ? new CliDumper : new HtmlDumper; |
|
| 10 | + /** |
|
| 11 | + * Dump a value with elegance. |
|
| 12 | + * |
|
| 13 | + * @param mixed $value |
|
| 14 | + * @return void |
|
| 15 | + */ |
|
| 16 | + public function dump($value) |
|
| 17 | + { |
|
| 18 | + if (class_exists(CliDumper::class)) { |
|
| 19 | + $dumper = 'cli' === PHP_SAPI ? new CliDumper : new HtmlDumper; |
|
| 20 | 20 | |
| 21 | - $dumper->dump((new VarCloner)->cloneVar($value)); |
|
| 22 | - } else { |
|
| 23 | - var_dump($value); |
|
| 24 | - } |
|
| 25 | - } |
|
| 21 | + $dumper->dump((new VarCloner)->cloneVar($value)); |
|
| 22 | + } else { |
|
| 23 | + var_dump($value); |
|
| 24 | + } |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -4,95 +4,95 @@ |
||
| 4 | 4 | |
| 5 | 5 | interface Log |
| 6 | 6 | { |
| 7 | - /** |
|
| 8 | - * Log an alert message to the logs. |
|
| 9 | - * |
|
| 10 | - * @param string $message |
|
| 11 | - * @param array $context |
|
| 12 | - * @return void |
|
| 13 | - */ |
|
| 14 | - public function alert($message, array $context = []); |
|
| 7 | + /** |
|
| 8 | + * Log an alert message to the logs. |
|
| 9 | + * |
|
| 10 | + * @param string $message |
|
| 11 | + * @param array $context |
|
| 12 | + * @return void |
|
| 13 | + */ |
|
| 14 | + public function alert($message, array $context = []); |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * Log a critical message to the logs. |
|
| 18 | - * |
|
| 19 | - * @param string $message |
|
| 20 | - * @param array $context |
|
| 21 | - * @return void |
|
| 22 | - */ |
|
| 23 | - public function critical($message, array $context = []); |
|
| 16 | + /** |
|
| 17 | + * Log a critical message to the logs. |
|
| 18 | + * |
|
| 19 | + * @param string $message |
|
| 20 | + * @param array $context |
|
| 21 | + * @return void |
|
| 22 | + */ |
|
| 23 | + public function critical($message, array $context = []); |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * Log an error message to the logs. |
|
| 27 | - * |
|
| 28 | - * @param string $message |
|
| 29 | - * @param array $context |
|
| 30 | - * @return void |
|
| 31 | - */ |
|
| 32 | - public function error($message, array $context = []); |
|
| 25 | + /** |
|
| 26 | + * Log an error message to the logs. |
|
| 27 | + * |
|
| 28 | + * @param string $message |
|
| 29 | + * @param array $context |
|
| 30 | + * @return void |
|
| 31 | + */ |
|
| 32 | + public function error($message, array $context = []); |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Log a warning message to the logs. |
|
| 36 | - * |
|
| 37 | - * @param string $message |
|
| 38 | - * @param array $context |
|
| 39 | - * @return void |
|
| 40 | - */ |
|
| 41 | - public function warning($message, array $context = []); |
|
| 34 | + /** |
|
| 35 | + * Log a warning message to the logs. |
|
| 36 | + * |
|
| 37 | + * @param string $message |
|
| 38 | + * @param array $context |
|
| 39 | + * @return void |
|
| 40 | + */ |
|
| 41 | + public function warning($message, array $context = []); |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Log a notice to the logs. |
|
| 45 | - * |
|
| 46 | - * @param string $message |
|
| 47 | - * @param array $context |
|
| 48 | - * @return void |
|
| 49 | - */ |
|
| 50 | - public function notice($message, array $context = []); |
|
| 43 | + /** |
|
| 44 | + * Log a notice to the logs. |
|
| 45 | + * |
|
| 46 | + * @param string $message |
|
| 47 | + * @param array $context |
|
| 48 | + * @return void |
|
| 49 | + */ |
|
| 50 | + public function notice($message, array $context = []); |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * Log an informational message to the logs. |
|
| 54 | - * |
|
| 55 | - * @param string $message |
|
| 56 | - * @param array $context |
|
| 57 | - * @return void |
|
| 58 | - */ |
|
| 59 | - public function info($message, array $context = []); |
|
| 52 | + /** |
|
| 53 | + * Log an informational message to the logs. |
|
| 54 | + * |
|
| 55 | + * @param string $message |
|
| 56 | + * @param array $context |
|
| 57 | + * @return void |
|
| 58 | + */ |
|
| 59 | + public function info($message, array $context = []); |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Log a debug message to the logs. |
|
| 63 | - * |
|
| 64 | - * @param string $message |
|
| 65 | - * @param array $context |
|
| 66 | - * @return void |
|
| 67 | - */ |
|
| 68 | - public function debug($message, array $context = []); |
|
| 61 | + /** |
|
| 62 | + * Log a debug message to the logs. |
|
| 63 | + * |
|
| 64 | + * @param string $message |
|
| 65 | + * @param array $context |
|
| 66 | + * @return void |
|
| 67 | + */ |
|
| 68 | + public function debug($message, array $context = []); |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Log a message to the logs. |
|
| 72 | - * |
|
| 73 | - * @param string $level |
|
| 74 | - * @param string $message |
|
| 75 | - * @param array $context |
|
| 76 | - * @return void |
|
| 77 | - */ |
|
| 78 | - public function log($level, $message, array $context = []); |
|
| 70 | + /** |
|
| 71 | + * Log a message to the logs. |
|
| 72 | + * |
|
| 73 | + * @param string $level |
|
| 74 | + * @param string $message |
|
| 75 | + * @param array $context |
|
| 76 | + * @return void |
|
| 77 | + */ |
|
| 78 | + public function log($level, $message, array $context = []); |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * Register a file log handler. |
|
| 82 | - * |
|
| 83 | - * @param string $path |
|
| 84 | - * @param string $level |
|
| 85 | - * @return void |
|
| 86 | - */ |
|
| 87 | - public function useFiles($path, $level = 'debug'); |
|
| 80 | + /** |
|
| 81 | + * Register a file log handler. |
|
| 82 | + * |
|
| 83 | + * @param string $path |
|
| 84 | + * @param string $level |
|
| 85 | + * @return void |
|
| 86 | + */ |
|
| 87 | + public function useFiles($path, $level = 'debug'); |
|
| 88 | 88 | |
| 89 | - /** |
|
| 90 | - * Register a daily file log handler. |
|
| 91 | - * |
|
| 92 | - * @param string $path |
|
| 93 | - * @param int $days |
|
| 94 | - * @param string $level |
|
| 95 | - * @return void |
|
| 96 | - */ |
|
| 97 | - public function useDailyFiles($path, $days = 0, $level = 'debug'); |
|
| 89 | + /** |
|
| 90 | + * Register a daily file log handler. |
|
| 91 | + * |
|
| 92 | + * @param string $path |
|
| 93 | + * @param int $days |
|
| 94 | + * @param string $level |
|
| 95 | + * @return void |
|
| 96 | + */ |
|
| 97 | + public function useDailyFiles($path, $days = 0, $level = 'debug'); |
|
| 98 | 98 | } |
@@ -42,519 +42,519 @@ discard block |
||
| 42 | 42 | */ |
| 43 | 43 | class ClassLoader |
| 44 | 44 | { |
| 45 | - /** @var ?string */ |
|
| 46 | - private $vendorDir; |
|
| 47 | - |
|
| 48 | - // PSR-4 |
|
| 49 | - /** |
|
| 50 | - * @var array[] |
|
| 51 | - * @psalm-var array<string, array<string, int>> |
|
| 52 | - */ |
|
| 53 | - private $prefixLengthsPsr4 = array(); |
|
| 54 | - /** |
|
| 55 | - * @var array[] |
|
| 56 | - * @psalm-var array<string, array<int, string>> |
|
| 57 | - */ |
|
| 58 | - private $prefixDirsPsr4 = array(); |
|
| 59 | - /** |
|
| 60 | - * @var array[] |
|
| 61 | - * @psalm-var array<string, string> |
|
| 62 | - */ |
|
| 63 | - private $fallbackDirsPsr4 = array(); |
|
| 64 | - |
|
| 65 | - // PSR-0 |
|
| 66 | - /** |
|
| 67 | - * @var array[] |
|
| 68 | - * @psalm-var array<string, array<string, string[]>> |
|
| 69 | - */ |
|
| 70 | - private $prefixesPsr0 = array(); |
|
| 71 | - /** |
|
| 72 | - * @var array[] |
|
| 73 | - * @psalm-var array<string, string> |
|
| 74 | - */ |
|
| 75 | - private $fallbackDirsPsr0 = array(); |
|
| 76 | - |
|
| 77 | - /** @var bool */ |
|
| 78 | - private $useIncludePath = false; |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @var string[] |
|
| 82 | - * @psalm-var array<string, string> |
|
| 83 | - */ |
|
| 84 | - private $classMap = array(); |
|
| 85 | - |
|
| 86 | - /** @var bool */ |
|
| 87 | - private $classMapAuthoritative = false; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @var bool[] |
|
| 91 | - * @psalm-var array<string, bool> |
|
| 92 | - */ |
|
| 93 | - private $missingClasses = array(); |
|
| 94 | - |
|
| 95 | - /** @var ?string */ |
|
| 96 | - private $apcuPrefix; |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * @var self[] |
|
| 100 | - */ |
|
| 101 | - private static $registeredLoaders = array(); |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param ?string $vendorDir |
|
| 105 | - */ |
|
| 106 | - public function __construct($vendorDir = null) |
|
| 107 | - { |
|
| 108 | - $this->vendorDir = $vendorDir; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @return string[] |
|
| 113 | - */ |
|
| 114 | - public function getPrefixes() |
|
| 115 | - { |
|
| 116 | - if (!empty($this->prefixesPsr0)) { |
|
| 117 | - return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return array(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * @return array[] |
|
| 125 | - * @psalm-return array<string, array<int, string>> |
|
| 126 | - */ |
|
| 127 | - public function getPrefixesPsr4() |
|
| 128 | - { |
|
| 129 | - return $this->prefixDirsPsr4; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @return array[] |
|
| 134 | - * @psalm-return array<string, string> |
|
| 135 | - */ |
|
| 136 | - public function getFallbackDirs() |
|
| 137 | - { |
|
| 138 | - return $this->fallbackDirsPsr0; |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @return array[] |
|
| 143 | - * @psalm-return array<string, string> |
|
| 144 | - */ |
|
| 145 | - public function getFallbackDirsPsr4() |
|
| 146 | - { |
|
| 147 | - return $this->fallbackDirsPsr4; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * @return string[] Array of classname => path |
|
| 152 | - * @psalm-return array<string, string> |
|
| 153 | - */ |
|
| 154 | - public function getClassMap() |
|
| 155 | - { |
|
| 156 | - return $this->classMap; |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - /** |
|
| 160 | - * @param string[] $classMap Class to filename map |
|
| 161 | - * @psalm-param array<string, string> $classMap |
|
| 162 | - * |
|
| 163 | - * @return void |
|
| 164 | - */ |
|
| 165 | - public function addClassMap(array $classMap) |
|
| 166 | - { |
|
| 167 | - if ($this->classMap) { |
|
| 168 | - $this->classMap = array_merge($this->classMap, $classMap); |
|
| 169 | - } else { |
|
| 170 | - $this->classMap = $classMap; |
|
| 171 | - } |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Registers a set of PSR-0 directories for a given prefix, either |
|
| 176 | - * appending or prepending to the ones previously set for this prefix. |
|
| 177 | - * |
|
| 178 | - * @param string $prefix The prefix |
|
| 179 | - * @param string[]|string $paths The PSR-0 root directories |
|
| 180 | - * @param bool $prepend Whether to prepend the directories |
|
| 181 | - * |
|
| 182 | - * @return void |
|
| 183 | - */ |
|
| 184 | - public function add($prefix, $paths, $prepend = false) |
|
| 185 | - { |
|
| 186 | - if (!$prefix) { |
|
| 187 | - if ($prepend) { |
|
| 188 | - $this->fallbackDirsPsr0 = array_merge( |
|
| 189 | - (array) $paths, |
|
| 190 | - $this->fallbackDirsPsr0 |
|
| 191 | - ); |
|
| 192 | - } else { |
|
| 193 | - $this->fallbackDirsPsr0 = array_merge( |
|
| 194 | - $this->fallbackDirsPsr0, |
|
| 195 | - (array) $paths |
|
| 196 | - ); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $first = $prefix[0]; |
|
| 203 | - if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
| 204 | - $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
| 205 | - |
|
| 206 | - return; |
|
| 207 | - } |
|
| 208 | - if ($prepend) { |
|
| 209 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
| 210 | - (array) $paths, |
|
| 211 | - $this->prefixesPsr0[$first][$prefix] |
|
| 212 | - ); |
|
| 213 | - } else { |
|
| 214 | - $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
| 215 | - $this->prefixesPsr0[$first][$prefix], |
|
| 216 | - (array) $paths |
|
| 217 | - ); |
|
| 218 | - } |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * Registers a set of PSR-4 directories for a given namespace, either |
|
| 223 | - * appending or prepending to the ones previously set for this namespace. |
|
| 224 | - * |
|
| 225 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
| 226 | - * @param string[]|string $paths The PSR-4 base directories |
|
| 227 | - * @param bool $prepend Whether to prepend the directories |
|
| 228 | - * |
|
| 229 | - * @throws \InvalidArgumentException |
|
| 230 | - * |
|
| 231 | - * @return void |
|
| 232 | - */ |
|
| 233 | - public function addPsr4($prefix, $paths, $prepend = false) |
|
| 234 | - { |
|
| 235 | - if (!$prefix) { |
|
| 236 | - // Register directories for the root namespace. |
|
| 237 | - if ($prepend) { |
|
| 238 | - $this->fallbackDirsPsr4 = array_merge( |
|
| 239 | - (array) $paths, |
|
| 240 | - $this->fallbackDirsPsr4 |
|
| 241 | - ); |
|
| 242 | - } else { |
|
| 243 | - $this->fallbackDirsPsr4 = array_merge( |
|
| 244 | - $this->fallbackDirsPsr4, |
|
| 245 | - (array) $paths |
|
| 246 | - ); |
|
| 247 | - } |
|
| 248 | - } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
| 249 | - // Register directories for a new namespace. |
|
| 250 | - $length = strlen($prefix); |
|
| 251 | - if ('\\' !== $prefix[$length - 1]) { |
|
| 252 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
| 253 | - } |
|
| 254 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
| 255 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
| 256 | - } elseif ($prepend) { |
|
| 257 | - // Prepend directories for an already registered namespace. |
|
| 258 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
| 259 | - (array) $paths, |
|
| 260 | - $this->prefixDirsPsr4[$prefix] |
|
| 261 | - ); |
|
| 262 | - } else { |
|
| 263 | - // Append directories for an already registered namespace. |
|
| 264 | - $this->prefixDirsPsr4[$prefix] = array_merge( |
|
| 265 | - $this->prefixDirsPsr4[$prefix], |
|
| 266 | - (array) $paths |
|
| 267 | - ); |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - /** |
|
| 272 | - * Registers a set of PSR-0 directories for a given prefix, |
|
| 273 | - * replacing any others previously set for this prefix. |
|
| 274 | - * |
|
| 275 | - * @param string $prefix The prefix |
|
| 276 | - * @param string[]|string $paths The PSR-0 base directories |
|
| 277 | - * |
|
| 278 | - * @return void |
|
| 279 | - */ |
|
| 280 | - public function set($prefix, $paths) |
|
| 281 | - { |
|
| 282 | - if (!$prefix) { |
|
| 283 | - $this->fallbackDirsPsr0 = (array) $paths; |
|
| 284 | - } else { |
|
| 285 | - $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * Registers a set of PSR-4 directories for a given namespace, |
|
| 291 | - * replacing any others previously set for this namespace. |
|
| 292 | - * |
|
| 293 | - * @param string $prefix The prefix/namespace, with trailing '\\' |
|
| 294 | - * @param string[]|string $paths The PSR-4 base directories |
|
| 295 | - * |
|
| 296 | - * @throws \InvalidArgumentException |
|
| 297 | - * |
|
| 298 | - * @return void |
|
| 299 | - */ |
|
| 300 | - public function setPsr4($prefix, $paths) |
|
| 301 | - { |
|
| 302 | - if (!$prefix) { |
|
| 303 | - $this->fallbackDirsPsr4 = (array) $paths; |
|
| 304 | - } else { |
|
| 305 | - $length = strlen($prefix); |
|
| 306 | - if ('\\' !== $prefix[$length - 1]) { |
|
| 307 | - throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
| 308 | - } |
|
| 309 | - $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
| 310 | - $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
| 311 | - } |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - /** |
|
| 315 | - * Turns on searching the include path for class files. |
|
| 316 | - * |
|
| 317 | - * @param bool $useIncludePath |
|
| 318 | - * |
|
| 319 | - * @return void |
|
| 320 | - */ |
|
| 321 | - public function setUseIncludePath($useIncludePath) |
|
| 322 | - { |
|
| 323 | - $this->useIncludePath = $useIncludePath; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Can be used to check if the autoloader uses the include path to check |
|
| 328 | - * for classes. |
|
| 329 | - * |
|
| 330 | - * @return bool |
|
| 331 | - */ |
|
| 332 | - public function getUseIncludePath() |
|
| 333 | - { |
|
| 334 | - return $this->useIncludePath; |
|
| 335 | - } |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Turns off searching the prefix and fallback directories for classes |
|
| 339 | - * that have not been registered with the class map. |
|
| 340 | - * |
|
| 341 | - * @param bool $classMapAuthoritative |
|
| 342 | - * |
|
| 343 | - * @return void |
|
| 344 | - */ |
|
| 345 | - public function setClassMapAuthoritative($classMapAuthoritative) |
|
| 346 | - { |
|
| 347 | - $this->classMapAuthoritative = $classMapAuthoritative; |
|
| 348 | - } |
|
| 349 | - |
|
| 350 | - /** |
|
| 351 | - * Should class lookup fail if not found in the current class map? |
|
| 352 | - * |
|
| 353 | - * @return bool |
|
| 354 | - */ |
|
| 355 | - public function isClassMapAuthoritative() |
|
| 356 | - { |
|
| 357 | - return $this->classMapAuthoritative; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
| 362 | - * |
|
| 363 | - * @param string|null $apcuPrefix |
|
| 364 | - * |
|
| 365 | - * @return void |
|
| 366 | - */ |
|
| 367 | - public function setApcuPrefix($apcuPrefix) |
|
| 368 | - { |
|
| 369 | - $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
| 370 | - } |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * The APCu prefix in use, or null if APCu caching is not enabled. |
|
| 374 | - * |
|
| 375 | - * @return string|null |
|
| 376 | - */ |
|
| 377 | - public function getApcuPrefix() |
|
| 378 | - { |
|
| 379 | - return $this->apcuPrefix; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * Registers this instance as an autoloader. |
|
| 384 | - * |
|
| 385 | - * @param bool $prepend Whether to prepend the autoloader or not |
|
| 386 | - * |
|
| 387 | - * @return void |
|
| 388 | - */ |
|
| 389 | - public function register($prepend = false) |
|
| 390 | - { |
|
| 391 | - spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
| 392 | - |
|
| 393 | - if (null === $this->vendorDir) { |
|
| 394 | - return; |
|
| 395 | - } |
|
| 396 | - |
|
| 397 | - if ($prepend) { |
|
| 398 | - self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; |
|
| 399 | - } else { |
|
| 400 | - unset(self::$registeredLoaders[$this->vendorDir]); |
|
| 401 | - self::$registeredLoaders[$this->vendorDir] = $this; |
|
| 402 | - } |
|
| 403 | - } |
|
| 404 | - |
|
| 405 | - /** |
|
| 406 | - * Unregisters this instance as an autoloader. |
|
| 407 | - * |
|
| 408 | - * @return void |
|
| 409 | - */ |
|
| 410 | - public function unregister() |
|
| 411 | - { |
|
| 412 | - spl_autoload_unregister(array($this, 'loadClass')); |
|
| 413 | - |
|
| 414 | - if (null !== $this->vendorDir) { |
|
| 415 | - unset(self::$registeredLoaders[$this->vendorDir]); |
|
| 416 | - } |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - /** |
|
| 420 | - * Loads the given class or interface. |
|
| 421 | - * |
|
| 422 | - * @param string $class The name of the class |
|
| 423 | - * @return true|null True if loaded, null otherwise |
|
| 424 | - */ |
|
| 425 | - public function loadClass($class) |
|
| 426 | - { |
|
| 427 | - if ($file = $this->findFile($class)) { |
|
| 428 | - includeFile($file); |
|
| 429 | - |
|
| 430 | - return true; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - return null; |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - /** |
|
| 437 | - * Finds the path to the file where the class is defined. |
|
| 438 | - * |
|
| 439 | - * @param string $class The name of the class |
|
| 440 | - * |
|
| 441 | - * @return string|false The path if found, false otherwise |
|
| 442 | - */ |
|
| 443 | - public function findFile($class) |
|
| 444 | - { |
|
| 445 | - // class map lookup |
|
| 446 | - if (isset($this->classMap[$class])) { |
|
| 447 | - return $this->classMap[$class]; |
|
| 448 | - } |
|
| 449 | - if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
| 450 | - return false; |
|
| 451 | - } |
|
| 452 | - if (null !== $this->apcuPrefix) { |
|
| 453 | - $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
| 454 | - if ($hit) { |
|
| 455 | - return $file; |
|
| 456 | - } |
|
| 457 | - } |
|
| 458 | - |
|
| 459 | - $file = $this->findFileWithExtension($class, '.php'); |
|
| 460 | - |
|
| 461 | - // Search for Hack files if we are running on HHVM |
|
| 462 | - if (false === $file && defined('HHVM_VERSION')) { |
|
| 463 | - $file = $this->findFileWithExtension($class, '.hh'); |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - if (null !== $this->apcuPrefix) { |
|
| 467 | - apcu_add($this->apcuPrefix.$class, $file); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - if (false === $file) { |
|
| 471 | - // Remember that this class does not exist. |
|
| 472 | - $this->missingClasses[$class] = true; |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - return $file; |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - /** |
|
| 479 | - * Returns the currently registered loaders indexed by their corresponding vendor directories. |
|
| 480 | - * |
|
| 481 | - * @return self[] |
|
| 482 | - */ |
|
| 483 | - public static function getRegisteredLoaders() |
|
| 484 | - { |
|
| 485 | - return self::$registeredLoaders; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * @param string $class |
|
| 490 | - * @param string $ext |
|
| 491 | - * @return string|false |
|
| 492 | - */ |
|
| 493 | - private function findFileWithExtension($class, $ext) |
|
| 494 | - { |
|
| 495 | - // PSR-4 lookup |
|
| 496 | - $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
| 497 | - |
|
| 498 | - $first = $class[0]; |
|
| 499 | - if (isset($this->prefixLengthsPsr4[$first])) { |
|
| 500 | - $subPath = $class; |
|
| 501 | - while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
| 502 | - $subPath = substr($subPath, 0, $lastPos); |
|
| 503 | - $search = $subPath . '\\'; |
|
| 504 | - if (isset($this->prefixDirsPsr4[$search])) { |
|
| 505 | - $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
| 506 | - foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
| 507 | - if (file_exists($file = $dir . $pathEnd)) { |
|
| 508 | - return $file; |
|
| 509 | - } |
|
| 510 | - } |
|
| 511 | - } |
|
| 512 | - } |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - // PSR-4 fallback dirs |
|
| 516 | - foreach ($this->fallbackDirsPsr4 as $dir) { |
|
| 517 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
| 518 | - return $file; |
|
| 519 | - } |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - // PSR-0 lookup |
|
| 523 | - if (false !== $pos = strrpos($class, '\\')) { |
|
| 524 | - // namespaced class name |
|
| 525 | - $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
| 526 | - . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
| 527 | - } else { |
|
| 528 | - // PEAR-like class name |
|
| 529 | - $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
| 530 | - } |
|
| 531 | - |
|
| 532 | - if (isset($this->prefixesPsr0[$first])) { |
|
| 533 | - foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
| 534 | - if (0 === strpos($class, $prefix)) { |
|
| 535 | - foreach ($dirs as $dir) { |
|
| 536 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 537 | - return $file; |
|
| 538 | - } |
|
| 539 | - } |
|
| 540 | - } |
|
| 541 | - } |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - // PSR-0 fallback dirs |
|
| 545 | - foreach ($this->fallbackDirsPsr0 as $dir) { |
|
| 546 | - if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 547 | - return $file; |
|
| 548 | - } |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - // PSR-0 include paths. |
|
| 552 | - if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
| 553 | - return $file; |
|
| 554 | - } |
|
| 555 | - |
|
| 556 | - return false; |
|
| 557 | - } |
|
| 45 | + /** @var ?string */ |
|
| 46 | + private $vendorDir; |
|
| 47 | + |
|
| 48 | + // PSR-4 |
|
| 49 | + /** |
|
| 50 | + * @var array[] |
|
| 51 | + * @psalm-var array<string, array<string, int>> |
|
| 52 | + */ |
|
| 53 | + private $prefixLengthsPsr4 = array(); |
|
| 54 | + /** |
|
| 55 | + * @var array[] |
|
| 56 | + * @psalm-var array<string, array<int, string>> |
|
| 57 | + */ |
|
| 58 | + private $prefixDirsPsr4 = array(); |
|
| 59 | + /** |
|
| 60 | + * @var array[] |
|
| 61 | + * @psalm-var array<string, string> |
|
| 62 | + */ |
|
| 63 | + private $fallbackDirsPsr4 = array(); |
|
| 64 | + |
|
| 65 | + // PSR-0 |
|
| 66 | + /** |
|
| 67 | + * @var array[] |
|
| 68 | + * @psalm-var array<string, array<string, string[]>> |
|
| 69 | + */ |
|
| 70 | + private $prefixesPsr0 = array(); |
|
| 71 | + /** |
|
| 72 | + * @var array[] |
|
| 73 | + * @psalm-var array<string, string> |
|
| 74 | + */ |
|
| 75 | + private $fallbackDirsPsr0 = array(); |
|
| 76 | + |
|
| 77 | + /** @var bool */ |
|
| 78 | + private $useIncludePath = false; |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @var string[] |
|
| 82 | + * @psalm-var array<string, string> |
|
| 83 | + */ |
|
| 84 | + private $classMap = array(); |
|
| 85 | + |
|
| 86 | + /** @var bool */ |
|
| 87 | + private $classMapAuthoritative = false; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @var bool[] |
|
| 91 | + * @psalm-var array<string, bool> |
|
| 92 | + */ |
|
| 93 | + private $missingClasses = array(); |
|
| 94 | + |
|
| 95 | + /** @var ?string */ |
|
| 96 | + private $apcuPrefix; |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * @var self[] |
|
| 100 | + */ |
|
| 101 | + private static $registeredLoaders = array(); |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param ?string $vendorDir |
|
| 105 | + */ |
|
| 106 | + public function __construct($vendorDir = null) |
|
| 107 | + { |
|
| 108 | + $this->vendorDir = $vendorDir; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @return string[] |
|
| 113 | + */ |
|
| 114 | + public function getPrefixes() |
|
| 115 | + { |
|
| 116 | + if (!empty($this->prefixesPsr0)) { |
|
| 117 | + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return array(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * @return array[] |
|
| 125 | + * @psalm-return array<string, array<int, string>> |
|
| 126 | + */ |
|
| 127 | + public function getPrefixesPsr4() |
|
| 128 | + { |
|
| 129 | + return $this->prefixDirsPsr4; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @return array[] |
|
| 134 | + * @psalm-return array<string, string> |
|
| 135 | + */ |
|
| 136 | + public function getFallbackDirs() |
|
| 137 | + { |
|
| 138 | + return $this->fallbackDirsPsr0; |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @return array[] |
|
| 143 | + * @psalm-return array<string, string> |
|
| 144 | + */ |
|
| 145 | + public function getFallbackDirsPsr4() |
|
| 146 | + { |
|
| 147 | + return $this->fallbackDirsPsr4; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * @return string[] Array of classname => path |
|
| 152 | + * @psalm-return array<string, string> |
|
| 153 | + */ |
|
| 154 | + public function getClassMap() |
|
| 155 | + { |
|
| 156 | + return $this->classMap; |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + /** |
|
| 160 | + * @param string[] $classMap Class to filename map |
|
| 161 | + * @psalm-param array<string, string> $classMap |
|
| 162 | + * |
|
| 163 | + * @return void |
|
| 164 | + */ |
|
| 165 | + public function addClassMap(array $classMap) |
|
| 166 | + { |
|
| 167 | + if ($this->classMap) { |
|
| 168 | + $this->classMap = array_merge($this->classMap, $classMap); |
|
| 169 | + } else { |
|
| 170 | + $this->classMap = $classMap; |
|
| 171 | + } |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Registers a set of PSR-0 directories for a given prefix, either |
|
| 176 | + * appending or prepending to the ones previously set for this prefix. |
|
| 177 | + * |
|
| 178 | + * @param string $prefix The prefix |
|
| 179 | + * @param string[]|string $paths The PSR-0 root directories |
|
| 180 | + * @param bool $prepend Whether to prepend the directories |
|
| 181 | + * |
|
| 182 | + * @return void |
|
| 183 | + */ |
|
| 184 | + public function add($prefix, $paths, $prepend = false) |
|
| 185 | + { |
|
| 186 | + if (!$prefix) { |
|
| 187 | + if ($prepend) { |
|
| 188 | + $this->fallbackDirsPsr0 = array_merge( |
|
| 189 | + (array) $paths, |
|
| 190 | + $this->fallbackDirsPsr0 |
|
| 191 | + ); |
|
| 192 | + } else { |
|
| 193 | + $this->fallbackDirsPsr0 = array_merge( |
|
| 194 | + $this->fallbackDirsPsr0, |
|
| 195 | + (array) $paths |
|
| 196 | + ); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $first = $prefix[0]; |
|
| 203 | + if (!isset($this->prefixesPsr0[$first][$prefix])) { |
|
| 204 | + $this->prefixesPsr0[$first][$prefix] = (array) $paths; |
|
| 205 | + |
|
| 206 | + return; |
|
| 207 | + } |
|
| 208 | + if ($prepend) { |
|
| 209 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
| 210 | + (array) $paths, |
|
| 211 | + $this->prefixesPsr0[$first][$prefix] |
|
| 212 | + ); |
|
| 213 | + } else { |
|
| 214 | + $this->prefixesPsr0[$first][$prefix] = array_merge( |
|
| 215 | + $this->prefixesPsr0[$first][$prefix], |
|
| 216 | + (array) $paths |
|
| 217 | + ); |
|
| 218 | + } |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * Registers a set of PSR-4 directories for a given namespace, either |
|
| 223 | + * appending or prepending to the ones previously set for this namespace. |
|
| 224 | + * |
|
| 225 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
| 226 | + * @param string[]|string $paths The PSR-4 base directories |
|
| 227 | + * @param bool $prepend Whether to prepend the directories |
|
| 228 | + * |
|
| 229 | + * @throws \InvalidArgumentException |
|
| 230 | + * |
|
| 231 | + * @return void |
|
| 232 | + */ |
|
| 233 | + public function addPsr4($prefix, $paths, $prepend = false) |
|
| 234 | + { |
|
| 235 | + if (!$prefix) { |
|
| 236 | + // Register directories for the root namespace. |
|
| 237 | + if ($prepend) { |
|
| 238 | + $this->fallbackDirsPsr4 = array_merge( |
|
| 239 | + (array) $paths, |
|
| 240 | + $this->fallbackDirsPsr4 |
|
| 241 | + ); |
|
| 242 | + } else { |
|
| 243 | + $this->fallbackDirsPsr4 = array_merge( |
|
| 244 | + $this->fallbackDirsPsr4, |
|
| 245 | + (array) $paths |
|
| 246 | + ); |
|
| 247 | + } |
|
| 248 | + } elseif (!isset($this->prefixDirsPsr4[$prefix])) { |
|
| 249 | + // Register directories for a new namespace. |
|
| 250 | + $length = strlen($prefix); |
|
| 251 | + if ('\\' !== $prefix[$length - 1]) { |
|
| 252 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
| 253 | + } |
|
| 254 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
| 255 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
| 256 | + } elseif ($prepend) { |
|
| 257 | + // Prepend directories for an already registered namespace. |
|
| 258 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
| 259 | + (array) $paths, |
|
| 260 | + $this->prefixDirsPsr4[$prefix] |
|
| 261 | + ); |
|
| 262 | + } else { |
|
| 263 | + // Append directories for an already registered namespace. |
|
| 264 | + $this->prefixDirsPsr4[$prefix] = array_merge( |
|
| 265 | + $this->prefixDirsPsr4[$prefix], |
|
| 266 | + (array) $paths |
|
| 267 | + ); |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + /** |
|
| 272 | + * Registers a set of PSR-0 directories for a given prefix, |
|
| 273 | + * replacing any others previously set for this prefix. |
|
| 274 | + * |
|
| 275 | + * @param string $prefix The prefix |
|
| 276 | + * @param string[]|string $paths The PSR-0 base directories |
|
| 277 | + * |
|
| 278 | + * @return void |
|
| 279 | + */ |
|
| 280 | + public function set($prefix, $paths) |
|
| 281 | + { |
|
| 282 | + if (!$prefix) { |
|
| 283 | + $this->fallbackDirsPsr0 = (array) $paths; |
|
| 284 | + } else { |
|
| 285 | + $this->prefixesPsr0[$prefix[0]][$prefix] = (array) $paths; |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * Registers a set of PSR-4 directories for a given namespace, |
|
| 291 | + * replacing any others previously set for this namespace. |
|
| 292 | + * |
|
| 293 | + * @param string $prefix The prefix/namespace, with trailing '\\' |
|
| 294 | + * @param string[]|string $paths The PSR-4 base directories |
|
| 295 | + * |
|
| 296 | + * @throws \InvalidArgumentException |
|
| 297 | + * |
|
| 298 | + * @return void |
|
| 299 | + */ |
|
| 300 | + public function setPsr4($prefix, $paths) |
|
| 301 | + { |
|
| 302 | + if (!$prefix) { |
|
| 303 | + $this->fallbackDirsPsr4 = (array) $paths; |
|
| 304 | + } else { |
|
| 305 | + $length = strlen($prefix); |
|
| 306 | + if ('\\' !== $prefix[$length - 1]) { |
|
| 307 | + throw new \InvalidArgumentException("A non-empty PSR-4 prefix must end with a namespace separator."); |
|
| 308 | + } |
|
| 309 | + $this->prefixLengthsPsr4[$prefix[0]][$prefix] = $length; |
|
| 310 | + $this->prefixDirsPsr4[$prefix] = (array) $paths; |
|
| 311 | + } |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + /** |
|
| 315 | + * Turns on searching the include path for class files. |
|
| 316 | + * |
|
| 317 | + * @param bool $useIncludePath |
|
| 318 | + * |
|
| 319 | + * @return void |
|
| 320 | + */ |
|
| 321 | + public function setUseIncludePath($useIncludePath) |
|
| 322 | + { |
|
| 323 | + $this->useIncludePath = $useIncludePath; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Can be used to check if the autoloader uses the include path to check |
|
| 328 | + * for classes. |
|
| 329 | + * |
|
| 330 | + * @return bool |
|
| 331 | + */ |
|
| 332 | + public function getUseIncludePath() |
|
| 333 | + { |
|
| 334 | + return $this->useIncludePath; |
|
| 335 | + } |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Turns off searching the prefix and fallback directories for classes |
|
| 339 | + * that have not been registered with the class map. |
|
| 340 | + * |
|
| 341 | + * @param bool $classMapAuthoritative |
|
| 342 | + * |
|
| 343 | + * @return void |
|
| 344 | + */ |
|
| 345 | + public function setClassMapAuthoritative($classMapAuthoritative) |
|
| 346 | + { |
|
| 347 | + $this->classMapAuthoritative = $classMapAuthoritative; |
|
| 348 | + } |
|
| 349 | + |
|
| 350 | + /** |
|
| 351 | + * Should class lookup fail if not found in the current class map? |
|
| 352 | + * |
|
| 353 | + * @return bool |
|
| 354 | + */ |
|
| 355 | + public function isClassMapAuthoritative() |
|
| 356 | + { |
|
| 357 | + return $this->classMapAuthoritative; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * APCu prefix to use to cache found/not-found classes, if the extension is enabled. |
|
| 362 | + * |
|
| 363 | + * @param string|null $apcuPrefix |
|
| 364 | + * |
|
| 365 | + * @return void |
|
| 366 | + */ |
|
| 367 | + public function setApcuPrefix($apcuPrefix) |
|
| 368 | + { |
|
| 369 | + $this->apcuPrefix = function_exists('apcu_fetch') && filter_var(ini_get('apc.enabled'), FILTER_VALIDATE_BOOLEAN) ? $apcuPrefix : null; |
|
| 370 | + } |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * The APCu prefix in use, or null if APCu caching is not enabled. |
|
| 374 | + * |
|
| 375 | + * @return string|null |
|
| 376 | + */ |
|
| 377 | + public function getApcuPrefix() |
|
| 378 | + { |
|
| 379 | + return $this->apcuPrefix; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * Registers this instance as an autoloader. |
|
| 384 | + * |
|
| 385 | + * @param bool $prepend Whether to prepend the autoloader or not |
|
| 386 | + * |
|
| 387 | + * @return void |
|
| 388 | + */ |
|
| 389 | + public function register($prepend = false) |
|
| 390 | + { |
|
| 391 | + spl_autoload_register(array($this, 'loadClass'), true, $prepend); |
|
| 392 | + |
|
| 393 | + if (null === $this->vendorDir) { |
|
| 394 | + return; |
|
| 395 | + } |
|
| 396 | + |
|
| 397 | + if ($prepend) { |
|
| 398 | + self::$registeredLoaders = array($this->vendorDir => $this) + self::$registeredLoaders; |
|
| 399 | + } else { |
|
| 400 | + unset(self::$registeredLoaders[$this->vendorDir]); |
|
| 401 | + self::$registeredLoaders[$this->vendorDir] = $this; |
|
| 402 | + } |
|
| 403 | + } |
|
| 404 | + |
|
| 405 | + /** |
|
| 406 | + * Unregisters this instance as an autoloader. |
|
| 407 | + * |
|
| 408 | + * @return void |
|
| 409 | + */ |
|
| 410 | + public function unregister() |
|
| 411 | + { |
|
| 412 | + spl_autoload_unregister(array($this, 'loadClass')); |
|
| 413 | + |
|
| 414 | + if (null !== $this->vendorDir) { |
|
| 415 | + unset(self::$registeredLoaders[$this->vendorDir]); |
|
| 416 | + } |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + /** |
|
| 420 | + * Loads the given class or interface. |
|
| 421 | + * |
|
| 422 | + * @param string $class The name of the class |
|
| 423 | + * @return true|null True if loaded, null otherwise |
|
| 424 | + */ |
|
| 425 | + public function loadClass($class) |
|
| 426 | + { |
|
| 427 | + if ($file = $this->findFile($class)) { |
|
| 428 | + includeFile($file); |
|
| 429 | + |
|
| 430 | + return true; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + return null; |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + /** |
|
| 437 | + * Finds the path to the file where the class is defined. |
|
| 438 | + * |
|
| 439 | + * @param string $class The name of the class |
|
| 440 | + * |
|
| 441 | + * @return string|false The path if found, false otherwise |
|
| 442 | + */ |
|
| 443 | + public function findFile($class) |
|
| 444 | + { |
|
| 445 | + // class map lookup |
|
| 446 | + if (isset($this->classMap[$class])) { |
|
| 447 | + return $this->classMap[$class]; |
|
| 448 | + } |
|
| 449 | + if ($this->classMapAuthoritative || isset($this->missingClasses[$class])) { |
|
| 450 | + return false; |
|
| 451 | + } |
|
| 452 | + if (null !== $this->apcuPrefix) { |
|
| 453 | + $file = apcu_fetch($this->apcuPrefix.$class, $hit); |
|
| 454 | + if ($hit) { |
|
| 455 | + return $file; |
|
| 456 | + } |
|
| 457 | + } |
|
| 458 | + |
|
| 459 | + $file = $this->findFileWithExtension($class, '.php'); |
|
| 460 | + |
|
| 461 | + // Search for Hack files if we are running on HHVM |
|
| 462 | + if (false === $file && defined('HHVM_VERSION')) { |
|
| 463 | + $file = $this->findFileWithExtension($class, '.hh'); |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + if (null !== $this->apcuPrefix) { |
|
| 467 | + apcu_add($this->apcuPrefix.$class, $file); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + if (false === $file) { |
|
| 471 | + // Remember that this class does not exist. |
|
| 472 | + $this->missingClasses[$class] = true; |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + return $file; |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + /** |
|
| 479 | + * Returns the currently registered loaders indexed by their corresponding vendor directories. |
|
| 480 | + * |
|
| 481 | + * @return self[] |
|
| 482 | + */ |
|
| 483 | + public static function getRegisteredLoaders() |
|
| 484 | + { |
|
| 485 | + return self::$registeredLoaders; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * @param string $class |
|
| 490 | + * @param string $ext |
|
| 491 | + * @return string|false |
|
| 492 | + */ |
|
| 493 | + private function findFileWithExtension($class, $ext) |
|
| 494 | + { |
|
| 495 | + // PSR-4 lookup |
|
| 496 | + $logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . $ext; |
|
| 497 | + |
|
| 498 | + $first = $class[0]; |
|
| 499 | + if (isset($this->prefixLengthsPsr4[$first])) { |
|
| 500 | + $subPath = $class; |
|
| 501 | + while (false !== $lastPos = strrpos($subPath, '\\')) { |
|
| 502 | + $subPath = substr($subPath, 0, $lastPos); |
|
| 503 | + $search = $subPath . '\\'; |
|
| 504 | + if (isset($this->prefixDirsPsr4[$search])) { |
|
| 505 | + $pathEnd = DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $lastPos + 1); |
|
| 506 | + foreach ($this->prefixDirsPsr4[$search] as $dir) { |
|
| 507 | + if (file_exists($file = $dir . $pathEnd)) { |
|
| 508 | + return $file; |
|
| 509 | + } |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | + } |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + // PSR-4 fallback dirs |
|
| 516 | + foreach ($this->fallbackDirsPsr4 as $dir) { |
|
| 517 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) { |
|
| 518 | + return $file; |
|
| 519 | + } |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + // PSR-0 lookup |
|
| 523 | + if (false !== $pos = strrpos($class, '\\')) { |
|
| 524 | + // namespaced class name |
|
| 525 | + $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) |
|
| 526 | + . strtr(substr($logicalPathPsr4, $pos + 1), '_', DIRECTORY_SEPARATOR); |
|
| 527 | + } else { |
|
| 528 | + // PEAR-like class name |
|
| 529 | + $logicalPathPsr0 = strtr($class, '_', DIRECTORY_SEPARATOR) . $ext; |
|
| 530 | + } |
|
| 531 | + |
|
| 532 | + if (isset($this->prefixesPsr0[$first])) { |
|
| 533 | + foreach ($this->prefixesPsr0[$first] as $prefix => $dirs) { |
|
| 534 | + if (0 === strpos($class, $prefix)) { |
|
| 535 | + foreach ($dirs as $dir) { |
|
| 536 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 537 | + return $file; |
|
| 538 | + } |
|
| 539 | + } |
|
| 540 | + } |
|
| 541 | + } |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + // PSR-0 fallback dirs |
|
| 545 | + foreach ($this->fallbackDirsPsr0 as $dir) { |
|
| 546 | + if (file_exists($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr0)) { |
|
| 547 | + return $file; |
|
| 548 | + } |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + // PSR-0 include paths. |
|
| 552 | + if ($this->useIncludePath && $file = stream_resolve_include_path($logicalPathPsr0)) { |
|
| 553 | + return $file; |
|
| 554 | + } |
|
| 555 | + |
|
| 556 | + return false; |
|
| 557 | + } |
|
| 558 | 558 | } |
| 559 | 559 | |
| 560 | 560 | /** |
@@ -568,5 +568,5 @@ discard block |
||
| 568 | 568 | */ |
| 569 | 569 | function includeFile($file) |
| 570 | 570 | { |
| 571 | - include $file; |
|
| 571 | + include $file; |
|
| 572 | 572 | } |