@@ -48,1069 +48,1069 @@ |
||
| 48 | 48 | */ |
| 49 | 49 | trait EnumeratesValues |
| 50 | 50 | { |
| 51 | - /** |
|
| 52 | - * Indicates that the object's string representation should be escaped when __toString is invoked. |
|
| 53 | - * |
|
| 54 | - * @var bool |
|
| 55 | - */ |
|
| 56 | - protected $escapeWhenCastingToString = false; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * The methods that can be proxied. |
|
| 60 | - * |
|
| 61 | - * @var string[] |
|
| 62 | - */ |
|
| 63 | - protected static $proxies = [ |
|
| 64 | - 'average', |
|
| 65 | - 'avg', |
|
| 66 | - 'contains', |
|
| 67 | - 'doesntContain', |
|
| 68 | - 'each', |
|
| 69 | - 'every', |
|
| 70 | - 'filter', |
|
| 71 | - 'first', |
|
| 72 | - 'flatMap', |
|
| 73 | - 'groupBy', |
|
| 74 | - 'keyBy', |
|
| 75 | - 'map', |
|
| 76 | - 'max', |
|
| 77 | - 'min', |
|
| 78 | - 'partition', |
|
| 79 | - 'reject', |
|
| 80 | - 'skipUntil', |
|
| 81 | - 'skipWhile', |
|
| 82 | - 'some', |
|
| 83 | - 'sortBy', |
|
| 84 | - 'sortByDesc', |
|
| 85 | - 'sum', |
|
| 86 | - 'takeUntil', |
|
| 87 | - 'takeWhile', |
|
| 88 | - 'unique', |
|
| 89 | - 'until', |
|
| 90 | - ]; |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Create a new collection instance if the value isn't one already. |
|
| 94 | - * |
|
| 95 | - * @param mixed $items |
|
| 96 | - * @return static |
|
| 97 | - */ |
|
| 98 | - public static function make($items = []) |
|
| 99 | - { |
|
| 100 | - return new static($items); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Wrap the given value in a collection if applicable. |
|
| 105 | - * |
|
| 106 | - * @param mixed $value |
|
| 107 | - * @return static |
|
| 108 | - */ |
|
| 109 | - public static function wrap($value) |
|
| 110 | - { |
|
| 111 | - return $value instanceof Enumerable |
|
| 112 | - ? new static($value) |
|
| 113 | - : new static(Arr::wrap($value)); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - /** |
|
| 117 | - * Get the underlying items from the given collection if applicable. |
|
| 118 | - * |
|
| 119 | - * @param array|static $value |
|
| 120 | - * @return array |
|
| 121 | - */ |
|
| 122 | - public static function unwrap($value) |
|
| 123 | - { |
|
| 124 | - return $value instanceof Enumerable ? $value->all() : $value; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Create a new instance with no items. |
|
| 129 | - * |
|
| 130 | - * @return static |
|
| 131 | - */ |
|
| 132 | - public static function empty() |
|
| 133 | - { |
|
| 134 | - return new static([]); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * Create a new collection by invoking the callback a given amount of times. |
|
| 139 | - * |
|
| 140 | - * @param int $number |
|
| 141 | - * @param callable|null $callback |
|
| 142 | - * @return static |
|
| 143 | - */ |
|
| 144 | - public static function times($number, callable $callback = null) |
|
| 145 | - { |
|
| 146 | - if ($number < 1) { |
|
| 147 | - return new static; |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return static::range(1, $number) |
|
| 151 | - ->when($callback) |
|
| 152 | - ->map($callback); |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Alias for the "avg" method. |
|
| 157 | - * |
|
| 158 | - * @param callable|string|null $callback |
|
| 159 | - * @return mixed |
|
| 160 | - */ |
|
| 161 | - public function average($callback = null) |
|
| 162 | - { |
|
| 163 | - return $this->avg($callback); |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Alias for the "contains" method. |
|
| 168 | - * |
|
| 169 | - * @param mixed $key |
|
| 170 | - * @param mixed $operator |
|
| 171 | - * @param mixed $value |
|
| 172 | - * @return bool |
|
| 173 | - */ |
|
| 174 | - public function some($key, $operator = null, $value = null) |
|
| 175 | - { |
|
| 176 | - return $this->contains(...func_get_args()); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Determine if an item exists, using strict comparison. |
|
| 181 | - * |
|
| 182 | - * @param mixed $key |
|
| 183 | - * @param mixed $value |
|
| 184 | - * @return bool |
|
| 185 | - */ |
|
| 186 | - public function containsStrict($key, $value = null) |
|
| 187 | - { |
|
| 188 | - if (func_num_args() === 2) { |
|
| 189 | - return $this->contains(function ($item) use ($key, $value) { |
|
| 190 | - return data_get($item, $key) === $value; |
|
| 191 | - }); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - if ($this->useAsCallable($key)) { |
|
| 195 | - return ! is_null($this->first($key)); |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - foreach ($this as $item) { |
|
| 199 | - if ($item === $key) { |
|
| 200 | - return true; |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - return false; |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - /** |
|
| 208 | - * Dump the items and end the script. |
|
| 209 | - * |
|
| 210 | - * @param mixed ...$args |
|
| 211 | - * @return void |
|
| 212 | - */ |
|
| 213 | - public function dd(...$args) |
|
| 214 | - { |
|
| 215 | - $this->dump(...$args); |
|
| 216 | - |
|
| 217 | - exit(1); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - /** |
|
| 221 | - * Dump the items. |
|
| 222 | - * |
|
| 223 | - * @return $this |
|
| 224 | - */ |
|
| 225 | - public function dump() |
|
| 226 | - { |
|
| 227 | - (new Collection(func_get_args())) |
|
| 228 | - ->push($this->all()) |
|
| 229 | - ->each(function ($item) { |
|
| 230 | - VarDumper::dump($item); |
|
| 231 | - }); |
|
| 232 | - |
|
| 233 | - return $this; |
|
| 234 | - } |
|
| 235 | - |
|
| 236 | - /** |
|
| 237 | - * Execute a callback over each item. |
|
| 238 | - * |
|
| 239 | - * @param callable $callback |
|
| 240 | - * @return $this |
|
| 241 | - */ |
|
| 242 | - public function each(callable $callback) |
|
| 243 | - { |
|
| 244 | - foreach ($this as $key => $item) { |
|
| 245 | - if ($callback($item, $key) === false) { |
|
| 246 | - break; |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - |
|
| 250 | - return $this; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * Execute a callback over each nested chunk of items. |
|
| 255 | - * |
|
| 256 | - * @param callable $callback |
|
| 257 | - * @return static |
|
| 258 | - */ |
|
| 259 | - public function eachSpread(callable $callback) |
|
| 260 | - { |
|
| 261 | - return $this->each(function ($chunk, $key) use ($callback) { |
|
| 262 | - $chunk[] = $key; |
|
| 263 | - |
|
| 264 | - return $callback(...$chunk); |
|
| 265 | - }); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Determine if all items pass the given truth test. |
|
| 270 | - * |
|
| 271 | - * @param string|callable $key |
|
| 272 | - * @param mixed $operator |
|
| 273 | - * @param mixed $value |
|
| 274 | - * @return bool |
|
| 275 | - */ |
|
| 276 | - public function every($key, $operator = null, $value = null) |
|
| 277 | - { |
|
| 278 | - if (func_num_args() === 1) { |
|
| 279 | - $callback = $this->valueRetriever($key); |
|
| 280 | - |
|
| 281 | - foreach ($this as $k => $v) { |
|
| 282 | - if (! $callback($v, $k)) { |
|
| 283 | - return false; |
|
| 284 | - } |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - return true; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - return $this->every($this->operatorForWhere(...func_get_args())); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Get the first item by the given key value pair. |
|
| 295 | - * |
|
| 296 | - * @param string $key |
|
| 297 | - * @param mixed $operator |
|
| 298 | - * @param mixed $value |
|
| 299 | - * @return mixed |
|
| 300 | - */ |
|
| 301 | - public function firstWhere($key, $operator = null, $value = null) |
|
| 302 | - { |
|
| 303 | - return $this->first($this->operatorForWhere(...func_get_args())); |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Determine if the collection is not empty. |
|
| 308 | - * |
|
| 309 | - * @return bool |
|
| 310 | - */ |
|
| 311 | - public function isNotEmpty() |
|
| 312 | - { |
|
| 313 | - return ! $this->isEmpty(); |
|
| 314 | - } |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * Run a map over each nested chunk of items. |
|
| 318 | - * |
|
| 319 | - * @param callable $callback |
|
| 320 | - * @return static |
|
| 321 | - */ |
|
| 322 | - public function mapSpread(callable $callback) |
|
| 323 | - { |
|
| 324 | - return $this->map(function ($chunk, $key) use ($callback) { |
|
| 325 | - $chunk[] = $key; |
|
| 326 | - |
|
| 327 | - return $callback(...$chunk); |
|
| 328 | - }); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - /** |
|
| 332 | - * Run a grouping map over the items. |
|
| 333 | - * |
|
| 334 | - * The callback should return an associative array with a single key/value pair. |
|
| 335 | - * |
|
| 336 | - * @param callable $callback |
|
| 337 | - * @return static |
|
| 338 | - */ |
|
| 339 | - public function mapToGroups(callable $callback) |
|
| 340 | - { |
|
| 341 | - $groups = $this->mapToDictionary($callback); |
|
| 342 | - |
|
| 343 | - return $groups->map([$this, 'make']); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Map a collection and flatten the result by a single level. |
|
| 348 | - * |
|
| 349 | - * @param callable $callback |
|
| 350 | - * @return static |
|
| 351 | - */ |
|
| 352 | - public function flatMap(callable $callback) |
|
| 353 | - { |
|
| 354 | - return $this->map($callback)->collapse(); |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * Map the values into a new class. |
|
| 359 | - * |
|
| 360 | - * @param string $class |
|
| 361 | - * @return static |
|
| 362 | - */ |
|
| 363 | - public function mapInto($class) |
|
| 364 | - { |
|
| 365 | - return $this->map(function ($value, $key) use ($class) { |
|
| 366 | - return new $class($value, $key); |
|
| 367 | - }); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - /** |
|
| 371 | - * Get the min value of a given key. |
|
| 372 | - * |
|
| 373 | - * @param callable|string|null $callback |
|
| 374 | - * @return mixed |
|
| 375 | - */ |
|
| 376 | - public function min($callback = null) |
|
| 377 | - { |
|
| 378 | - $callback = $this->valueRetriever($callback); |
|
| 379 | - |
|
| 380 | - return $this->map(function ($value) use ($callback) { |
|
| 381 | - return $callback($value); |
|
| 382 | - })->filter(function ($value) { |
|
| 383 | - return ! is_null($value); |
|
| 384 | - })->reduce(function ($result, $value) { |
|
| 385 | - return is_null($result) || $value < $result ? $value : $result; |
|
| 386 | - }); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - /** |
|
| 390 | - * Get the max value of a given key. |
|
| 391 | - * |
|
| 392 | - * @param callable|string|null $callback |
|
| 393 | - * @return mixed |
|
| 394 | - */ |
|
| 395 | - public function max($callback = null) |
|
| 396 | - { |
|
| 397 | - $callback = $this->valueRetriever($callback); |
|
| 398 | - |
|
| 399 | - return $this->filter(function ($value) { |
|
| 400 | - return ! is_null($value); |
|
| 401 | - })->reduce(function ($result, $item) use ($callback) { |
|
| 402 | - $value = $callback($item); |
|
| 403 | - |
|
| 404 | - return is_null($result) || $value > $result ? $value : $result; |
|
| 405 | - }); |
|
| 406 | - } |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * "Paginate" the collection by slicing it into a smaller collection. |
|
| 410 | - * |
|
| 411 | - * @param int $page |
|
| 412 | - * @param int $perPage |
|
| 413 | - * @return static |
|
| 414 | - */ |
|
| 415 | - public function forPage($page, $perPage) |
|
| 416 | - { |
|
| 417 | - $offset = max(0, ($page - 1) * $perPage); |
|
| 418 | - |
|
| 419 | - return $this->slice($offset, $perPage); |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - /** |
|
| 423 | - * Partition the collection into two arrays using the given callback or key. |
|
| 424 | - * |
|
| 425 | - * @param callable|string $key |
|
| 426 | - * @param mixed $operator |
|
| 427 | - * @param mixed $value |
|
| 428 | - * @return static |
|
| 429 | - */ |
|
| 430 | - public function partition($key, $operator = null, $value = null) |
|
| 431 | - { |
|
| 432 | - $passed = []; |
|
| 433 | - $failed = []; |
|
| 434 | - |
|
| 435 | - $callback = func_num_args() === 1 |
|
| 436 | - ? $this->valueRetriever($key) |
|
| 437 | - : $this->operatorForWhere(...func_get_args()); |
|
| 438 | - |
|
| 439 | - foreach ($this as $key => $item) { |
|
| 440 | - if ($callback($item, $key)) { |
|
| 441 | - $passed[$key] = $item; |
|
| 442 | - } else { |
|
| 443 | - $failed[$key] = $item; |
|
| 444 | - } |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - return new static([new static($passed), new static($failed)]); |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * Get the sum of the given values. |
|
| 452 | - * |
|
| 453 | - * @param callable|string|null $callback |
|
| 454 | - * @return mixed |
|
| 455 | - */ |
|
| 456 | - public function sum($callback = null) |
|
| 457 | - { |
|
| 458 | - $callback = is_null($callback) |
|
| 459 | - ? $this->identity() |
|
| 460 | - : $this->valueRetriever($callback); |
|
| 461 | - |
|
| 462 | - return $this->reduce(function ($result, $item) use ($callback) { |
|
| 463 | - return $result + $callback($item); |
|
| 464 | - }, 0); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - /** |
|
| 468 | - * Apply the callback if the value is truthy. |
|
| 469 | - * |
|
| 470 | - * @param bool|mixed $value |
|
| 471 | - * @param callable|null $callback |
|
| 472 | - * @param callable|null $default |
|
| 473 | - * @return static|mixed |
|
| 474 | - */ |
|
| 475 | - public function when($value, callable $callback = null, callable $default = null) |
|
| 476 | - { |
|
| 477 | - if (! $callback) { |
|
| 478 | - return new HigherOrderWhenProxy($this, $value); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - if ($value) { |
|
| 482 | - return $callback($this, $value); |
|
| 483 | - } elseif ($default) { |
|
| 484 | - return $default($this, $value); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - return $this; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - /** |
|
| 491 | - * Apply the callback if the collection is empty. |
|
| 492 | - * |
|
| 493 | - * @param callable $callback |
|
| 494 | - * @param callable|null $default |
|
| 495 | - * @return static|mixed |
|
| 496 | - */ |
|
| 497 | - public function whenEmpty(callable $callback, callable $default = null) |
|
| 498 | - { |
|
| 499 | - return $this->when($this->isEmpty(), $callback, $default); |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * Apply the callback if the collection is not empty. |
|
| 504 | - * |
|
| 505 | - * @param callable $callback |
|
| 506 | - * @param callable|null $default |
|
| 507 | - * @return static|mixed |
|
| 508 | - */ |
|
| 509 | - public function whenNotEmpty(callable $callback, callable $default = null) |
|
| 510 | - { |
|
| 511 | - return $this->when($this->isNotEmpty(), $callback, $default); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - /** |
|
| 515 | - * Apply the callback if the value is falsy. |
|
| 516 | - * |
|
| 517 | - * @param bool $value |
|
| 518 | - * @param callable $callback |
|
| 519 | - * @param callable|null $default |
|
| 520 | - * @return static|mixed |
|
| 521 | - */ |
|
| 522 | - public function unless($value, callable $callback, callable $default = null) |
|
| 523 | - { |
|
| 524 | - return $this->when(! $value, $callback, $default); |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - /** |
|
| 528 | - * Apply the callback unless the collection is empty. |
|
| 529 | - * |
|
| 530 | - * @param callable $callback |
|
| 531 | - * @param callable|null $default |
|
| 532 | - * @return static|mixed |
|
| 533 | - */ |
|
| 534 | - public function unlessEmpty(callable $callback, callable $default = null) |
|
| 535 | - { |
|
| 536 | - return $this->whenNotEmpty($callback, $default); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * Apply the callback unless the collection is not empty. |
|
| 541 | - * |
|
| 542 | - * @param callable $callback |
|
| 543 | - * @param callable|null $default |
|
| 544 | - * @return static|mixed |
|
| 545 | - */ |
|
| 546 | - public function unlessNotEmpty(callable $callback, callable $default = null) |
|
| 547 | - { |
|
| 548 | - return $this->whenEmpty($callback, $default); |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Filter items by the given key value pair. |
|
| 553 | - * |
|
| 554 | - * @param string $key |
|
| 555 | - * @param mixed $operator |
|
| 556 | - * @param mixed $value |
|
| 557 | - * @return static |
|
| 558 | - */ |
|
| 559 | - public function where($key, $operator = null, $value = null) |
|
| 560 | - { |
|
| 561 | - return $this->filter($this->operatorForWhere(...func_get_args())); |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * Filter items where the value for the given key is null. |
|
| 566 | - * |
|
| 567 | - * @param string|null $key |
|
| 568 | - * @return static |
|
| 569 | - */ |
|
| 570 | - public function whereNull($key = null) |
|
| 571 | - { |
|
| 572 | - return $this->whereStrict($key, null); |
|
| 573 | - } |
|
| 574 | - |
|
| 575 | - /** |
|
| 576 | - * Filter items where the value for the given key is not null. |
|
| 577 | - * |
|
| 578 | - * @param string|null $key |
|
| 579 | - * @return static |
|
| 580 | - */ |
|
| 581 | - public function whereNotNull($key = null) |
|
| 582 | - { |
|
| 583 | - return $this->where($key, '!==', null); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * Filter items by the given key value pair using strict comparison. |
|
| 588 | - * |
|
| 589 | - * @param string $key |
|
| 590 | - * @param mixed $value |
|
| 591 | - * @return static |
|
| 592 | - */ |
|
| 593 | - public function whereStrict($key, $value) |
|
| 594 | - { |
|
| 595 | - return $this->where($key, '===', $value); |
|
| 596 | - } |
|
| 597 | - |
|
| 598 | - /** |
|
| 599 | - * Filter items by the given key value pair. |
|
| 600 | - * |
|
| 601 | - * @param string $key |
|
| 602 | - * @param mixed $values |
|
| 603 | - * @param bool $strict |
|
| 604 | - * @return static |
|
| 605 | - */ |
|
| 606 | - public function whereIn($key, $values, $strict = false) |
|
| 607 | - { |
|
| 608 | - $values = $this->getArrayableItems($values); |
|
| 609 | - |
|
| 610 | - return $this->filter(function ($item) use ($key, $values, $strict) { |
|
| 611 | - return in_array(data_get($item, $key), $values, $strict); |
|
| 612 | - }); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - /** |
|
| 616 | - * Filter items by the given key value pair using strict comparison. |
|
| 617 | - * |
|
| 618 | - * @param string $key |
|
| 619 | - * @param mixed $values |
|
| 620 | - * @return static |
|
| 621 | - */ |
|
| 622 | - public function whereInStrict($key, $values) |
|
| 623 | - { |
|
| 624 | - return $this->whereIn($key, $values, true); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - /** |
|
| 628 | - * Filter items such that the value of the given key is between the given values. |
|
| 629 | - * |
|
| 630 | - * @param string $key |
|
| 631 | - * @param array $values |
|
| 632 | - * @return static |
|
| 633 | - */ |
|
| 634 | - public function whereBetween($key, $values) |
|
| 635 | - { |
|
| 636 | - return $this->where($key, '>=', reset($values))->where($key, '<=', end($values)); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Filter items such that the value of the given key is not between the given values. |
|
| 641 | - * |
|
| 642 | - * @param string $key |
|
| 643 | - * @param array $values |
|
| 644 | - * @return static |
|
| 645 | - */ |
|
| 646 | - public function whereNotBetween($key, $values) |
|
| 647 | - { |
|
| 648 | - return $this->filter(function ($item) use ($key, $values) { |
|
| 649 | - return data_get($item, $key) < reset($values) || data_get($item, $key) > end($values); |
|
| 650 | - }); |
|
| 651 | - } |
|
| 652 | - |
|
| 653 | - /** |
|
| 654 | - * Filter items by the given key value pair. |
|
| 655 | - * |
|
| 656 | - * @param string $key |
|
| 657 | - * @param mixed $values |
|
| 658 | - * @param bool $strict |
|
| 659 | - * @return static |
|
| 660 | - */ |
|
| 661 | - public function whereNotIn($key, $values, $strict = false) |
|
| 662 | - { |
|
| 663 | - $values = $this->getArrayableItems($values); |
|
| 664 | - |
|
| 665 | - return $this->reject(function ($item) use ($key, $values, $strict) { |
|
| 666 | - return in_array(data_get($item, $key), $values, $strict); |
|
| 667 | - }); |
|
| 668 | - } |
|
| 669 | - |
|
| 670 | - /** |
|
| 671 | - * Filter items by the given key value pair using strict comparison. |
|
| 672 | - * |
|
| 673 | - * @param string $key |
|
| 674 | - * @param mixed $values |
|
| 675 | - * @return static |
|
| 676 | - */ |
|
| 677 | - public function whereNotInStrict($key, $values) |
|
| 678 | - { |
|
| 679 | - return $this->whereNotIn($key, $values, true); |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - /** |
|
| 683 | - * Filter the items, removing any items that don't match the given type(s). |
|
| 684 | - * |
|
| 685 | - * @param string|string[] $type |
|
| 686 | - * @return static |
|
| 687 | - */ |
|
| 688 | - public function whereInstanceOf($type) |
|
| 689 | - { |
|
| 690 | - return $this->filter(function ($value) use ($type) { |
|
| 691 | - if (is_array($type)) { |
|
| 692 | - foreach ($type as $classType) { |
|
| 693 | - if ($value instanceof $classType) { |
|
| 694 | - return true; |
|
| 695 | - } |
|
| 696 | - } |
|
| 697 | - |
|
| 698 | - return false; |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - return $value instanceof $type; |
|
| 702 | - }); |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * Pass the collection to the given callback and return the result. |
|
| 707 | - * |
|
| 708 | - * @param callable $callback |
|
| 709 | - * @return mixed |
|
| 710 | - */ |
|
| 711 | - public function pipe(callable $callback) |
|
| 712 | - { |
|
| 713 | - return $callback($this); |
|
| 714 | - } |
|
| 715 | - |
|
| 716 | - /** |
|
| 717 | - * Pass the collection into a new class. |
|
| 718 | - * |
|
| 719 | - * @param string $class |
|
| 720 | - * @return mixed |
|
| 721 | - */ |
|
| 722 | - public function pipeInto($class) |
|
| 723 | - { |
|
| 724 | - return new $class($this); |
|
| 725 | - } |
|
| 726 | - |
|
| 727 | - /** |
|
| 728 | - * Pass the collection through a series of callable pipes and return the result. |
|
| 729 | - * |
|
| 730 | - * @param array<callable> $pipes |
|
| 731 | - * @return mixed |
|
| 732 | - */ |
|
| 733 | - public function pipeThrough($pipes) |
|
| 734 | - { |
|
| 735 | - return static::make($pipes)->reduce( |
|
| 736 | - function ($carry, $pipe) { |
|
| 737 | - return $pipe($carry); |
|
| 738 | - }, |
|
| 739 | - $this |
|
| 740 | - ); |
|
| 741 | - } |
|
| 742 | - |
|
| 743 | - /** |
|
| 744 | - * Pass the collection to the given callback and then return it. |
|
| 745 | - * |
|
| 746 | - * @param callable $callback |
|
| 747 | - * @return $this |
|
| 748 | - */ |
|
| 749 | - public function tap(callable $callback) |
|
| 750 | - { |
|
| 751 | - $callback(clone $this); |
|
| 752 | - |
|
| 753 | - return $this; |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - /** |
|
| 757 | - * Reduce the collection to a single value. |
|
| 758 | - * |
|
| 759 | - * @param callable $callback |
|
| 760 | - * @param mixed $initial |
|
| 761 | - * @return mixed |
|
| 762 | - */ |
|
| 763 | - public function reduce(callable $callback, $initial = null) |
|
| 764 | - { |
|
| 765 | - $result = $initial; |
|
| 766 | - |
|
| 767 | - foreach ($this as $key => $value) { |
|
| 768 | - $result = $callback($result, $value, $key); |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - return $result; |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - /** |
|
| 775 | - * Reduce the collection to multiple aggregate values. |
|
| 776 | - * |
|
| 777 | - * @param callable $callback |
|
| 778 | - * @param mixed ...$initial |
|
| 779 | - * @return array |
|
| 780 | - * |
|
| 781 | - * @deprecated Use "reduceSpread" instead |
|
| 782 | - * |
|
| 783 | - * @throws \UnexpectedValueException |
|
| 784 | - */ |
|
| 785 | - public function reduceMany(callable $callback, ...$initial) |
|
| 786 | - { |
|
| 787 | - return $this->reduceSpread($callback, ...$initial); |
|
| 788 | - } |
|
| 789 | - |
|
| 790 | - /** |
|
| 791 | - * Reduce the collection to multiple aggregate values. |
|
| 792 | - * |
|
| 793 | - * @param callable $callback |
|
| 794 | - * @param mixed ...$initial |
|
| 795 | - * @return array |
|
| 796 | - * |
|
| 797 | - * @throws \UnexpectedValueException |
|
| 798 | - */ |
|
| 799 | - public function reduceSpread(callable $callback, ...$initial) |
|
| 800 | - { |
|
| 801 | - $result = $initial; |
|
| 802 | - |
|
| 803 | - foreach ($this as $key => $value) { |
|
| 804 | - $result = call_user_func_array($callback, array_merge($result, [$value, $key])); |
|
| 805 | - |
|
| 806 | - if (! is_array($result)) { |
|
| 807 | - throw new UnexpectedValueException(sprintf( |
|
| 808 | - "%s::reduceMany expects reducer to return an array, but got a '%s' instead.", |
|
| 809 | - class_basename(static::class), gettype($result) |
|
| 810 | - )); |
|
| 811 | - } |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - return $result; |
|
| 815 | - } |
|
| 816 | - |
|
| 817 | - /** |
|
| 818 | - * Reduce an associative collection to a single value. |
|
| 819 | - * |
|
| 820 | - * @param callable $callback |
|
| 821 | - * @param mixed $initial |
|
| 822 | - * @return mixed |
|
| 823 | - */ |
|
| 824 | - public function reduceWithKeys(callable $callback, $initial = null) |
|
| 825 | - { |
|
| 826 | - return $this->reduce($callback, $initial); |
|
| 827 | - } |
|
| 828 | - |
|
| 829 | - /** |
|
| 830 | - * Create a collection of all elements that do not pass a given truth test. |
|
| 831 | - * |
|
| 832 | - * @param callable|mixed $callback |
|
| 833 | - * @return static |
|
| 834 | - */ |
|
| 835 | - public function reject($callback = true) |
|
| 836 | - { |
|
| 837 | - $useAsCallable = $this->useAsCallable($callback); |
|
| 838 | - |
|
| 839 | - return $this->filter(function ($value, $key) use ($callback, $useAsCallable) { |
|
| 840 | - return $useAsCallable |
|
| 841 | - ? ! $callback($value, $key) |
|
| 842 | - : $value != $callback; |
|
| 843 | - }); |
|
| 844 | - } |
|
| 845 | - |
|
| 846 | - /** |
|
| 847 | - * Return only unique items from the collection array using strict comparison. |
|
| 848 | - * |
|
| 849 | - * @param string|callable|null $key |
|
| 850 | - * @return static |
|
| 851 | - */ |
|
| 852 | - public function uniqueStrict($key = null) |
|
| 853 | - { |
|
| 854 | - return $this->unique($key, true); |
|
| 855 | - } |
|
| 856 | - |
|
| 857 | - /** |
|
| 858 | - * Collect the values into a collection. |
|
| 859 | - * |
|
| 860 | - * @return \Illuminate\Support\Collection |
|
| 861 | - */ |
|
| 862 | - public function collect() |
|
| 863 | - { |
|
| 864 | - return new Collection($this->all()); |
|
| 865 | - } |
|
| 866 | - |
|
| 867 | - /** |
|
| 868 | - * Get the collection of items as a plain array. |
|
| 869 | - * |
|
| 870 | - * @return array |
|
| 871 | - */ |
|
| 872 | - public function toArray() |
|
| 873 | - { |
|
| 874 | - return $this->map(function ($value) { |
|
| 875 | - return $value instanceof Arrayable ? $value->toArray() : $value; |
|
| 876 | - })->all(); |
|
| 877 | - } |
|
| 878 | - |
|
| 879 | - /** |
|
| 880 | - * Convert the object into something JSON serializable. |
|
| 881 | - * |
|
| 882 | - * @return array |
|
| 883 | - */ |
|
| 884 | - #[\ReturnTypeWillChange] |
|
| 885 | - public function jsonSerialize() |
|
| 886 | - { |
|
| 887 | - return array_map(function ($value) { |
|
| 888 | - if ($value instanceof JsonSerializable) { |
|
| 889 | - return $value->jsonSerialize(); |
|
| 890 | - } elseif ($value instanceof Jsonable) { |
|
| 891 | - return json_decode($value->toJson(), true); |
|
| 892 | - } elseif ($value instanceof Arrayable) { |
|
| 893 | - return $value->toArray(); |
|
| 894 | - } |
|
| 895 | - |
|
| 896 | - return $value; |
|
| 897 | - }, $this->all()); |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - /** |
|
| 901 | - * Get the collection of items as JSON. |
|
| 902 | - * |
|
| 903 | - * @param int $options |
|
| 904 | - * @return string |
|
| 905 | - */ |
|
| 906 | - public function toJson($options = 0) |
|
| 907 | - { |
|
| 908 | - return json_encode($this->jsonSerialize(), $options); |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - /** |
|
| 912 | - * Get a CachingIterator instance. |
|
| 913 | - * |
|
| 914 | - * @param int $flags |
|
| 915 | - * @return \CachingIterator |
|
| 916 | - */ |
|
| 917 | - public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
| 918 | - { |
|
| 919 | - return new CachingIterator($this->getIterator(), $flags); |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - /** |
|
| 923 | - * Convert the collection to its string representation. |
|
| 924 | - * |
|
| 925 | - * @return string |
|
| 926 | - */ |
|
| 927 | - public function __toString() |
|
| 928 | - { |
|
| 929 | - return $this->escapeWhenCastingToString |
|
| 930 | - ? e($this->toJson()) |
|
| 931 | - : $this->toJson(); |
|
| 932 | - } |
|
| 933 | - |
|
| 934 | - /** |
|
| 935 | - * Indicate that the model's string representation should be escaped when __toString is invoked. |
|
| 936 | - * |
|
| 937 | - * @param bool $escape |
|
| 938 | - * @return $this |
|
| 939 | - */ |
|
| 940 | - public function escapeWhenCastingToString($escape = true) |
|
| 941 | - { |
|
| 942 | - $this->escapeWhenCastingToString = $escape; |
|
| 943 | - |
|
| 944 | - return $this; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - /** |
|
| 948 | - * Add a method to the list of proxied methods. |
|
| 949 | - * |
|
| 950 | - * @param string $method |
|
| 951 | - * @return void |
|
| 952 | - */ |
|
| 953 | - public static function proxy($method) |
|
| 954 | - { |
|
| 955 | - static::$proxies[] = $method; |
|
| 956 | - } |
|
| 957 | - |
|
| 958 | - /** |
|
| 959 | - * Dynamically access collection proxies. |
|
| 960 | - * |
|
| 961 | - * @param string $key |
|
| 962 | - * @return mixed |
|
| 963 | - * |
|
| 964 | - * @throws \Exception |
|
| 965 | - */ |
|
| 966 | - public function __get($key) |
|
| 967 | - { |
|
| 968 | - if (! in_array($key, static::$proxies)) { |
|
| 969 | - throw new Exception("Property [{$key}] does not exist on this collection instance."); |
|
| 970 | - } |
|
| 971 | - |
|
| 972 | - return new HigherOrderCollectionProxy($this, $key); |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - /** |
|
| 976 | - * Results array of items from Collection or Arrayable. |
|
| 977 | - * |
|
| 978 | - * @param mixed $items |
|
| 979 | - * @return array |
|
| 980 | - */ |
|
| 981 | - protected function getArrayableItems($items) |
|
| 982 | - { |
|
| 983 | - if (is_array($items)) { |
|
| 984 | - return $items; |
|
| 985 | - } elseif ($items instanceof Enumerable) { |
|
| 986 | - return $items->all(); |
|
| 987 | - } elseif ($items instanceof Arrayable) { |
|
| 988 | - return $items->toArray(); |
|
| 989 | - } elseif ($items instanceof Jsonable) { |
|
| 990 | - return json_decode($items->toJson(), true); |
|
| 991 | - } elseif ($items instanceof JsonSerializable) { |
|
| 992 | - return (array) $items->jsonSerialize(); |
|
| 993 | - } elseif ($items instanceof Traversable) { |
|
| 994 | - return iterator_to_array($items); |
|
| 995 | - } elseif ($items instanceof UnitEnum) { |
|
| 996 | - return [$items]; |
|
| 997 | - } |
|
| 998 | - |
|
| 999 | - return (array) $items; |
|
| 1000 | - } |
|
| 1001 | - |
|
| 1002 | - /** |
|
| 1003 | - * Get an operator checker callback. |
|
| 1004 | - * |
|
| 1005 | - * @param string $key |
|
| 1006 | - * @param string|null $operator |
|
| 1007 | - * @param mixed $value |
|
| 1008 | - * @return \Closure |
|
| 1009 | - */ |
|
| 1010 | - protected function operatorForWhere($key, $operator = null, $value = null) |
|
| 1011 | - { |
|
| 1012 | - if (func_num_args() === 1) { |
|
| 1013 | - $value = true; |
|
| 1014 | - |
|
| 1015 | - $operator = '='; |
|
| 1016 | - } |
|
| 1017 | - |
|
| 1018 | - if (func_num_args() === 2) { |
|
| 1019 | - $value = $operator; |
|
| 1020 | - |
|
| 1021 | - $operator = '='; |
|
| 1022 | - } |
|
| 1023 | - |
|
| 1024 | - return function ($item) use ($key, $operator, $value) { |
|
| 1025 | - $retrieved = data_get($item, $key); |
|
| 1026 | - |
|
| 1027 | - $strings = array_filter([$retrieved, $value], function ($value) { |
|
| 1028 | - return is_string($value) || (is_object($value) && method_exists($value, '__toString')); |
|
| 1029 | - }); |
|
| 1030 | - |
|
| 1031 | - if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) { |
|
| 1032 | - return in_array($operator, ['!=', '<>', '!==']); |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - switch ($operator) { |
|
| 1036 | - default: |
|
| 1037 | - case '=': |
|
| 1038 | - case '==': return $retrieved == $value; |
|
| 1039 | - case '!=': |
|
| 1040 | - case '<>': return $retrieved != $value; |
|
| 1041 | - case '<': return $retrieved < $value; |
|
| 1042 | - case '>': return $retrieved > $value; |
|
| 1043 | - case '<=': return $retrieved <= $value; |
|
| 1044 | - case '>=': return $retrieved >= $value; |
|
| 1045 | - case '===': return $retrieved === $value; |
|
| 1046 | - case '!==': return $retrieved !== $value; |
|
| 1047 | - } |
|
| 1048 | - }; |
|
| 1049 | - } |
|
| 1050 | - |
|
| 1051 | - /** |
|
| 1052 | - * Determine if the given value is callable, but not a string. |
|
| 1053 | - * |
|
| 1054 | - * @param mixed $value |
|
| 1055 | - * @return bool |
|
| 1056 | - */ |
|
| 1057 | - protected function useAsCallable($value) |
|
| 1058 | - { |
|
| 1059 | - return ! is_string($value) && is_callable($value); |
|
| 1060 | - } |
|
| 1061 | - |
|
| 1062 | - /** |
|
| 1063 | - * Get a value retrieving callback. |
|
| 1064 | - * |
|
| 1065 | - * @param callable|string|null $value |
|
| 1066 | - * @return callable |
|
| 1067 | - */ |
|
| 1068 | - protected function valueRetriever($value) |
|
| 1069 | - { |
|
| 1070 | - if ($this->useAsCallable($value)) { |
|
| 1071 | - return $value; |
|
| 1072 | - } |
|
| 1073 | - |
|
| 1074 | - return function ($item) use ($value) { |
|
| 1075 | - return data_get($item, $value); |
|
| 1076 | - }; |
|
| 1077 | - } |
|
| 1078 | - |
|
| 1079 | - /** |
|
| 1080 | - * Make a function to check an item's equality. |
|
| 1081 | - * |
|
| 1082 | - * @param mixed $value |
|
| 1083 | - * @return \Closure |
|
| 1084 | - */ |
|
| 1085 | - protected function equality($value) |
|
| 1086 | - { |
|
| 1087 | - return function ($item) use ($value) { |
|
| 1088 | - return $item === $value; |
|
| 1089 | - }; |
|
| 1090 | - } |
|
| 1091 | - |
|
| 1092 | - /** |
|
| 1093 | - * Make a function using another function, by negating its result. |
|
| 1094 | - * |
|
| 1095 | - * @param \Closure $callback |
|
| 1096 | - * @return \Closure |
|
| 1097 | - */ |
|
| 1098 | - protected function negate(Closure $callback) |
|
| 1099 | - { |
|
| 1100 | - return function (...$params) use ($callback) { |
|
| 1101 | - return ! $callback(...$params); |
|
| 1102 | - }; |
|
| 1103 | - } |
|
| 1104 | - |
|
| 1105 | - /** |
|
| 1106 | - * Make a function that returns what's passed to it. |
|
| 1107 | - * |
|
| 1108 | - * @return \Closure |
|
| 1109 | - */ |
|
| 1110 | - protected function identity() |
|
| 1111 | - { |
|
| 1112 | - return function ($value) { |
|
| 1113 | - return $value; |
|
| 1114 | - }; |
|
| 1115 | - } |
|
| 51 | + /** |
|
| 52 | + * Indicates that the object's string representation should be escaped when __toString is invoked. |
|
| 53 | + * |
|
| 54 | + * @var bool |
|
| 55 | + */ |
|
| 56 | + protected $escapeWhenCastingToString = false; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * The methods that can be proxied. |
|
| 60 | + * |
|
| 61 | + * @var string[] |
|
| 62 | + */ |
|
| 63 | + protected static $proxies = [ |
|
| 64 | + 'average', |
|
| 65 | + 'avg', |
|
| 66 | + 'contains', |
|
| 67 | + 'doesntContain', |
|
| 68 | + 'each', |
|
| 69 | + 'every', |
|
| 70 | + 'filter', |
|
| 71 | + 'first', |
|
| 72 | + 'flatMap', |
|
| 73 | + 'groupBy', |
|
| 74 | + 'keyBy', |
|
| 75 | + 'map', |
|
| 76 | + 'max', |
|
| 77 | + 'min', |
|
| 78 | + 'partition', |
|
| 79 | + 'reject', |
|
| 80 | + 'skipUntil', |
|
| 81 | + 'skipWhile', |
|
| 82 | + 'some', |
|
| 83 | + 'sortBy', |
|
| 84 | + 'sortByDesc', |
|
| 85 | + 'sum', |
|
| 86 | + 'takeUntil', |
|
| 87 | + 'takeWhile', |
|
| 88 | + 'unique', |
|
| 89 | + 'until', |
|
| 90 | + ]; |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Create a new collection instance if the value isn't one already. |
|
| 94 | + * |
|
| 95 | + * @param mixed $items |
|
| 96 | + * @return static |
|
| 97 | + */ |
|
| 98 | + public static function make($items = []) |
|
| 99 | + { |
|
| 100 | + return new static($items); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Wrap the given value in a collection if applicable. |
|
| 105 | + * |
|
| 106 | + * @param mixed $value |
|
| 107 | + * @return static |
|
| 108 | + */ |
|
| 109 | + public static function wrap($value) |
|
| 110 | + { |
|
| 111 | + return $value instanceof Enumerable |
|
| 112 | + ? new static($value) |
|
| 113 | + : new static(Arr::wrap($value)); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + /** |
|
| 117 | + * Get the underlying items from the given collection if applicable. |
|
| 118 | + * |
|
| 119 | + * @param array|static $value |
|
| 120 | + * @return array |
|
| 121 | + */ |
|
| 122 | + public static function unwrap($value) |
|
| 123 | + { |
|
| 124 | + return $value instanceof Enumerable ? $value->all() : $value; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Create a new instance with no items. |
|
| 129 | + * |
|
| 130 | + * @return static |
|
| 131 | + */ |
|
| 132 | + public static function empty() |
|
| 133 | + { |
|
| 134 | + return new static([]); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * Create a new collection by invoking the callback a given amount of times. |
|
| 139 | + * |
|
| 140 | + * @param int $number |
|
| 141 | + * @param callable|null $callback |
|
| 142 | + * @return static |
|
| 143 | + */ |
|
| 144 | + public static function times($number, callable $callback = null) |
|
| 145 | + { |
|
| 146 | + if ($number < 1) { |
|
| 147 | + return new static; |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return static::range(1, $number) |
|
| 151 | + ->when($callback) |
|
| 152 | + ->map($callback); |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Alias for the "avg" method. |
|
| 157 | + * |
|
| 158 | + * @param callable|string|null $callback |
|
| 159 | + * @return mixed |
|
| 160 | + */ |
|
| 161 | + public function average($callback = null) |
|
| 162 | + { |
|
| 163 | + return $this->avg($callback); |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Alias for the "contains" method. |
|
| 168 | + * |
|
| 169 | + * @param mixed $key |
|
| 170 | + * @param mixed $operator |
|
| 171 | + * @param mixed $value |
|
| 172 | + * @return bool |
|
| 173 | + */ |
|
| 174 | + public function some($key, $operator = null, $value = null) |
|
| 175 | + { |
|
| 176 | + return $this->contains(...func_get_args()); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Determine if an item exists, using strict comparison. |
|
| 181 | + * |
|
| 182 | + * @param mixed $key |
|
| 183 | + * @param mixed $value |
|
| 184 | + * @return bool |
|
| 185 | + */ |
|
| 186 | + public function containsStrict($key, $value = null) |
|
| 187 | + { |
|
| 188 | + if (func_num_args() === 2) { |
|
| 189 | + return $this->contains(function ($item) use ($key, $value) { |
|
| 190 | + return data_get($item, $key) === $value; |
|
| 191 | + }); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + if ($this->useAsCallable($key)) { |
|
| 195 | + return ! is_null($this->first($key)); |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + foreach ($this as $item) { |
|
| 199 | + if ($item === $key) { |
|
| 200 | + return true; |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + return false; |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + /** |
|
| 208 | + * Dump the items and end the script. |
|
| 209 | + * |
|
| 210 | + * @param mixed ...$args |
|
| 211 | + * @return void |
|
| 212 | + */ |
|
| 213 | + public function dd(...$args) |
|
| 214 | + { |
|
| 215 | + $this->dump(...$args); |
|
| 216 | + |
|
| 217 | + exit(1); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + /** |
|
| 221 | + * Dump the items. |
|
| 222 | + * |
|
| 223 | + * @return $this |
|
| 224 | + */ |
|
| 225 | + public function dump() |
|
| 226 | + { |
|
| 227 | + (new Collection(func_get_args())) |
|
| 228 | + ->push($this->all()) |
|
| 229 | + ->each(function ($item) { |
|
| 230 | + VarDumper::dump($item); |
|
| 231 | + }); |
|
| 232 | + |
|
| 233 | + return $this; |
|
| 234 | + } |
|
| 235 | + |
|
| 236 | + /** |
|
| 237 | + * Execute a callback over each item. |
|
| 238 | + * |
|
| 239 | + * @param callable $callback |
|
| 240 | + * @return $this |
|
| 241 | + */ |
|
| 242 | + public function each(callable $callback) |
|
| 243 | + { |
|
| 244 | + foreach ($this as $key => $item) { |
|
| 245 | + if ($callback($item, $key) === false) { |
|
| 246 | + break; |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + |
|
| 250 | + return $this; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * Execute a callback over each nested chunk of items. |
|
| 255 | + * |
|
| 256 | + * @param callable $callback |
|
| 257 | + * @return static |
|
| 258 | + */ |
|
| 259 | + public function eachSpread(callable $callback) |
|
| 260 | + { |
|
| 261 | + return $this->each(function ($chunk, $key) use ($callback) { |
|
| 262 | + $chunk[] = $key; |
|
| 263 | + |
|
| 264 | + return $callback(...$chunk); |
|
| 265 | + }); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Determine if all items pass the given truth test. |
|
| 270 | + * |
|
| 271 | + * @param string|callable $key |
|
| 272 | + * @param mixed $operator |
|
| 273 | + * @param mixed $value |
|
| 274 | + * @return bool |
|
| 275 | + */ |
|
| 276 | + public function every($key, $operator = null, $value = null) |
|
| 277 | + { |
|
| 278 | + if (func_num_args() === 1) { |
|
| 279 | + $callback = $this->valueRetriever($key); |
|
| 280 | + |
|
| 281 | + foreach ($this as $k => $v) { |
|
| 282 | + if (! $callback($v, $k)) { |
|
| 283 | + return false; |
|
| 284 | + } |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + return true; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + return $this->every($this->operatorForWhere(...func_get_args())); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Get the first item by the given key value pair. |
|
| 295 | + * |
|
| 296 | + * @param string $key |
|
| 297 | + * @param mixed $operator |
|
| 298 | + * @param mixed $value |
|
| 299 | + * @return mixed |
|
| 300 | + */ |
|
| 301 | + public function firstWhere($key, $operator = null, $value = null) |
|
| 302 | + { |
|
| 303 | + return $this->first($this->operatorForWhere(...func_get_args())); |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Determine if the collection is not empty. |
|
| 308 | + * |
|
| 309 | + * @return bool |
|
| 310 | + */ |
|
| 311 | + public function isNotEmpty() |
|
| 312 | + { |
|
| 313 | + return ! $this->isEmpty(); |
|
| 314 | + } |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * Run a map over each nested chunk of items. |
|
| 318 | + * |
|
| 319 | + * @param callable $callback |
|
| 320 | + * @return static |
|
| 321 | + */ |
|
| 322 | + public function mapSpread(callable $callback) |
|
| 323 | + { |
|
| 324 | + return $this->map(function ($chunk, $key) use ($callback) { |
|
| 325 | + $chunk[] = $key; |
|
| 326 | + |
|
| 327 | + return $callback(...$chunk); |
|
| 328 | + }); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + /** |
|
| 332 | + * Run a grouping map over the items. |
|
| 333 | + * |
|
| 334 | + * The callback should return an associative array with a single key/value pair. |
|
| 335 | + * |
|
| 336 | + * @param callable $callback |
|
| 337 | + * @return static |
|
| 338 | + */ |
|
| 339 | + public function mapToGroups(callable $callback) |
|
| 340 | + { |
|
| 341 | + $groups = $this->mapToDictionary($callback); |
|
| 342 | + |
|
| 343 | + return $groups->map([$this, 'make']); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Map a collection and flatten the result by a single level. |
|
| 348 | + * |
|
| 349 | + * @param callable $callback |
|
| 350 | + * @return static |
|
| 351 | + */ |
|
| 352 | + public function flatMap(callable $callback) |
|
| 353 | + { |
|
| 354 | + return $this->map($callback)->collapse(); |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * Map the values into a new class. |
|
| 359 | + * |
|
| 360 | + * @param string $class |
|
| 361 | + * @return static |
|
| 362 | + */ |
|
| 363 | + public function mapInto($class) |
|
| 364 | + { |
|
| 365 | + return $this->map(function ($value, $key) use ($class) { |
|
| 366 | + return new $class($value, $key); |
|
| 367 | + }); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + /** |
|
| 371 | + * Get the min value of a given key. |
|
| 372 | + * |
|
| 373 | + * @param callable|string|null $callback |
|
| 374 | + * @return mixed |
|
| 375 | + */ |
|
| 376 | + public function min($callback = null) |
|
| 377 | + { |
|
| 378 | + $callback = $this->valueRetriever($callback); |
|
| 379 | + |
|
| 380 | + return $this->map(function ($value) use ($callback) { |
|
| 381 | + return $callback($value); |
|
| 382 | + })->filter(function ($value) { |
|
| 383 | + return ! is_null($value); |
|
| 384 | + })->reduce(function ($result, $value) { |
|
| 385 | + return is_null($result) || $value < $result ? $value : $result; |
|
| 386 | + }); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + /** |
|
| 390 | + * Get the max value of a given key. |
|
| 391 | + * |
|
| 392 | + * @param callable|string|null $callback |
|
| 393 | + * @return mixed |
|
| 394 | + */ |
|
| 395 | + public function max($callback = null) |
|
| 396 | + { |
|
| 397 | + $callback = $this->valueRetriever($callback); |
|
| 398 | + |
|
| 399 | + return $this->filter(function ($value) { |
|
| 400 | + return ! is_null($value); |
|
| 401 | + })->reduce(function ($result, $item) use ($callback) { |
|
| 402 | + $value = $callback($item); |
|
| 403 | + |
|
| 404 | + return is_null($result) || $value > $result ? $value : $result; |
|
| 405 | + }); |
|
| 406 | + } |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * "Paginate" the collection by slicing it into a smaller collection. |
|
| 410 | + * |
|
| 411 | + * @param int $page |
|
| 412 | + * @param int $perPage |
|
| 413 | + * @return static |
|
| 414 | + */ |
|
| 415 | + public function forPage($page, $perPage) |
|
| 416 | + { |
|
| 417 | + $offset = max(0, ($page - 1) * $perPage); |
|
| 418 | + |
|
| 419 | + return $this->slice($offset, $perPage); |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + /** |
|
| 423 | + * Partition the collection into two arrays using the given callback or key. |
|
| 424 | + * |
|
| 425 | + * @param callable|string $key |
|
| 426 | + * @param mixed $operator |
|
| 427 | + * @param mixed $value |
|
| 428 | + * @return static |
|
| 429 | + */ |
|
| 430 | + public function partition($key, $operator = null, $value = null) |
|
| 431 | + { |
|
| 432 | + $passed = []; |
|
| 433 | + $failed = []; |
|
| 434 | + |
|
| 435 | + $callback = func_num_args() === 1 |
|
| 436 | + ? $this->valueRetriever($key) |
|
| 437 | + : $this->operatorForWhere(...func_get_args()); |
|
| 438 | + |
|
| 439 | + foreach ($this as $key => $item) { |
|
| 440 | + if ($callback($item, $key)) { |
|
| 441 | + $passed[$key] = $item; |
|
| 442 | + } else { |
|
| 443 | + $failed[$key] = $item; |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + return new static([new static($passed), new static($failed)]); |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * Get the sum of the given values. |
|
| 452 | + * |
|
| 453 | + * @param callable|string|null $callback |
|
| 454 | + * @return mixed |
|
| 455 | + */ |
|
| 456 | + public function sum($callback = null) |
|
| 457 | + { |
|
| 458 | + $callback = is_null($callback) |
|
| 459 | + ? $this->identity() |
|
| 460 | + : $this->valueRetriever($callback); |
|
| 461 | + |
|
| 462 | + return $this->reduce(function ($result, $item) use ($callback) { |
|
| 463 | + return $result + $callback($item); |
|
| 464 | + }, 0); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + /** |
|
| 468 | + * Apply the callback if the value is truthy. |
|
| 469 | + * |
|
| 470 | + * @param bool|mixed $value |
|
| 471 | + * @param callable|null $callback |
|
| 472 | + * @param callable|null $default |
|
| 473 | + * @return static|mixed |
|
| 474 | + */ |
|
| 475 | + public function when($value, callable $callback = null, callable $default = null) |
|
| 476 | + { |
|
| 477 | + if (! $callback) { |
|
| 478 | + return new HigherOrderWhenProxy($this, $value); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + if ($value) { |
|
| 482 | + return $callback($this, $value); |
|
| 483 | + } elseif ($default) { |
|
| 484 | + return $default($this, $value); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + return $this; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + /** |
|
| 491 | + * Apply the callback if the collection is empty. |
|
| 492 | + * |
|
| 493 | + * @param callable $callback |
|
| 494 | + * @param callable|null $default |
|
| 495 | + * @return static|mixed |
|
| 496 | + */ |
|
| 497 | + public function whenEmpty(callable $callback, callable $default = null) |
|
| 498 | + { |
|
| 499 | + return $this->when($this->isEmpty(), $callback, $default); |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * Apply the callback if the collection is not empty. |
|
| 504 | + * |
|
| 505 | + * @param callable $callback |
|
| 506 | + * @param callable|null $default |
|
| 507 | + * @return static|mixed |
|
| 508 | + */ |
|
| 509 | + public function whenNotEmpty(callable $callback, callable $default = null) |
|
| 510 | + { |
|
| 511 | + return $this->when($this->isNotEmpty(), $callback, $default); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + /** |
|
| 515 | + * Apply the callback if the value is falsy. |
|
| 516 | + * |
|
| 517 | + * @param bool $value |
|
| 518 | + * @param callable $callback |
|
| 519 | + * @param callable|null $default |
|
| 520 | + * @return static|mixed |
|
| 521 | + */ |
|
| 522 | + public function unless($value, callable $callback, callable $default = null) |
|
| 523 | + { |
|
| 524 | + return $this->when(! $value, $callback, $default); |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + /** |
|
| 528 | + * Apply the callback unless the collection is empty. |
|
| 529 | + * |
|
| 530 | + * @param callable $callback |
|
| 531 | + * @param callable|null $default |
|
| 532 | + * @return static|mixed |
|
| 533 | + */ |
|
| 534 | + public function unlessEmpty(callable $callback, callable $default = null) |
|
| 535 | + { |
|
| 536 | + return $this->whenNotEmpty($callback, $default); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * Apply the callback unless the collection is not empty. |
|
| 541 | + * |
|
| 542 | + * @param callable $callback |
|
| 543 | + * @param callable|null $default |
|
| 544 | + * @return static|mixed |
|
| 545 | + */ |
|
| 546 | + public function unlessNotEmpty(callable $callback, callable $default = null) |
|
| 547 | + { |
|
| 548 | + return $this->whenEmpty($callback, $default); |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Filter items by the given key value pair. |
|
| 553 | + * |
|
| 554 | + * @param string $key |
|
| 555 | + * @param mixed $operator |
|
| 556 | + * @param mixed $value |
|
| 557 | + * @return static |
|
| 558 | + */ |
|
| 559 | + public function where($key, $operator = null, $value = null) |
|
| 560 | + { |
|
| 561 | + return $this->filter($this->operatorForWhere(...func_get_args())); |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * Filter items where the value for the given key is null. |
|
| 566 | + * |
|
| 567 | + * @param string|null $key |
|
| 568 | + * @return static |
|
| 569 | + */ |
|
| 570 | + public function whereNull($key = null) |
|
| 571 | + { |
|
| 572 | + return $this->whereStrict($key, null); |
|
| 573 | + } |
|
| 574 | + |
|
| 575 | + /** |
|
| 576 | + * Filter items where the value for the given key is not null. |
|
| 577 | + * |
|
| 578 | + * @param string|null $key |
|
| 579 | + * @return static |
|
| 580 | + */ |
|
| 581 | + public function whereNotNull($key = null) |
|
| 582 | + { |
|
| 583 | + return $this->where($key, '!==', null); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * Filter items by the given key value pair using strict comparison. |
|
| 588 | + * |
|
| 589 | + * @param string $key |
|
| 590 | + * @param mixed $value |
|
| 591 | + * @return static |
|
| 592 | + */ |
|
| 593 | + public function whereStrict($key, $value) |
|
| 594 | + { |
|
| 595 | + return $this->where($key, '===', $value); |
|
| 596 | + } |
|
| 597 | + |
|
| 598 | + /** |
|
| 599 | + * Filter items by the given key value pair. |
|
| 600 | + * |
|
| 601 | + * @param string $key |
|
| 602 | + * @param mixed $values |
|
| 603 | + * @param bool $strict |
|
| 604 | + * @return static |
|
| 605 | + */ |
|
| 606 | + public function whereIn($key, $values, $strict = false) |
|
| 607 | + { |
|
| 608 | + $values = $this->getArrayableItems($values); |
|
| 609 | + |
|
| 610 | + return $this->filter(function ($item) use ($key, $values, $strict) { |
|
| 611 | + return in_array(data_get($item, $key), $values, $strict); |
|
| 612 | + }); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + /** |
|
| 616 | + * Filter items by the given key value pair using strict comparison. |
|
| 617 | + * |
|
| 618 | + * @param string $key |
|
| 619 | + * @param mixed $values |
|
| 620 | + * @return static |
|
| 621 | + */ |
|
| 622 | + public function whereInStrict($key, $values) |
|
| 623 | + { |
|
| 624 | + return $this->whereIn($key, $values, true); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + /** |
|
| 628 | + * Filter items such that the value of the given key is between the given values. |
|
| 629 | + * |
|
| 630 | + * @param string $key |
|
| 631 | + * @param array $values |
|
| 632 | + * @return static |
|
| 633 | + */ |
|
| 634 | + public function whereBetween($key, $values) |
|
| 635 | + { |
|
| 636 | + return $this->where($key, '>=', reset($values))->where($key, '<=', end($values)); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Filter items such that the value of the given key is not between the given values. |
|
| 641 | + * |
|
| 642 | + * @param string $key |
|
| 643 | + * @param array $values |
|
| 644 | + * @return static |
|
| 645 | + */ |
|
| 646 | + public function whereNotBetween($key, $values) |
|
| 647 | + { |
|
| 648 | + return $this->filter(function ($item) use ($key, $values) { |
|
| 649 | + return data_get($item, $key) < reset($values) || data_get($item, $key) > end($values); |
|
| 650 | + }); |
|
| 651 | + } |
|
| 652 | + |
|
| 653 | + /** |
|
| 654 | + * Filter items by the given key value pair. |
|
| 655 | + * |
|
| 656 | + * @param string $key |
|
| 657 | + * @param mixed $values |
|
| 658 | + * @param bool $strict |
|
| 659 | + * @return static |
|
| 660 | + */ |
|
| 661 | + public function whereNotIn($key, $values, $strict = false) |
|
| 662 | + { |
|
| 663 | + $values = $this->getArrayableItems($values); |
|
| 664 | + |
|
| 665 | + return $this->reject(function ($item) use ($key, $values, $strict) { |
|
| 666 | + return in_array(data_get($item, $key), $values, $strict); |
|
| 667 | + }); |
|
| 668 | + } |
|
| 669 | + |
|
| 670 | + /** |
|
| 671 | + * Filter items by the given key value pair using strict comparison. |
|
| 672 | + * |
|
| 673 | + * @param string $key |
|
| 674 | + * @param mixed $values |
|
| 675 | + * @return static |
|
| 676 | + */ |
|
| 677 | + public function whereNotInStrict($key, $values) |
|
| 678 | + { |
|
| 679 | + return $this->whereNotIn($key, $values, true); |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + /** |
|
| 683 | + * Filter the items, removing any items that don't match the given type(s). |
|
| 684 | + * |
|
| 685 | + * @param string|string[] $type |
|
| 686 | + * @return static |
|
| 687 | + */ |
|
| 688 | + public function whereInstanceOf($type) |
|
| 689 | + { |
|
| 690 | + return $this->filter(function ($value) use ($type) { |
|
| 691 | + if (is_array($type)) { |
|
| 692 | + foreach ($type as $classType) { |
|
| 693 | + if ($value instanceof $classType) { |
|
| 694 | + return true; |
|
| 695 | + } |
|
| 696 | + } |
|
| 697 | + |
|
| 698 | + return false; |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + return $value instanceof $type; |
|
| 702 | + }); |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * Pass the collection to the given callback and return the result. |
|
| 707 | + * |
|
| 708 | + * @param callable $callback |
|
| 709 | + * @return mixed |
|
| 710 | + */ |
|
| 711 | + public function pipe(callable $callback) |
|
| 712 | + { |
|
| 713 | + return $callback($this); |
|
| 714 | + } |
|
| 715 | + |
|
| 716 | + /** |
|
| 717 | + * Pass the collection into a new class. |
|
| 718 | + * |
|
| 719 | + * @param string $class |
|
| 720 | + * @return mixed |
|
| 721 | + */ |
|
| 722 | + public function pipeInto($class) |
|
| 723 | + { |
|
| 724 | + return new $class($this); |
|
| 725 | + } |
|
| 726 | + |
|
| 727 | + /** |
|
| 728 | + * Pass the collection through a series of callable pipes and return the result. |
|
| 729 | + * |
|
| 730 | + * @param array<callable> $pipes |
|
| 731 | + * @return mixed |
|
| 732 | + */ |
|
| 733 | + public function pipeThrough($pipes) |
|
| 734 | + { |
|
| 735 | + return static::make($pipes)->reduce( |
|
| 736 | + function ($carry, $pipe) { |
|
| 737 | + return $pipe($carry); |
|
| 738 | + }, |
|
| 739 | + $this |
|
| 740 | + ); |
|
| 741 | + } |
|
| 742 | + |
|
| 743 | + /** |
|
| 744 | + * Pass the collection to the given callback and then return it. |
|
| 745 | + * |
|
| 746 | + * @param callable $callback |
|
| 747 | + * @return $this |
|
| 748 | + */ |
|
| 749 | + public function tap(callable $callback) |
|
| 750 | + { |
|
| 751 | + $callback(clone $this); |
|
| 752 | + |
|
| 753 | + return $this; |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + /** |
|
| 757 | + * Reduce the collection to a single value. |
|
| 758 | + * |
|
| 759 | + * @param callable $callback |
|
| 760 | + * @param mixed $initial |
|
| 761 | + * @return mixed |
|
| 762 | + */ |
|
| 763 | + public function reduce(callable $callback, $initial = null) |
|
| 764 | + { |
|
| 765 | + $result = $initial; |
|
| 766 | + |
|
| 767 | + foreach ($this as $key => $value) { |
|
| 768 | + $result = $callback($result, $value, $key); |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + return $result; |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + /** |
|
| 775 | + * Reduce the collection to multiple aggregate values. |
|
| 776 | + * |
|
| 777 | + * @param callable $callback |
|
| 778 | + * @param mixed ...$initial |
|
| 779 | + * @return array |
|
| 780 | + * |
|
| 781 | + * @deprecated Use "reduceSpread" instead |
|
| 782 | + * |
|
| 783 | + * @throws \UnexpectedValueException |
|
| 784 | + */ |
|
| 785 | + public function reduceMany(callable $callback, ...$initial) |
|
| 786 | + { |
|
| 787 | + return $this->reduceSpread($callback, ...$initial); |
|
| 788 | + } |
|
| 789 | + |
|
| 790 | + /** |
|
| 791 | + * Reduce the collection to multiple aggregate values. |
|
| 792 | + * |
|
| 793 | + * @param callable $callback |
|
| 794 | + * @param mixed ...$initial |
|
| 795 | + * @return array |
|
| 796 | + * |
|
| 797 | + * @throws \UnexpectedValueException |
|
| 798 | + */ |
|
| 799 | + public function reduceSpread(callable $callback, ...$initial) |
|
| 800 | + { |
|
| 801 | + $result = $initial; |
|
| 802 | + |
|
| 803 | + foreach ($this as $key => $value) { |
|
| 804 | + $result = call_user_func_array($callback, array_merge($result, [$value, $key])); |
|
| 805 | + |
|
| 806 | + if (! is_array($result)) { |
|
| 807 | + throw new UnexpectedValueException(sprintf( |
|
| 808 | + "%s::reduceMany expects reducer to return an array, but got a '%s' instead.", |
|
| 809 | + class_basename(static::class), gettype($result) |
|
| 810 | + )); |
|
| 811 | + } |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + return $result; |
|
| 815 | + } |
|
| 816 | + |
|
| 817 | + /** |
|
| 818 | + * Reduce an associative collection to a single value. |
|
| 819 | + * |
|
| 820 | + * @param callable $callback |
|
| 821 | + * @param mixed $initial |
|
| 822 | + * @return mixed |
|
| 823 | + */ |
|
| 824 | + public function reduceWithKeys(callable $callback, $initial = null) |
|
| 825 | + { |
|
| 826 | + return $this->reduce($callback, $initial); |
|
| 827 | + } |
|
| 828 | + |
|
| 829 | + /** |
|
| 830 | + * Create a collection of all elements that do not pass a given truth test. |
|
| 831 | + * |
|
| 832 | + * @param callable|mixed $callback |
|
| 833 | + * @return static |
|
| 834 | + */ |
|
| 835 | + public function reject($callback = true) |
|
| 836 | + { |
|
| 837 | + $useAsCallable = $this->useAsCallable($callback); |
|
| 838 | + |
|
| 839 | + return $this->filter(function ($value, $key) use ($callback, $useAsCallable) { |
|
| 840 | + return $useAsCallable |
|
| 841 | + ? ! $callback($value, $key) |
|
| 842 | + : $value != $callback; |
|
| 843 | + }); |
|
| 844 | + } |
|
| 845 | + |
|
| 846 | + /** |
|
| 847 | + * Return only unique items from the collection array using strict comparison. |
|
| 848 | + * |
|
| 849 | + * @param string|callable|null $key |
|
| 850 | + * @return static |
|
| 851 | + */ |
|
| 852 | + public function uniqueStrict($key = null) |
|
| 853 | + { |
|
| 854 | + return $this->unique($key, true); |
|
| 855 | + } |
|
| 856 | + |
|
| 857 | + /** |
|
| 858 | + * Collect the values into a collection. |
|
| 859 | + * |
|
| 860 | + * @return \Illuminate\Support\Collection |
|
| 861 | + */ |
|
| 862 | + public function collect() |
|
| 863 | + { |
|
| 864 | + return new Collection($this->all()); |
|
| 865 | + } |
|
| 866 | + |
|
| 867 | + /** |
|
| 868 | + * Get the collection of items as a plain array. |
|
| 869 | + * |
|
| 870 | + * @return array |
|
| 871 | + */ |
|
| 872 | + public function toArray() |
|
| 873 | + { |
|
| 874 | + return $this->map(function ($value) { |
|
| 875 | + return $value instanceof Arrayable ? $value->toArray() : $value; |
|
| 876 | + })->all(); |
|
| 877 | + } |
|
| 878 | + |
|
| 879 | + /** |
|
| 880 | + * Convert the object into something JSON serializable. |
|
| 881 | + * |
|
| 882 | + * @return array |
|
| 883 | + */ |
|
| 884 | + #[\ReturnTypeWillChange] |
|
| 885 | + public function jsonSerialize() |
|
| 886 | + { |
|
| 887 | + return array_map(function ($value) { |
|
| 888 | + if ($value instanceof JsonSerializable) { |
|
| 889 | + return $value->jsonSerialize(); |
|
| 890 | + } elseif ($value instanceof Jsonable) { |
|
| 891 | + return json_decode($value->toJson(), true); |
|
| 892 | + } elseif ($value instanceof Arrayable) { |
|
| 893 | + return $value->toArray(); |
|
| 894 | + } |
|
| 895 | + |
|
| 896 | + return $value; |
|
| 897 | + }, $this->all()); |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + /** |
|
| 901 | + * Get the collection of items as JSON. |
|
| 902 | + * |
|
| 903 | + * @param int $options |
|
| 904 | + * @return string |
|
| 905 | + */ |
|
| 906 | + public function toJson($options = 0) |
|
| 907 | + { |
|
| 908 | + return json_encode($this->jsonSerialize(), $options); |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + /** |
|
| 912 | + * Get a CachingIterator instance. |
|
| 913 | + * |
|
| 914 | + * @param int $flags |
|
| 915 | + * @return \CachingIterator |
|
| 916 | + */ |
|
| 917 | + public function getCachingIterator($flags = CachingIterator::CALL_TOSTRING) |
|
| 918 | + { |
|
| 919 | + return new CachingIterator($this->getIterator(), $flags); |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + /** |
|
| 923 | + * Convert the collection to its string representation. |
|
| 924 | + * |
|
| 925 | + * @return string |
|
| 926 | + */ |
|
| 927 | + public function __toString() |
|
| 928 | + { |
|
| 929 | + return $this->escapeWhenCastingToString |
|
| 930 | + ? e($this->toJson()) |
|
| 931 | + : $this->toJson(); |
|
| 932 | + } |
|
| 933 | + |
|
| 934 | + /** |
|
| 935 | + * Indicate that the model's string representation should be escaped when __toString is invoked. |
|
| 936 | + * |
|
| 937 | + * @param bool $escape |
|
| 938 | + * @return $this |
|
| 939 | + */ |
|
| 940 | + public function escapeWhenCastingToString($escape = true) |
|
| 941 | + { |
|
| 942 | + $this->escapeWhenCastingToString = $escape; |
|
| 943 | + |
|
| 944 | + return $this; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + /** |
|
| 948 | + * Add a method to the list of proxied methods. |
|
| 949 | + * |
|
| 950 | + * @param string $method |
|
| 951 | + * @return void |
|
| 952 | + */ |
|
| 953 | + public static function proxy($method) |
|
| 954 | + { |
|
| 955 | + static::$proxies[] = $method; |
|
| 956 | + } |
|
| 957 | + |
|
| 958 | + /** |
|
| 959 | + * Dynamically access collection proxies. |
|
| 960 | + * |
|
| 961 | + * @param string $key |
|
| 962 | + * @return mixed |
|
| 963 | + * |
|
| 964 | + * @throws \Exception |
|
| 965 | + */ |
|
| 966 | + public function __get($key) |
|
| 967 | + { |
|
| 968 | + if (! in_array($key, static::$proxies)) { |
|
| 969 | + throw new Exception("Property [{$key}] does not exist on this collection instance."); |
|
| 970 | + } |
|
| 971 | + |
|
| 972 | + return new HigherOrderCollectionProxy($this, $key); |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + /** |
|
| 976 | + * Results array of items from Collection or Arrayable. |
|
| 977 | + * |
|
| 978 | + * @param mixed $items |
|
| 979 | + * @return array |
|
| 980 | + */ |
|
| 981 | + protected function getArrayableItems($items) |
|
| 982 | + { |
|
| 983 | + if (is_array($items)) { |
|
| 984 | + return $items; |
|
| 985 | + } elseif ($items instanceof Enumerable) { |
|
| 986 | + return $items->all(); |
|
| 987 | + } elseif ($items instanceof Arrayable) { |
|
| 988 | + return $items->toArray(); |
|
| 989 | + } elseif ($items instanceof Jsonable) { |
|
| 990 | + return json_decode($items->toJson(), true); |
|
| 991 | + } elseif ($items instanceof JsonSerializable) { |
|
| 992 | + return (array) $items->jsonSerialize(); |
|
| 993 | + } elseif ($items instanceof Traversable) { |
|
| 994 | + return iterator_to_array($items); |
|
| 995 | + } elseif ($items instanceof UnitEnum) { |
|
| 996 | + return [$items]; |
|
| 997 | + } |
|
| 998 | + |
|
| 999 | + return (array) $items; |
|
| 1000 | + } |
|
| 1001 | + |
|
| 1002 | + /** |
|
| 1003 | + * Get an operator checker callback. |
|
| 1004 | + * |
|
| 1005 | + * @param string $key |
|
| 1006 | + * @param string|null $operator |
|
| 1007 | + * @param mixed $value |
|
| 1008 | + * @return \Closure |
|
| 1009 | + */ |
|
| 1010 | + protected function operatorForWhere($key, $operator = null, $value = null) |
|
| 1011 | + { |
|
| 1012 | + if (func_num_args() === 1) { |
|
| 1013 | + $value = true; |
|
| 1014 | + |
|
| 1015 | + $operator = '='; |
|
| 1016 | + } |
|
| 1017 | + |
|
| 1018 | + if (func_num_args() === 2) { |
|
| 1019 | + $value = $operator; |
|
| 1020 | + |
|
| 1021 | + $operator = '='; |
|
| 1022 | + } |
|
| 1023 | + |
|
| 1024 | + return function ($item) use ($key, $operator, $value) { |
|
| 1025 | + $retrieved = data_get($item, $key); |
|
| 1026 | + |
|
| 1027 | + $strings = array_filter([$retrieved, $value], function ($value) { |
|
| 1028 | + return is_string($value) || (is_object($value) && method_exists($value, '__toString')); |
|
| 1029 | + }); |
|
| 1030 | + |
|
| 1031 | + if (count($strings) < 2 && count(array_filter([$retrieved, $value], 'is_object')) == 1) { |
|
| 1032 | + return in_array($operator, ['!=', '<>', '!==']); |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + switch ($operator) { |
|
| 1036 | + default: |
|
| 1037 | + case '=': |
|
| 1038 | + case '==': return $retrieved == $value; |
|
| 1039 | + case '!=': |
|
| 1040 | + case '<>': return $retrieved != $value; |
|
| 1041 | + case '<': return $retrieved < $value; |
|
| 1042 | + case '>': return $retrieved > $value; |
|
| 1043 | + case '<=': return $retrieved <= $value; |
|
| 1044 | + case '>=': return $retrieved >= $value; |
|
| 1045 | + case '===': return $retrieved === $value; |
|
| 1046 | + case '!==': return $retrieved !== $value; |
|
| 1047 | + } |
|
| 1048 | + }; |
|
| 1049 | + } |
|
| 1050 | + |
|
| 1051 | + /** |
|
| 1052 | + * Determine if the given value is callable, but not a string. |
|
| 1053 | + * |
|
| 1054 | + * @param mixed $value |
|
| 1055 | + * @return bool |
|
| 1056 | + */ |
|
| 1057 | + protected function useAsCallable($value) |
|
| 1058 | + { |
|
| 1059 | + return ! is_string($value) && is_callable($value); |
|
| 1060 | + } |
|
| 1061 | + |
|
| 1062 | + /** |
|
| 1063 | + * Get a value retrieving callback. |
|
| 1064 | + * |
|
| 1065 | + * @param callable|string|null $value |
|
| 1066 | + * @return callable |
|
| 1067 | + */ |
|
| 1068 | + protected function valueRetriever($value) |
|
| 1069 | + { |
|
| 1070 | + if ($this->useAsCallable($value)) { |
|
| 1071 | + return $value; |
|
| 1072 | + } |
|
| 1073 | + |
|
| 1074 | + return function ($item) use ($value) { |
|
| 1075 | + return data_get($item, $value); |
|
| 1076 | + }; |
|
| 1077 | + } |
|
| 1078 | + |
|
| 1079 | + /** |
|
| 1080 | + * Make a function to check an item's equality. |
|
| 1081 | + * |
|
| 1082 | + * @param mixed $value |
|
| 1083 | + * @return \Closure |
|
| 1084 | + */ |
|
| 1085 | + protected function equality($value) |
|
| 1086 | + { |
|
| 1087 | + return function ($item) use ($value) { |
|
| 1088 | + return $item === $value; |
|
| 1089 | + }; |
|
| 1090 | + } |
|
| 1091 | + |
|
| 1092 | + /** |
|
| 1093 | + * Make a function using another function, by negating its result. |
|
| 1094 | + * |
|
| 1095 | + * @param \Closure $callback |
|
| 1096 | + * @return \Closure |
|
| 1097 | + */ |
|
| 1098 | + protected function negate(Closure $callback) |
|
| 1099 | + { |
|
| 1100 | + return function (...$params) use ($callback) { |
|
| 1101 | + return ! $callback(...$params); |
|
| 1102 | + }; |
|
| 1103 | + } |
|
| 1104 | + |
|
| 1105 | + /** |
|
| 1106 | + * Make a function that returns what's passed to it. |
|
| 1107 | + * |
|
| 1108 | + * @return \Closure |
|
| 1109 | + */ |
|
| 1110 | + protected function identity() |
|
| 1111 | + { |
|
| 1112 | + return function ($value) { |
|
| 1113 | + return $value; |
|
| 1114 | + }; |
|
| 1115 | + } |
|
| 1116 | 1116 | } |