Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Arrayy often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Arrayy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class Arrayy extends \ArrayObject implements \IteratorAggregate, \ArrayAccess, \Serializable, \JsonSerializable, \Countable |
||
| 24 | { |
||
| 25 | const ARRAYY_HELPER_TYPES_FOR_ALL_PROPERTIES = '!!!!Arrayy_Helper_Types_For_All_Properties!!!!'; |
||
| 26 | |||
| 27 | const ARRAYY_HELPER_WALK = '!!!!Arrayy_Helper_Walk!!!!'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | * |
||
| 32 | * @phpstan-var array<array-key|TKey,T> |
||
| 33 | */ |
||
| 34 | protected $array = []; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \Arrayy\ArrayyRewindableGenerator|null |
||
| 38 | * |
||
| 39 | * @phpstan-var \Arrayy\ArrayyRewindableGenerator<TKey,T>|null |
||
| 40 | */ |
||
| 41 | protected $generator; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var string |
||
| 45 | * |
||
| 46 | * @phpstan-var class-string<\Arrayy\ArrayyIterator> |
||
| 47 | */ |
||
| 48 | protected $iteratorClass = ArrayyIterator::class; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | protected $pathSeparator = '.'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var bool |
||
| 57 | */ |
||
| 58 | protected $checkPropertyTypes = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected $checkForMissingPropertiesInConstructor = false; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | protected $checkPropertiesMismatchInConstructor = false; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var bool |
||
| 72 | */ |
||
| 73 | protected $checkPropertiesMismatch = true; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var array<array-key,TypeCheckInterface>|TypeCheckArray<array-key,TypeCheckInterface> |
||
| 77 | */ |
||
| 78 | protected $properties = []; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Initializes |
||
| 82 | * |
||
| 83 | * @param mixed $data <p> |
||
| 84 | * Should be an array or a generator, otherwise it will try |
||
| 85 | * to convert it into an array. |
||
| 86 | * </p> |
||
| 87 | * @param string $iteratorClass optional <p> |
||
| 88 | * You can overwrite the ArrayyIterator, but mostly you don't |
||
| 89 | * need this option. |
||
| 90 | * </p> |
||
| 91 | * @param bool $checkPropertiesInConstructor optional <p> |
||
| 92 | * You need to extend the "Arrayy"-class and you need to set |
||
| 93 | * the $checkPropertiesMismatchInConstructor class property |
||
| 94 | * to |
||
| 95 | * true, otherwise this option didn't not work anyway. |
||
| 96 | * </p> |
||
| 97 | * |
||
| 98 | * @phpstan-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 99 | */ |
||
| 100 | 1215 | public function __construct( |
|
| 101 | $data = [], |
||
| 102 | string $iteratorClass = ArrayyIterator::class, |
||
| 103 | bool $checkPropertiesInConstructor = true |
||
| 104 | ) { |
||
| 105 | 1215 | $data = $this->fallbackForArray($data); |
|
| 106 | |||
| 107 | // used only for serialize + unserialize, all other methods are overwritten |
||
| 108 | /** |
||
| 109 | * @psalm-suppress InvalidArgument - why? |
||
| 110 | */ |
||
| 111 | 1213 | parent::__construct([], 0, $iteratorClass); |
|
| 112 | |||
| 113 | 1213 | $this->setInitialValuesAndProperties($data, $checkPropertiesInConstructor); |
|
| 114 | |||
| 115 | 1205 | $this->setIteratorClass($iteratorClass); |
|
| 116 | 1205 | } |
|
| 117 | |||
| 118 | /** |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | 53 | public function __clone() |
|
| 122 | { |
||
| 123 | 53 | if (!\is_array($this->properties)) { |
|
| 124 | 1 | $this->properties = clone $this->properties; |
|
|
|
|||
| 125 | } |
||
| 126 | |||
| 127 | 53 | if ($this->generator !== null) { |
|
| 128 | 1 | $this->generator = clone $this->generator; |
|
| 129 | } |
||
| 130 | 53 | } |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Call object as function. |
||
| 134 | * |
||
| 135 | * @param mixed $key |
||
| 136 | * |
||
| 137 | * @return mixed |
||
| 138 | * |
||
| 139 | * @phpstan-param TKey $key |
||
| 140 | * @phpstan-return false|T|array<TKey,T> |
||
| 141 | */ |
||
| 142 | 1 | public function __invoke($key = null) |
|
| 143 | { |
||
| 144 | 1 | if ($key !== null) { |
|
| 145 | 1 | $this->generatorToArray(); |
|
| 146 | |||
| 147 | 1 | return $this->array[$key] ?? false; |
|
| 148 | } |
||
| 149 | |||
| 150 | return $this->toArray(); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Whether or not an element exists by key. |
||
| 155 | * |
||
| 156 | * @param mixed $key |
||
| 157 | * |
||
| 158 | * @return bool |
||
| 159 | * <p>True is the key/index exists, otherwise false.</p> |
||
| 160 | * |
||
| 161 | * @phpstan-param TKey $key |
||
| 162 | */ |
||
| 163 | public function __isset($key): bool |
||
| 164 | { |
||
| 165 | return $this->offsetExists($key); |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Assigns a value to the specified element. |
||
| 170 | * |
||
| 171 | * @param mixed $key |
||
| 172 | * @param mixed $value |
||
| 173 | * |
||
| 174 | * @return void |
||
| 175 | * |
||
| 176 | * @phpstan-param TKey $key |
||
| 177 | * @phpstan-param T $value |
||
| 178 | */ |
||
| 179 | 3 | public function __set($key, $value) |
|
| 180 | { |
||
| 181 | 3 | $this->internalSet($key, $value); |
|
| 182 | 3 | } |
|
| 183 | |||
| 184 | /** |
||
| 185 | * magic to string |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 15 | public function __toString(): string |
|
| 190 | { |
||
| 191 | 15 | return $this->toString(); |
|
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Unset element by key. |
||
| 196 | * |
||
| 197 | * @param mixed $key |
||
| 198 | * |
||
| 199 | * @phpstan-param TKey $key |
||
| 200 | */ |
||
| 201 | public function __unset($key) |
||
| 202 | { |
||
| 203 | $this->internalRemove($key); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get a value by key. |
||
| 208 | * |
||
| 209 | * @param mixed $key |
||
| 210 | * |
||
| 211 | * @return mixed |
||
| 212 | * <p>Get a Value from the current array.</p> |
||
| 213 | * |
||
| 214 | * @phpstan-param TKey $key |
||
| 215 | * @phpstan-return null|self<array-key,T>|T |
||
| 216 | */ |
||
| 217 | 134 | public function &__get($key) |
|
| 218 | { |
||
| 219 | 134 | $return = $this->get($key, null, null, true); |
|
| 220 | |||
| 221 | 134 | if (\is_array($return) === true) { |
|
| 222 | $return = static::create( |
||
| 223 | [], |
||
| 224 | $this->iteratorClass, |
||
| 225 | false |
||
| 226 | )->createByReference($return); |
||
| 227 | } |
||
| 228 | |||
| 229 | 134 | return $return; |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Add new values (optional using dot-notation). |
||
| 234 | * |
||
| 235 | * @param mixed $value |
||
| 236 | * @param int|string|null $key |
||
| 237 | * |
||
| 238 | * @return static |
||
| 239 | * <p>(Immutable) Return this Arrayy object, with the appended values.</p> |
||
| 240 | * |
||
| 241 | * @phpstan-param T $value |
||
| 242 | * @phpstan-param TKey $key |
||
| 243 | * @phpstan-return static<TKey,T> |
||
| 244 | * |
||
| 245 | * @psalm-mutation-free |
||
| 246 | */ |
||
| 247 | 13 | public function add($value, $key = null) |
|
| 248 | { |
||
| 249 | 13 | if ($key !== null) { |
|
| 250 | 5 | $get = $this->get($key); |
|
| 251 | 5 | if ($get !== null) { |
|
| 252 | 1 | $value = \array_merge_recursive( |
|
| 253 | 1 | !$get instanceof self ? [$get] : $get->getArray(), |
|
| 254 | 1 | !\is_array($value) ? [$value] : $value |
|
| 255 | ); |
||
| 256 | } |
||
| 257 | |||
| 258 | 5 | $this->internalSet($key, $value); |
|
| 259 | |||
| 260 | 4 | return $this; |
|
| 261 | } |
||
| 262 | |||
| 263 | 8 | return $this->append($value); |
|
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Append a (key) + value to the current array. |
||
| 268 | * |
||
| 269 | * EXAMPLE: <code> |
||
| 270 | * a(['fòô' => 'bàř'])->append('foo'); // Arrayy['fòô' => 'bàř', 0 => 'foo'] |
||
| 271 | * </code> |
||
| 272 | * |
||
| 273 | * @param mixed $value |
||
| 274 | * @param mixed $key |
||
| 275 | * |
||
| 276 | * @return $this |
||
| 277 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 278 | * |
||
| 279 | * @phpstan-param T $value |
||
| 280 | * @phpstan-param TKey|null $key |
||
| 281 | * @phpstan-return static<TKey,T> |
||
| 282 | */ |
||
| 283 | 20 | public function append($value, $key = null): self |
|
| 284 | { |
||
| 285 | 20 | $this->generatorToArray(); |
|
| 286 | |||
| 287 | 20 | if ($this->properties !== []) { |
|
| 288 | 6 | $this->checkType($key, $value); |
|
| 289 | } |
||
| 290 | |||
| 291 | 19 | if ($key !== null) { |
|
| 292 | if ( |
||
| 293 | 2 | isset($this->array[$key]) |
|
| 294 | && |
||
| 295 | 2 | \is_array($this->array[$key]) |
|
| 296 | ) { |
||
| 297 | $this->array[$key][] = $value; |
||
| 298 | } else { |
||
| 299 | 2 | $this->array[$key] = $value; |
|
| 300 | } |
||
| 301 | } else { |
||
| 302 | 17 | $this->array[] = $value; |
|
| 303 | } |
||
| 304 | |||
| 305 | 19 | return $this; |
|
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Append a (key) + value to the current array. |
||
| 310 | * |
||
| 311 | * EXAMPLE: <code> |
||
| 312 | * a(['fòô' => 'bàř'])->appendImmutable('foo')->getArray(); // ['fòô' => 'bàř', 0 => 'foo'] |
||
| 313 | * </code> |
||
| 314 | * |
||
| 315 | * @param mixed $value |
||
| 316 | * @param mixed $key |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | * <p>(Immutable) Return this Arrayy object, with the appended values.</p> |
||
| 320 | * |
||
| 321 | * @phpstan-param T $value |
||
| 322 | * @phpstan-param TKey $key |
||
| 323 | * @phpstan-return static<TKey,T> |
||
| 324 | * @psalm-mutation-free |
||
| 325 | */ |
||
| 326 | 1 | View Code Duplication | public function appendImmutable($value, $key = null): self |
| 327 | { |
||
| 328 | /** |
||
| 329 | * @phpstan-return \Generator<TKey,T> $generator |
||
| 330 | */ |
||
| 331 | $generator = function () use ($key, $value): \Generator { |
||
| 332 | 1 | if ($this->properties !== []) { |
|
| 333 | $this->checkType($key, $value); |
||
| 334 | } |
||
| 335 | |||
| 336 | 1 | foreach ($this->getGenerator() as $keyOld => $itemOld) { |
|
| 337 | 1 | yield $keyOld => $itemOld; |
|
| 338 | } |
||
| 339 | |||
| 340 | 1 | if ($key !== null) { |
|
| 341 | yield $key => $value; |
||
| 342 | } else { |
||
| 343 | 1 | yield $value; |
|
| 344 | } |
||
| 345 | 1 | }; |
|
| 346 | |||
| 347 | 1 | return static::create( |
|
| 348 | 1 | $generator, |
|
| 349 | 1 | $this->iteratorClass, |
|
| 350 | 1 | false |
|
| 351 | ); |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Sort the entries by value. |
||
| 356 | * |
||
| 357 | * @param int $sort_flags [optional] <p> |
||
| 358 | * You may modify the behavior of the sort using the optional |
||
| 359 | * parameter sort_flags, for details |
||
| 360 | * see sort. |
||
| 361 | * </p> |
||
| 362 | * |
||
| 363 | * @return $this |
||
| 364 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 365 | * |
||
| 366 | * @phpstan-return static<TKey,T> |
||
| 367 | */ |
||
| 368 | 4 | public function asort(int $sort_flags = 0): self |
|
| 369 | { |
||
| 370 | 4 | $this->generatorToArray(); |
|
| 371 | |||
| 372 | 4 | \asort($this->array, $sort_flags); |
|
| 373 | |||
| 374 | 4 | return $this; |
|
| 375 | } |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Sort the entries by value. |
||
| 379 | * |
||
| 380 | * @param int $sort_flags [optional] <p> |
||
| 381 | * You may modify the behavior of the sort using the optional |
||
| 382 | * parameter sort_flags, for details |
||
| 383 | * see sort. |
||
| 384 | * </p> |
||
| 385 | * |
||
| 386 | * @return $this |
||
| 387 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 388 | * |
||
| 389 | * @phpstan-return static<TKey,T> |
||
| 390 | * @psalm-mutation-free |
||
| 391 | */ |
||
| 392 | 4 | public function asortImmutable(int $sort_flags = 0): self |
|
| 393 | { |
||
| 394 | 4 | $that = clone $this; |
|
| 395 | |||
| 396 | /** |
||
| 397 | * @psalm-suppress ImpureMethodCall - object is already cloned |
||
| 398 | */ |
||
| 399 | 4 | $that->asort($sort_flags); |
|
| 400 | |||
| 401 | 4 | return $that; |
|
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Counts all elements in an array, or something in an object. |
||
| 406 | * |
||
| 407 | * EXAMPLE: <code> |
||
| 408 | * a([-9, -8, -7, 1.32])->count(); // 4 |
||
| 409 | * </code> |
||
| 410 | * |
||
| 411 | * <p> |
||
| 412 | * For objects, if you have SPL installed, you can hook into count() by implementing interface {@see Countable}. |
||
| 413 | * The interface has exactly one method, {@see Countable::count()}, which returns the return value for the count() |
||
| 414 | * function. Please see the {@see Array} section of the manual for a detailed explanation of how arrays are |
||
| 415 | * implemented and used in PHP. |
||
| 416 | * </p> |
||
| 417 | * |
||
| 418 | * @see http://php.net/manual/en/function.count.php |
||
| 419 | * |
||
| 420 | * @param int $mode [optional] If the optional mode parameter is set to |
||
| 421 | * COUNT_RECURSIVE (or 1), count |
||
| 422 | * will recursively count the array. This is particularly useful for |
||
| 423 | * counting all the elements of a multidimensional array. count does not detect infinite recursion. |
||
| 424 | * |
||
| 425 | * @return int |
||
| 426 | * <p> |
||
| 427 | * The number of elements in var, which is |
||
| 428 | * typically an array, since anything else will have one |
||
| 429 | * element. |
||
| 430 | * </p> |
||
| 431 | * <p> |
||
| 432 | * If var is not an array or an object with |
||
| 433 | * implemented Countable interface, |
||
| 434 | * 1 will be returned. |
||
| 435 | * There is one exception, if var is &null;, |
||
| 436 | * 0 will be returned. |
||
| 437 | * </p> |
||
| 438 | * <p> |
||
| 439 | * Caution: count may return 0 for a variable that isn't set, |
||
| 440 | * but it may also return 0 for a variable that has been initialized with an |
||
| 441 | * empty array. Use isset to test if a variable is set. |
||
| 442 | * </p> |
||
| 443 | * @psalm-mutation-free |
||
| 444 | */ |
||
| 445 | 147 | public function count(int $mode = \COUNT_NORMAL): int |
|
| 446 | { |
||
| 447 | if ( |
||
| 448 | 147 | $this->generator |
|
| 449 | && |
||
| 450 | 147 | $mode === \COUNT_NORMAL |
|
| 451 | ) { |
||
| 452 | 4 | return \iterator_count($this->generator); |
|
| 453 | } |
||
| 454 | |||
| 455 | 143 | return \count($this->toArray(), $mode); |
|
| 456 | } |
||
| 457 | |||
| 458 | /** |
||
| 459 | * Exchange the array for another one. |
||
| 460 | * |
||
| 461 | * @param array|mixed|static $data |
||
| 462 | * |
||
| 463 | * 1. use the current array, if it's a array |
||
| 464 | * 2. fallback to empty array, if there is nothing |
||
| 465 | * 3. call "getArray()" on object, if there is a "Arrayy"-object |
||
| 466 | * 4. call "createFromObject()" on object, if there is a "\Traversable"-object |
||
| 467 | * 5. call "__toArray()" on object, if the method exists |
||
| 468 | * 6. cast a string or object with "__toString()" into an array |
||
| 469 | * 7. throw a "InvalidArgumentException"-Exception |
||
| 470 | * |
||
| 471 | * @return array |
||
| 472 | * |
||
| 473 | * @phpstan-param T|array<TKey,T>|self<TKey,T> $data |
||
| 474 | * @phpstan-return array<TKey,T> |
||
| 475 | */ |
||
| 476 | 1 | public function exchangeArray($data): array |
|
| 477 | { |
||
| 478 | /** @phpstan-var array<TKey,T> array */ |
||
| 479 | 1 | $array = $this->fallbackForArray($data); |
|
| 480 | |||
| 481 | 1 | $this->array = $array; |
|
| 482 | 1 | $this->generator = null; |
|
| 483 | |||
| 484 | 1 | return $this->array; |
|
| 485 | } |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Creates a copy of the ArrayyObject. |
||
| 489 | * |
||
| 490 | * @return array |
||
| 491 | * |
||
| 492 | * @phpstan-return array<int|string|TKey,T> |
||
| 493 | */ |
||
| 494 | 6 | public function getArrayCopy(): array |
|
| 495 | { |
||
| 496 | 6 | $this->generatorToArray(); |
|
| 497 | |||
| 498 | 6 | return $this->array; |
|
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Returns a new iterator, thus implementing the \Iterator interface. |
||
| 503 | * |
||
| 504 | * EXAMPLE: <code> |
||
| 505 | * a(['foo', 'bar'])->getIterator(); // ArrayyIterator['foo', 'bar'] |
||
| 506 | * </code> |
||
| 507 | * |
||
| 508 | * @return \Iterator<mixed, mixed> |
||
| 509 | * <p>An iterator for the values in the array.</p> |
||
| 510 | * @phpstan-return \Iterator<TKey, T> |
||
| 511 | */ |
||
| 512 | 28 | public function getIterator(): \Iterator |
|
| 513 | { |
||
| 514 | 28 | if ($this->generator instanceof ArrayyRewindableGenerator) { |
|
| 515 | 1 | $generator = clone $this->generator; |
|
| 516 | |||
| 517 | /** @phpstan-var \Arrayy\ArrayyRewindableGenerator<TKey,T> */ |
||
| 518 | 1 | $generatorTmp = new ArrayyRewindableExtendedGenerator( |
|
| 519 | static function () use ($generator): \Generator { |
||
| 520 | 1 | yield from $generator; |
|
| 521 | 1 | }, |
|
| 522 | 1 | null, |
|
| 523 | 1 | static::class |
|
| 524 | ); |
||
| 525 | |||
| 526 | 1 | $this->generator = $generatorTmp; |
|
| 527 | |||
| 528 | 1 | return $this->generator; |
|
| 529 | } |
||
| 530 | |||
| 531 | 27 | $iterator = $this->getIteratorClass(); |
|
| 532 | |||
| 533 | 27 | if ($iterator === ArrayyIterator::class) { |
|
| 534 | 27 | return new $iterator($this->toArray(), 0, static::class); |
|
| 535 | } |
||
| 536 | |||
| 537 | $return = new $iterator($this->toArray()); |
||
| 538 | \assert($return instanceof \Iterator); |
||
| 539 | |||
| 540 | return $return; |
||
| 541 | } |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Gets the iterator classname for the ArrayObject. |
||
| 545 | * |
||
| 546 | * @return string |
||
| 547 | * |
||
| 548 | * @phpstan-return class-string |
||
| 549 | */ |
||
| 550 | 27 | public function getIteratorClass(): string |
|
| 551 | { |
||
| 552 | 27 | return $this->iteratorClass; |
|
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Sort the entries by key. |
||
| 557 | * |
||
| 558 | * @param int $sort_flags [optional] <p> |
||
| 559 | * You may modify the behavior of the sort using the optional |
||
| 560 | * parameter sort_flags, for details |
||
| 561 | * see sort. |
||
| 562 | * </p> |
||
| 563 | * |
||
| 564 | * @return $this |
||
| 565 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 566 | * |
||
| 567 | * @phpstan-return static<TKey,T> |
||
| 568 | */ |
||
| 569 | 4 | public function ksort(int $sort_flags = 0): self |
|
| 570 | { |
||
| 571 | 4 | $this->generatorToArray(); |
|
| 572 | |||
| 573 | 4 | \ksort($this->array, $sort_flags); |
|
| 574 | |||
| 575 | 4 | return $this; |
|
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Sort the entries by key. |
||
| 580 | * |
||
| 581 | * @param int $sort_flags [optional] <p> |
||
| 582 | * You may modify the behavior of the sort using the optional |
||
| 583 | * parameter sort_flags, for details |
||
| 584 | * see sort. |
||
| 585 | * </p> |
||
| 586 | * |
||
| 587 | * @return $this |
||
| 588 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 589 | * |
||
| 590 | * @phpstan-return static<TKey,T> |
||
| 591 | */ |
||
| 592 | 4 | public function ksortImmutable(int $sort_flags = 0): self |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Sort an array using a case insensitive "natural order" algorithm. |
||
| 606 | * |
||
| 607 | * @return $this |
||
| 608 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 609 | * |
||
| 610 | * @phpstan-return static<TKey,T> |
||
| 611 | */ |
||
| 612 | 8 | public function natcasesort(): self |
|
| 613 | { |
||
| 614 | 8 | $this->generatorToArray(); |
|
| 615 | |||
| 616 | 8 | \natcasesort($this->array); |
|
| 617 | |||
| 618 | 8 | return $this; |
|
| 619 | } |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Sort an array using a case insensitive "natural order" algorithm. |
||
| 623 | * |
||
| 624 | * @return $this |
||
| 625 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 626 | * |
||
| 627 | * @phpstan-return static<TKey,T> |
||
| 628 | * @psalm-mutation-free |
||
| 629 | */ |
||
| 630 | 4 | public function natcasesortImmutable(): self |
|
| 641 | |||
| 642 | /** |
||
| 643 | * Sort entries using a "natural order" algorithm. |
||
| 644 | * |
||
| 645 | * @return $this |
||
| 646 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 647 | * |
||
| 648 | * @phpstan-return static<TKey,T> |
||
| 649 | */ |
||
| 650 | 10 | public function natsort(): self |
|
| 651 | { |
||
| 652 | 10 | $this->generatorToArray(); |
|
| 653 | |||
| 654 | 10 | \natsort($this->array); |
|
| 655 | |||
| 656 | 10 | return $this; |
|
| 657 | } |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Sort entries using a "natural order" algorithm. |
||
| 661 | * |
||
| 662 | * @return $this |
||
| 663 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 664 | * |
||
| 665 | * @phpstan-return static<TKey,T> |
||
| 666 | * @psalm-mutation-free |
||
| 667 | */ |
||
| 668 | 4 | public function natsortImmutable(): self |
|
| 679 | |||
| 680 | /** |
||
| 681 | * Whether or not an offset exists. |
||
| 682 | * |
||
| 683 | * @param bool|int|string $offset |
||
| 684 | * |
||
| 685 | * @return bool |
||
| 686 | * |
||
| 687 | * @psalm-mutation-free |
||
| 688 | */ |
||
| 689 | 164 | public function offsetExists($offset): bool |
|
| 690 | { |
||
| 691 | // php cast "bool"-index into "int"-index |
||
| 692 | 164 | if ((bool) $offset === $offset) { |
|
| 693 | 1 | $offset = (int) $offset; |
|
| 694 | } |
||
| 695 | \assert(\is_int($offset) || \is_string($offset)); |
||
| 696 | |||
| 697 | 164 | $offsetExists = $this->keyExists($offset); |
|
| 698 | 164 | if ($offsetExists === true) { |
|
| 699 | 143 | return true; |
|
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * https://github.com/vimeo/psalm/issues/2536 |
||
| 704 | * |
||
| 705 | * @psalm-suppress PossiblyInvalidArgument |
||
| 706 | * @psalm-suppress InvalidScalarArgument |
||
| 707 | */ |
||
| 708 | View Code Duplication | if ( |
|
| 709 | 124 | $this->pathSeparator |
|
| 710 | && |
||
| 711 | 124 | (string) $offset === $offset |
|
| 712 | && |
||
| 713 | 124 | \strpos($offset, $this->pathSeparator) !== false |
|
| 714 | ) { |
||
| 715 | 4 | $explodedPath = \explode($this->pathSeparator, (string) $offset); |
|
| 716 | 4 | if ($explodedPath !== false) { |
|
| 717 | /** @var string $lastOffset - helper for phpstan */ |
||
| 718 | 4 | $lastOffset = \array_pop($explodedPath); |
|
| 719 | 4 | $containerPath = \implode($this->pathSeparator, $explodedPath); |
|
| 720 | |||
| 721 | /** |
||
| 722 | * @psalm-suppress MissingClosureReturnType |
||
| 723 | * @psalm-suppress MissingClosureParamType |
||
| 724 | */ |
||
| 725 | 4 | $this->callAtPath( |
|
| 726 | 4 | $containerPath, |
|
| 727 | static function ($container) use ($lastOffset, &$offsetExists) { |
||
| 728 | 4 | $offsetExists = \array_key_exists($lastOffset, $container); |
|
| 729 | 4 | } |
|
| 730 | ); |
||
| 731 | } |
||
| 732 | } |
||
| 733 | |||
| 734 | 124 | return $offsetExists; |
|
| 735 | } |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Returns the value at specified offset. |
||
| 739 | * |
||
| 740 | * @param int|string $offset |
||
| 741 | * |
||
| 742 | * @return mixed |
||
| 743 | * <p>Will return null if the offset did not exists.</p> |
||
| 744 | * |
||
| 745 | * @phpstan-param TKey $offset |
||
| 746 | */ |
||
| 747 | 133 | public function &offsetGet($offset) |
|
| 758 | |||
| 759 | /** |
||
| 760 | * Assigns a value to the specified offset + check the type. |
||
| 761 | * |
||
| 762 | * @param int|string|null $offset |
||
| 763 | * @param mixed $value |
||
| 764 | * |
||
| 765 | * @return void |
||
| 766 | */ |
||
| 767 | 28 | public function offsetSet($offset, $value) |
|
| 785 | |||
| 786 | /** |
||
| 787 | * Unset an offset. |
||
| 788 | * |
||
| 789 | * @param int|string $offset |
||
| 790 | * |
||
| 791 | * @return void |
||
| 792 | * <p>(Mutable) Return nothing.</p> |
||
| 793 | */ |
||
| 794 | 26 | public function offsetUnset($offset) |
|
| 795 | { |
||
| 796 | 26 | $this->generatorToArray(); |
|
| 797 | |||
| 798 | 26 | if ($this->array === []) { |
|
| 799 | 6 | return; |
|
| 800 | } |
||
| 801 | |||
| 802 | 21 | if ($this->keyExists($offset)) { |
|
| 803 | 14 | unset($this->array[$offset]); |
|
| 804 | |||
| 805 | 14 | return; |
|
| 806 | } |
||
| 807 | |||
| 808 | /** |
||
| 809 | * https://github.com/vimeo/psalm/issues/2536 |
||
| 810 | * |
||
| 811 | * @psalm-suppress PossiblyInvalidArgument |
||
| 812 | * @psalm-suppress InvalidScalarArgument |
||
| 813 | */ |
||
| 814 | View Code Duplication | if ( |
|
| 815 | 10 | $this->pathSeparator |
|
| 816 | && |
||
| 817 | 10 | (string) $offset === $offset |
|
| 818 | && |
||
| 819 | 10 | \strpos($offset, $this->pathSeparator) !== false |
|
| 820 | ) { |
||
| 821 | 7 | $path = \explode($this->pathSeparator, (string) $offset); |
|
| 822 | |||
| 823 | 7 | if ($path !== false) { |
|
| 824 | 7 | $pathToUnset = \array_pop($path); |
|
| 825 | |||
| 826 | /** |
||
| 827 | * @psalm-suppress MissingClosureReturnType |
||
| 828 | * @psalm-suppress MissingClosureParamType |
||
| 829 | */ |
||
| 830 | 7 | $this->callAtPath( |
|
| 831 | 7 | \implode($this->pathSeparator, $path), |
|
| 832 | static function (&$offset) use ($pathToUnset) { |
||
| 833 | 6 | if (\is_array($offset)) { |
|
| 834 | 5 | unset($offset[$pathToUnset]); |
|
| 835 | } else { |
||
| 836 | 1 | $offset = null; |
|
| 837 | } |
||
| 838 | 7 | } |
|
| 839 | ); |
||
| 840 | } |
||
| 841 | } |
||
| 842 | |||
| 843 | 10 | unset($this->array[$offset]); |
|
| 844 | 10 | } |
|
| 845 | |||
| 846 | /** |
||
| 847 | * Serialize the current "Arrayy"-object. |
||
| 848 | * |
||
| 849 | * EXAMPLE: <code> |
||
| 850 | * a([1, 4, 7])->serialize(); |
||
| 851 | * </code> |
||
| 852 | * |
||
| 853 | * @return string |
||
| 854 | */ |
||
| 855 | 2 | public function serialize(): string |
|
| 865 | |||
| 866 | /** |
||
| 867 | * Sets the iterator classname for the current "Arrayy"-object. |
||
| 868 | * |
||
| 869 | * @param string $iteratorClass |
||
| 870 | * |
||
| 871 | * @throws \InvalidArgumentException |
||
| 872 | * |
||
| 873 | * @return void |
||
| 874 | * |
||
| 875 | * @phpstan-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 876 | */ |
||
| 877 | 1205 | public function setIteratorClass($iteratorClass) |
|
| 900 | |||
| 901 | /** |
||
| 902 | * Sort the entries with a user-defined comparison function and maintain key association. |
||
| 903 | * |
||
| 904 | * @param callable $callable |
||
| 905 | * |
||
| 906 | *@throws \InvalidArgumentException |
||
| 907 | * |
||
| 908 | * @return $this |
||
| 909 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 910 | * |
||
| 911 | * @phpstan-param callable(T,T):int $callable |
||
| 912 | * @phpstan-return static<TKey,T> |
||
| 913 | */ |
||
| 914 | 8 | public function uasort($callable): self |
|
| 926 | |||
| 927 | /** |
||
| 928 | * Sort the entries with a user-defined comparison function and maintain key association. |
||
| 929 | * |
||
| 930 | * @param callable $callable |
||
| 931 | * |
||
| 932 | *@throws \InvalidArgumentException |
||
| 933 | * |
||
| 934 | * @return $this |
||
| 935 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 936 | * |
||
| 937 | * @phpstan-param callable(T,T):int $callable |
||
| 938 | * @phpstan-return static<TKey,T> |
||
| 939 | * @psalm-mutation-free |
||
| 940 | */ |
||
| 941 | 4 | public function uasortImmutable($callable): self |
|
| 952 | |||
| 953 | /** |
||
| 954 | * Sort the entries by keys using a user-defined comparison function. |
||
| 955 | * |
||
| 956 | * @param callable $callable |
||
| 957 | * |
||
| 958 | * @throws \InvalidArgumentException |
||
| 959 | * |
||
| 960 | * @return static |
||
| 961 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 962 | * |
||
| 963 | * @phpstan-param callable(TKey,TKey):int $callable |
||
| 964 | * @phpstan-return static<TKey,T> |
||
| 965 | */ |
||
| 966 | 5 | public function uksort($callable): self |
|
| 970 | |||
| 971 | /** |
||
| 972 | * Sort the entries by keys using a user-defined comparison function. |
||
| 973 | * |
||
| 974 | * @param callable $callable |
||
| 975 | * |
||
| 976 | * @throws \InvalidArgumentException |
||
| 977 | * |
||
| 978 | * @return static |
||
| 979 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 980 | * |
||
| 981 | * @phpstan-param callable(TKey,TKey):int $callable |
||
| 982 | * @phpstan-return static<TKey,T> |
||
| 983 | * @psalm-mutation-free |
||
| 984 | */ |
||
| 985 | 1 | public function uksortImmutable($callable): self |
|
| 989 | |||
| 990 | /** |
||
| 991 | * Unserialize an string and return the instance of the "Arrayy"-class. |
||
| 992 | * |
||
| 993 | * EXAMPLE: <code> |
||
| 994 | * $serialized = a([1, 4, 7])->serialize(); |
||
| 995 | * a()->unserialize($serialized); |
||
| 996 | * </code> |
||
| 997 | * |
||
| 998 | * @param string $string |
||
| 999 | * |
||
| 1000 | * @return $this |
||
| 1001 | * |
||
| 1002 | * @phpstan-return static<TKey,T> |
||
| 1003 | */ |
||
| 1004 | 2 | public function unserialize($string): self |
|
| 1014 | |||
| 1015 | /** |
||
| 1016 | * Append a (key) + values to the current array. |
||
| 1017 | * |
||
| 1018 | * EXAMPLE: <code> |
||
| 1019 | * a(['fòô' => ['bàř']])->appendArrayValues(['foo1', 'foo2'], 'fòô'); // Arrayy['fòô' => ['bàř', 'foo1', 'foo2']] |
||
| 1020 | * </code> |
||
| 1021 | * |
||
| 1022 | * @param array $values |
||
| 1023 | * @param mixed $key |
||
| 1024 | * |
||
| 1025 | * @return $this |
||
| 1026 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 1027 | * |
||
| 1028 | * @phpstan-param array<array-key,T> $values |
||
| 1029 | * @phpstan-param TKey|null $key |
||
| 1030 | * @phpstan-return static<TKey,T> |
||
| 1031 | */ |
||
| 1032 | 1 | public function appendArrayValues(array $values, $key = null) |
|
| 1058 | |||
| 1059 | /** |
||
| 1060 | * Add a suffix to each key. |
||
| 1061 | * |
||
| 1062 | * @param int|string $prefix |
||
| 1063 | * |
||
| 1064 | * @return static |
||
| 1065 | * <p>(Immutable) Return an Arrayy object, with the prefixed keys.</p> |
||
| 1066 | * |
||
| 1067 | * @phpstan-return static<TKey,T> |
||
| 1068 | * @psalm-mutation-free |
||
| 1069 | */ |
||
| 1070 | 10 | View Code Duplication | public function appendToEachKey($prefix): self |
| 1093 | |||
| 1094 | /** |
||
| 1095 | * Add a prefix to each value. |
||
| 1096 | * |
||
| 1097 | * @param float|int|string $prefix |
||
| 1098 | * |
||
| 1099 | * @return static |
||
| 1100 | * <p>(Immutable) Return an Arrayy object, with the prefixed values.</p> |
||
| 1101 | * |
||
| 1102 | * @phpstan-return static<TKey,T> |
||
| 1103 | * @psalm-mutation-free |
||
| 1104 | */ |
||
| 1105 | 10 | View Code Duplication | public function appendToEachValue($prefix): self |
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Sort an array in reverse order and maintain index association. |
||
| 1127 | * |
||
| 1128 | * @return $this |
||
| 1129 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1130 | * |
||
| 1131 | * @phpstan-return static<TKey,T> |
||
| 1132 | */ |
||
| 1133 | 4 | public function arsort(): self |
|
| 1141 | |||
| 1142 | /** |
||
| 1143 | * Sort an array in reverse order and maintain index association. |
||
| 1144 | * |
||
| 1145 | * @return $this |
||
| 1146 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 1147 | * |
||
| 1148 | * @phpstan-return static<TKey,T> |
||
| 1149 | * @psalm-mutation-free |
||
| 1150 | */ |
||
| 1151 | 10 | public function arsortImmutable(): self |
|
| 1161 | |||
| 1162 | /** |
||
| 1163 | * Iterate over the current array and execute a callback for each loop. |
||
| 1164 | * |
||
| 1165 | * EXAMPLE: <code> |
||
| 1166 | * $result = A::create(); |
||
| 1167 | * $closure = function ($value, $key) use ($result) { |
||
| 1168 | * $result[$key] = ':' . $value . ':'; |
||
| 1169 | * }; |
||
| 1170 | * a(['foo', 'bar' => 'bis'])->at($closure); // Arrayy[':foo:', 'bar' => ':bis:'] |
||
| 1171 | * </code> |
||
| 1172 | * |
||
| 1173 | * @param \Closure $closure |
||
| 1174 | * |
||
| 1175 | * @return static |
||
| 1176 | * <p>(Immutable)</p> |
||
| 1177 | * |
||
| 1178 | * @phpstan-param \Closure(T=,TKey=):mixed $closure <p>INFO: \Closure result is not used, but void is not supported in PHP 7.0</p> |
||
| 1179 | * @phpstan-return static<TKey,T> |
||
| 1180 | * @psalm-mutation-free |
||
| 1181 | */ |
||
| 1182 | 3 | public function at(\Closure $closure): self |
|
| 1196 | |||
| 1197 | /** |
||
| 1198 | * Returns the average value of the current array. |
||
| 1199 | * |
||
| 1200 | * EXAMPLE: <code> |
||
| 1201 | * a([-9, -8, -7, 1.32])->average(2); // -5.67 |
||
| 1202 | * </code> |
||
| 1203 | * |
||
| 1204 | * @param int $decimals <p>The number of decimal-numbers to return.</p> |
||
| 1205 | * |
||
| 1206 | * @return float|int |
||
| 1207 | * <p>The average value.</p> |
||
| 1208 | * @psalm-mutation-free |
||
| 1209 | */ |
||
| 1210 | 10 | public function average($decimals = 0) |
|
| 1224 | |||
| 1225 | /** |
||
| 1226 | * Changes all keys in an array. |
||
| 1227 | * |
||
| 1228 | * @param int $case [optional] <p> Either <strong>CASE_UPPER</strong><br /> |
||
| 1229 | * or <strong>CASE_LOWER</strong> (default)</p> |
||
| 1230 | * |
||
| 1231 | * @return static |
||
| 1232 | * <p>(Immutable)</p> |
||
| 1233 | * |
||
| 1234 | * @phpstan-return static<TKey,T> |
||
| 1235 | * @psalm-mutation-free |
||
| 1236 | */ |
||
| 1237 | 1 | public function changeKeyCase(int $case = \CASE_LOWER): self |
|
| 1266 | |||
| 1267 | /** |
||
| 1268 | * Change the path separator of the array wrapper. |
||
| 1269 | * |
||
| 1270 | * By default, the separator is: "." |
||
| 1271 | * |
||
| 1272 | * @param string $separator <p>Separator to set.</p> |
||
| 1273 | * |
||
| 1274 | * @return $this |
||
| 1275 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1276 | * |
||
| 1277 | * @phpstan-return static<TKey,T> |
||
| 1278 | */ |
||
| 1279 | 11 | public function changeSeparator($separator): self |
|
| 1285 | |||
| 1286 | /** |
||
| 1287 | * Create a chunked version of the current array. |
||
| 1288 | * |
||
| 1289 | * EXAMPLE: <code> |
||
| 1290 | * a([-9, -8, -7, 1.32])->chunk(2); // Arrayy[[-9, -8], [-7, 1.32]] |
||
| 1291 | * </code> |
||
| 1292 | * |
||
| 1293 | * @param int $size <p>Size of each chunk.</p> |
||
| 1294 | * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
||
| 1295 | * |
||
| 1296 | * @return static |
||
| 1297 | * <p>(Immutable) A new array of chunks from the original array.</p> |
||
| 1298 | * |
||
| 1299 | * @phpstan-return static<TKey,T> |
||
| 1300 | * @psalm-mutation-free |
||
| 1301 | */ |
||
| 1302 | 5 | public function chunk($size, $preserveKeys = false): self |
|
| 1352 | |||
| 1353 | /** |
||
| 1354 | * Clean all falsy values from the current array. |
||
| 1355 | * |
||
| 1356 | * EXAMPLE: <code> |
||
| 1357 | * a([-8 => -9, 1, 2 => false])->clean(); // Arrayy[-8 => -9, 1] |
||
| 1358 | * </code> |
||
| 1359 | * |
||
| 1360 | * @return static |
||
| 1361 | * <p>(Immutable)</p> |
||
| 1362 | * |
||
| 1363 | * @phpstan-return static<TKey,T> |
||
| 1364 | * @psalm-mutation-free |
||
| 1365 | */ |
||
| 1366 | 8 | public function clean(): self |
|
| 1374 | |||
| 1375 | /** |
||
| 1376 | * WARNING!!! -> Clear the current full array or a $key of it. |
||
| 1377 | * |
||
| 1378 | * EXAMPLE: <code> |
||
| 1379 | * a([-8 => -9, 1, 2 => false])->clear(); // Arrayy[] |
||
| 1380 | * </code> |
||
| 1381 | * |
||
| 1382 | * @param int|int[]|string|string[]|null $key |
||
| 1383 | * |
||
| 1384 | * @return $this |
||
| 1385 | * <p>(Mutable) Return this Arrayy object, with an empty array.</p> |
||
| 1386 | * |
||
| 1387 | * @phpstan-return static<TKey,T> |
||
| 1388 | */ |
||
| 1389 | 10 | public function clear($key = null): self |
|
| 1408 | |||
| 1409 | /** |
||
| 1410 | * Check if an item is in the current array. |
||
| 1411 | * |
||
| 1412 | * EXAMPLE: <code> |
||
| 1413 | * a([1, true])->contains(true); // true |
||
| 1414 | * </code> |
||
| 1415 | * |
||
| 1416 | * @param float|int|string $value |
||
| 1417 | * @param bool $recursive |
||
| 1418 | * @param bool $strict |
||
| 1419 | * |
||
| 1420 | * @return bool |
||
| 1421 | * @psalm-mutation-free |
||
| 1422 | */ |
||
| 1423 | 23 | public function contains($value, bool $recursive = false, bool $strict = true): bool |
|
| 1445 | |||
| 1446 | /** |
||
| 1447 | * Check if an (case-insensitive) string is in the current array. |
||
| 1448 | * |
||
| 1449 | * EXAMPLE: <code> |
||
| 1450 | * a(['E', 'é'])->containsCaseInsensitive('É'); // true |
||
| 1451 | * </code> |
||
| 1452 | * |
||
| 1453 | * @param mixed $value |
||
| 1454 | * @param bool $recursive |
||
| 1455 | * |
||
| 1456 | * @return bool |
||
| 1457 | * @psalm-mutation-free |
||
| 1458 | * |
||
| 1459 | * @psalm-suppress InvalidCast - hack for int|float|bool support |
||
| 1460 | */ |
||
| 1461 | 26 | public function containsCaseInsensitive($value, $recursive = false): bool |
|
| 1492 | |||
| 1493 | /** |
||
| 1494 | * Check if the given key/index exists in the array. |
||
| 1495 | * |
||
| 1496 | * EXAMPLE: <code> |
||
| 1497 | * a([1 => true])->containsKey(1); // true |
||
| 1498 | * </code> |
||
| 1499 | * |
||
| 1500 | * @param int|string $key <p>key/index to search for</p> |
||
| 1501 | * |
||
| 1502 | * @return bool |
||
| 1503 | * <p>Returns true if the given key/index exists in the array, false otherwise.</p> |
||
| 1504 | * |
||
| 1505 | * @psalm-mutation-free |
||
| 1506 | */ |
||
| 1507 | 4 | public function containsKey($key): bool |
|
| 1511 | |||
| 1512 | /** |
||
| 1513 | * Check if all given needles are present in the array as key/index. |
||
| 1514 | * |
||
| 1515 | * EXAMPLE: <code> |
||
| 1516 | * a([1 => true])->containsKeys(array(1 => 0)); // true |
||
| 1517 | * </code> |
||
| 1518 | * |
||
| 1519 | * @param array $needles <p>The keys you are searching for.</p> |
||
| 1520 | * @param bool $recursive |
||
| 1521 | * |
||
| 1522 | * @return bool |
||
| 1523 | * <p>Returns true if all the given keys/indexes exists in the array, false otherwise.</p> |
||
| 1524 | * |
||
| 1525 | * @phpstan-param array<array-key>|array<TKey> $needles |
||
| 1526 | * @psalm-mutation-free |
||
| 1527 | */ |
||
| 1528 | 2 | public function containsKeys(array $needles, $recursive = false): bool |
|
| 1556 | |||
| 1557 | /** |
||
| 1558 | * Check if all given needles are present in the array as key/index. |
||
| 1559 | * |
||
| 1560 | * @param array $needles <p>The keys you are searching for.</p> |
||
| 1561 | * |
||
| 1562 | * @return bool |
||
| 1563 | * <p>Returns true if all the given keys/indexes exists in the array, false otherwise.</p> |
||
| 1564 | * |
||
| 1565 | * @phpstan-param array<array-key>|array<TKey> $needles |
||
| 1566 | * @psalm-mutation-free |
||
| 1567 | */ |
||
| 1568 | 1 | public function containsKeysRecursive(array $needles): bool |
|
| 1572 | |||
| 1573 | /** |
||
| 1574 | * alias: for "Arrayy->contains()" |
||
| 1575 | * |
||
| 1576 | * @param float|int|string $value |
||
| 1577 | * |
||
| 1578 | * @return bool |
||
| 1579 | * |
||
| 1580 | * @see Arrayy::contains() |
||
| 1581 | * @psalm-mutation-free |
||
| 1582 | */ |
||
| 1583 | 9 | public function containsValue($value): bool |
|
| 1587 | |||
| 1588 | /** |
||
| 1589 | * alias: for "Arrayy->contains($value, true)" |
||
| 1590 | * |
||
| 1591 | * @param float|int|string $value |
||
| 1592 | * |
||
| 1593 | * @return bool |
||
| 1594 | * |
||
| 1595 | * @see Arrayy::contains() |
||
| 1596 | * @psalm-mutation-free |
||
| 1597 | */ |
||
| 1598 | 18 | public function containsValueRecursive($value): bool |
|
| 1602 | |||
| 1603 | /** |
||
| 1604 | * Check if all given needles are present in the array. |
||
| 1605 | * |
||
| 1606 | * EXAMPLE: <code> |
||
| 1607 | * a([1, true])->containsValues(array(1, true)); // true |
||
| 1608 | * </code> |
||
| 1609 | * |
||
| 1610 | * @param array $needles |
||
| 1611 | * |
||
| 1612 | * @return bool |
||
| 1613 | * <p>Returns true if all the given values exists in the array, false otherwise.</p> |
||
| 1614 | * |
||
| 1615 | * @phpstan-param array<mixed>|array<T> $needles |
||
| 1616 | * @psalm-mutation-free |
||
| 1617 | */ |
||
| 1618 | 1 | public function containsValues(array $needles): bool |
|
| 1633 | |||
| 1634 | /** |
||
| 1635 | * Counts all the values of an array |
||
| 1636 | * |
||
| 1637 | * @see http://php.net/manual/en/function.array-count-values.php |
||
| 1638 | * |
||
| 1639 | * @return static |
||
| 1640 | * <p> |
||
| 1641 | * (Immutable) |
||
| 1642 | * An associative Arrayy-object of values from input as |
||
| 1643 | * keys and their count as value. |
||
| 1644 | * </p> |
||
| 1645 | * |
||
| 1646 | * @phpstan-return static<array-key,int> |
||
| 1647 | * @psalm-mutation-free |
||
| 1648 | */ |
||
| 1649 | 7 | public function countValues(): self |
|
| 1656 | |||
| 1657 | /** |
||
| 1658 | * Creates an Arrayy object. |
||
| 1659 | * |
||
| 1660 | * @param mixed $data |
||
| 1661 | * @param string $iteratorClass |
||
| 1662 | * @param bool $checkPropertiesInConstructor |
||
| 1663 | * |
||
| 1664 | * @return static |
||
| 1665 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1666 | * |
||
| 1667 | * @phpstan-param class-string<\Arrayy\ArrayyIterator> $iteratorClass |
||
| 1668 | * @phpstan-return static<TKey,T> |
||
| 1669 | * @psalm-mutation-free |
||
| 1670 | */ |
||
| 1671 | 733 | public static function create( |
|
| 1672 | $data = [], |
||
| 1673 | string $iteratorClass = ArrayyIterator::class, |
||
| 1674 | bool $checkPropertiesInConstructor = true |
||
| 1675 | ) { |
||
| 1676 | 733 | return new static( |
|
| 1677 | 733 | $data, |
|
| 1678 | $iteratorClass, |
||
| 1679 | $checkPropertiesInConstructor |
||
| 1680 | ); |
||
| 1681 | } |
||
| 1682 | |||
| 1683 | /** |
||
| 1684 | * Flatten an array with the given character as a key delimiter. |
||
| 1685 | * |
||
| 1686 | * EXAMPLE: <code> |
||
| 1687 | * $dot = a(['foo' => ['abc' => 'xyz', 'bar' => ['baz']]]); |
||
| 1688 | * $flatten = $dot->flatten(); |
||
| 1689 | * $flatten['foo.abc']; // 'xyz' |
||
| 1690 | * $flatten['foo.bar.0']; // 'baz' |
||
| 1691 | * </code> |
||
| 1692 | * |
||
| 1693 | * @param string $delimiter |
||
| 1694 | * @param string $prepend |
||
| 1695 | * @param array|null $items |
||
| 1696 | * |
||
| 1697 | * @return array |
||
| 1698 | */ |
||
| 1699 | 2 | public function flatten($delimiter = '.', $prepend = '', $items = null) |
|
| 1722 | |||
| 1723 | /** |
||
| 1724 | * WARNING: Creates an Arrayy object by reference. |
||
| 1725 | * |
||
| 1726 | * @param array $array |
||
| 1727 | * |
||
| 1728 | * @return $this |
||
| 1729 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1730 | * |
||
| 1731 | * @phpstan-param array<TKey,T> $array |
||
| 1732 | * @phpstan-return $this<TKey,T> |
||
| 1733 | * |
||
| 1734 | * @internal this will not check any types because it's set directly as reference |
||
| 1735 | */ |
||
| 1736 | 26 | public function createByReference(array &$array = []): self |
|
| 1743 | |||
| 1744 | /** |
||
| 1745 | * Create an new instance from a callable function which will return an Generator. |
||
| 1746 | * |
||
| 1747 | * @param callable $generatorFunction |
||
| 1748 | * |
||
| 1749 | * @return static |
||
| 1750 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1751 | * |
||
| 1752 | * @phpstan-param callable():\Generator<TKey,T> $generatorFunction |
||
| 1753 | * @phpstan-return static<TKey,T> |
||
| 1754 | * @psalm-mutation-free |
||
| 1755 | */ |
||
| 1756 | 8 | public static function createFromGeneratorFunction(callable $generatorFunction): self |
|
| 1760 | |||
| 1761 | /** |
||
| 1762 | * Create an new instance filled with a copy of values from a "Generator"-object. |
||
| 1763 | * |
||
| 1764 | * @param \Generator $generator |
||
| 1765 | * |
||
| 1766 | * @return static |
||
| 1767 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1768 | * |
||
| 1769 | * @phpstan-param \Generator<TKey,T> $generator |
||
| 1770 | * @phpstan-return static<TKey,T> |
||
| 1771 | * @psalm-mutation-free |
||
| 1772 | */ |
||
| 1773 | 4 | public static function createFromGeneratorImmutable(\Generator $generator): self |
|
| 1777 | |||
| 1778 | /** |
||
| 1779 | * Create an new Arrayy object via JSON. |
||
| 1780 | * |
||
| 1781 | * @param string $json |
||
| 1782 | * |
||
| 1783 | * @return static |
||
| 1784 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1785 | * |
||
| 1786 | * @phpstan-return static<int|string,mixed> |
||
| 1787 | * @psalm-mutation-free |
||
| 1788 | */ |
||
| 1789 | 6 | public static function createFromJson(string $json): self |
|
| 1793 | |||
| 1794 | /** |
||
| 1795 | * Create an new Arrayy object via JSON. |
||
| 1796 | * |
||
| 1797 | * @param array $array |
||
| 1798 | * |
||
| 1799 | * @return static |
||
| 1800 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1801 | * |
||
| 1802 | * @phpstan-param array<TKey,T> $array |
||
| 1803 | * @phpstan-return static<TKey,T> |
||
| 1804 | * @psalm-mutation-free |
||
| 1805 | */ |
||
| 1806 | 1 | public static function createFromArray(array $array): self |
|
| 1810 | |||
| 1811 | /** |
||
| 1812 | * Create an new instance filled with values from an object that is iterable. |
||
| 1813 | * |
||
| 1814 | * @param \Traversable $object <p>iterable object</p> |
||
| 1815 | * |
||
| 1816 | * @return static |
||
| 1817 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1818 | * |
||
| 1819 | * @phpstan-param \Traversable<array-key,T> $object |
||
| 1820 | * @phpstan-return static<array-key,T> |
||
| 1821 | * @psalm-mutation-free |
||
| 1822 | */ |
||
| 1823 | 4 | public static function createFromObject(\Traversable $object): self |
|
| 1843 | |||
| 1844 | /** |
||
| 1845 | * Create an new instance filled with values from an object. |
||
| 1846 | * |
||
| 1847 | * @param object $object |
||
| 1848 | * |
||
| 1849 | * @return static |
||
| 1850 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1851 | * |
||
| 1852 | * @phpstan-return static<array-key,mixed> |
||
| 1853 | * @psalm-mutation-free |
||
| 1854 | */ |
||
| 1855 | 5 | public static function createFromObjectVars($object): self |
|
| 1859 | |||
| 1860 | /** |
||
| 1861 | * Create an new Arrayy object via string. |
||
| 1862 | * |
||
| 1863 | * @param string $str <p>The input string.</p> |
||
| 1864 | * @param string|null $delimiter <p>The boundary string.</p> |
||
| 1865 | * @param string|null $regEx <p>Use the $delimiter or the $regEx, so if $pattern is null, $delimiter will be |
||
| 1866 | * used.</p> |
||
| 1867 | * |
||
| 1868 | * @return static |
||
| 1869 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1870 | * |
||
| 1871 | * @phpstan-return static<int,string> |
||
| 1872 | * @psalm-mutation-free |
||
| 1873 | */ |
||
| 1874 | 10 | public static function createFromString(string $str, string $delimiter = null, string $regEx = null): self |
|
| 1909 | |||
| 1910 | /** |
||
| 1911 | * Create an new instance filled with a copy of values from a "Traversable"-object. |
||
| 1912 | * |
||
| 1913 | * @param \Traversable $traversable |
||
| 1914 | * @param bool $use_keys [optional] <p> |
||
| 1915 | * Whether to use the iterator element keys as index. |
||
| 1916 | * </p> |
||
| 1917 | * |
||
| 1918 | * @return static |
||
| 1919 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1920 | * |
||
| 1921 | * @phpstan-param \Traversable<array-key|TKey,T> $traversable |
||
| 1922 | * @phpstan-return static<int|TKey,T> |
||
| 1923 | * @psalm-mutation-free |
||
| 1924 | */ |
||
| 1925 | 1 | public static function createFromTraversableImmutable(\Traversable $traversable, bool $use_keys = true): self |
|
| 1929 | |||
| 1930 | /** |
||
| 1931 | * Create an new instance containing a range of elements. |
||
| 1932 | * |
||
| 1933 | * @param float|int|string $low <p>First value of the sequence.</p> |
||
| 1934 | * @param float|int|string $high <p>The sequence is ended upon reaching the end value.</p> |
||
| 1935 | * @param float|int $step <p>Used as the increment between elements in the sequence.</p> |
||
| 1936 | * |
||
| 1937 | * @return static |
||
| 1938 | * <p>(Immutable) Returns an new instance of the Arrayy object.</p> |
||
| 1939 | * |
||
| 1940 | * @phpstan-return static<int,int|string> |
||
| 1941 | * @psalm-mutation-free |
||
| 1942 | */ |
||
| 1943 | 2 | public static function createWithRange($low, $high, $step = 1): self |
|
| 1950 | |||
| 1951 | /** |
||
| 1952 | * Gets the element of the array at the current internal iterator position. |
||
| 1953 | * |
||
| 1954 | * @return false|mixed |
||
| 1955 | * |
||
| 1956 | * @phpstan-return false|T |
||
| 1957 | */ |
||
| 1958 | public function current() |
||
| 1966 | |||
| 1967 | /** |
||
| 1968 | * Custom sort by index via "uksort". |
||
| 1969 | * |
||
| 1970 | * EXAMPLE: <code> |
||
| 1971 | * $callable = function ($a, $b) { |
||
| 1972 | * if ($a == $b) { |
||
| 1973 | * return 0; |
||
| 1974 | * } |
||
| 1975 | * return ($a > $b) ? 1 : -1; |
||
| 1976 | * }; |
||
| 1977 | * $arrayy = a(['three' => 3, 'one' => 1, 'two' => 2]); |
||
| 1978 | * $resultArrayy = $arrayy->customSortKeys($callable); // Arrayy['one' => 1, 'three' => 3, 'two' => 2] |
||
| 1979 | * </code> |
||
| 1980 | * |
||
| 1981 | * @see http://php.net/manual/en/function.uksort.php |
||
| 1982 | * |
||
| 1983 | * @param callable $callable |
||
| 1984 | * |
||
| 1985 | * @throws \InvalidArgumentException |
||
| 1986 | * |
||
| 1987 | * @return $this |
||
| 1988 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 1989 | * |
||
| 1990 | * @phpstan-param callable(TKey,TKey):int $callable |
||
| 1991 | * @phpstan-return static<TKey,T> |
||
| 1992 | */ |
||
| 1993 | 5 | public function customSortKeys(callable $callable): self |
|
| 2002 | |||
| 2003 | /** |
||
| 2004 | * Custom sort by index via "uksort". |
||
| 2005 | * |
||
| 2006 | * @see http://php.net/manual/en/function.uksort.php |
||
| 2007 | * |
||
| 2008 | * @param callable $callable |
||
| 2009 | * |
||
| 2010 | * @throws \InvalidArgumentException |
||
| 2011 | * |
||
| 2012 | * @return $this |
||
| 2013 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 2014 | * |
||
| 2015 | * @phpstan-param callable(TKey,TKey):int $callable |
||
| 2016 | * @phpstan-return static<TKey,T> |
||
| 2017 | * @psalm-mutation-free |
||
| 2018 | */ |
||
| 2019 | 1 | public function customSortKeysImmutable(callable $callable): self |
|
| 2033 | |||
| 2034 | /** |
||
| 2035 | * Custom sort by value via "usort". |
||
| 2036 | * |
||
| 2037 | * EXAMPLE: <code> |
||
| 2038 | * $callable = function ($a, $b) { |
||
| 2039 | * if ($a == $b) { |
||
| 2040 | * return 0; |
||
| 2041 | * } |
||
| 2042 | * return ($a > $b) ? 1 : -1; |
||
| 2043 | * }; |
||
| 2044 | * $arrayy = a(['three' => 3, 'one' => 1, 'two' => 2]); |
||
| 2045 | * $resultArrayy = $arrayy->customSortValues($callable); // Arrayy[1, 2, 3] |
||
| 2046 | * </code> |
||
| 2047 | * |
||
| 2048 | * @see http://php.net/manual/en/function.usort.php |
||
| 2049 | * |
||
| 2050 | * @param callable $callable |
||
| 2051 | * |
||
| 2052 | * @return $this |
||
| 2053 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 2054 | * |
||
| 2055 | * @phpstan-param callable(T,T):int $callable |
||
| 2056 | * @phpstan-return static<int,T> |
||
| 2057 | */ |
||
| 2058 | 11 | public function customSortValues(callable $callable): self |
|
| 2066 | |||
| 2067 | /** |
||
| 2068 | * Custom sort by value via "usort". |
||
| 2069 | * |
||
| 2070 | * @see http://php.net/manual/en/function.usort.php |
||
| 2071 | * |
||
| 2072 | * @param callable $callable |
||
| 2073 | * |
||
| 2074 | * @throws \InvalidArgumentException |
||
| 2075 | * |
||
| 2076 | * @return $this |
||
| 2077 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 2078 | * |
||
| 2079 | * @phpstan-param callable(T,T):int $callable |
||
| 2080 | * @phpstan-return static<int,T> |
||
| 2081 | * @psalm-mutation-free |
||
| 2082 | */ |
||
| 2083 | 5 | public function customSortValuesImmutable($callable): self |
|
| 2094 | |||
| 2095 | /** |
||
| 2096 | * Delete the given key or keys. |
||
| 2097 | * |
||
| 2098 | * @param int|int[]|string|string[] $keyOrKeys |
||
| 2099 | * |
||
| 2100 | * @return void |
||
| 2101 | */ |
||
| 2102 | 9 | public function delete($keyOrKeys) |
|
| 2110 | |||
| 2111 | /** |
||
| 2112 | * Return elements where the values that are only in the current array. |
||
| 2113 | * |
||
| 2114 | * EXAMPLE: <code> |
||
| 2115 | * a([1 => 1, 2 => 2])->diff([1 => 1]); // Arrayy[2 => 2] |
||
| 2116 | * </code> |
||
| 2117 | * |
||
| 2118 | * @param array ...$array |
||
| 2119 | * |
||
| 2120 | * @return static |
||
| 2121 | * <p>(Immutable)</p> |
||
| 2122 | * |
||
| 2123 | * @phpstan-param array<TKey,T> ...$array |
||
| 2124 | * @phpstan-return static<TKey,T> |
||
| 2125 | * @psalm-mutation-free |
||
| 2126 | */ |
||
| 2127 | 13 | View Code Duplication | public function diff(array ...$array): self |
| 2149 | |||
| 2150 | /** |
||
| 2151 | * Return elements where the keys are only in the current array. |
||
| 2152 | * |
||
| 2153 | * @param array ...$array |
||
| 2154 | * |
||
| 2155 | * @return static |
||
| 2156 | * <p>(Immutable)</p> |
||
| 2157 | * |
||
| 2158 | * @phpstan-param array<TKey,T> ...$array |
||
| 2159 | * @phpstan-return static<TKey,T> |
||
| 2160 | * @psalm-mutation-free |
||
| 2161 | */ |
||
| 2162 | 9 | View Code Duplication | public function diffKey(array ...$array): self |
| 2184 | |||
| 2185 | /** |
||
| 2186 | * Return elements where the values and keys are only in the current array. |
||
| 2187 | * |
||
| 2188 | * @param array ...$array |
||
| 2189 | * |
||
| 2190 | * @return static |
||
| 2191 | * <p>(Immutable)</p> |
||
| 2192 | * |
||
| 2193 | * @phpstan-param array<TKey,T> $array |
||
| 2194 | * @phpstan-return static<TKey,T> |
||
| 2195 | * @psalm-mutation-free |
||
| 2196 | */ |
||
| 2197 | 9 | public function diffKeyAndValue(array ...$array): self |
|
| 2225 | |||
| 2226 | /** |
||
| 2227 | * Return elements where the values are only in the current multi-dimensional array. |
||
| 2228 | * |
||
| 2229 | * EXAMPLE: <code> |
||
| 2230 | * a([1 => [1 => 1], 2 => [2 => 2]])->diffRecursive([1 => [1 => 1]]); // Arrayy[2 => [2 => 2]] |
||
| 2231 | * </code> |
||
| 2232 | * |
||
| 2233 | * @param array $array |
||
| 2234 | * @param array|\Generator|null $helperVariableForRecursion <p>(only for internal usage)</p> |
||
| 2235 | * |
||
| 2236 | * @return static |
||
| 2237 | * <p>(Immutable)</p> |
||
| 2238 | * |
||
| 2239 | * @phpstan-param array<TKey,T> $array |
||
| 2240 | * @phpstan-param null|array<TKey,T>|\Generator<TKey,T> $helperVariableForRecursion |
||
| 2241 | * @phpstan-return static<TKey,T> |
||
| 2242 | * @psalm-mutation-free |
||
| 2243 | */ |
||
| 2244 | 1 | public function diffRecursive(array $array = [], $helperVariableForRecursion = null): self |
|
| 2279 | |||
| 2280 | /** |
||
| 2281 | * Return elements where the values that are only in the new $array. |
||
| 2282 | * |
||
| 2283 | * EXAMPLE: <code> |
||
| 2284 | * a([1 => 1])->diffReverse([1 => 1, 2 => 2]); // Arrayy[2 => 2] |
||
| 2285 | * </code> |
||
| 2286 | * |
||
| 2287 | * @param array $array |
||
| 2288 | * |
||
| 2289 | * @return static |
||
| 2290 | * <p>(Immutable)</p> |
||
| 2291 | * |
||
| 2292 | * @phpstan-param array<TKey,T> $array |
||
| 2293 | * @phpstan-return static<TKey,T> |
||
| 2294 | * @psalm-mutation-free |
||
| 2295 | */ |
||
| 2296 | 8 | public function diffReverse(array $array = []): self |
|
| 2304 | |||
| 2305 | /** |
||
| 2306 | * Divide an array into two arrays. One with keys and the other with values. |
||
| 2307 | * |
||
| 2308 | * EXAMPLE: <code> |
||
| 2309 | * a(['a' => 1, 'b' => ''])->divide(); // Arrayy[Arrayy['a', 'b'], Arrayy[1, '']] |
||
| 2310 | * </code> |
||
| 2311 | * |
||
| 2312 | * @return static |
||
| 2313 | * <p>(Immutable)</p> |
||
| 2314 | * |
||
| 2315 | * @phpstan-return static<TKey,T> |
||
| 2316 | * @psalm-mutation-free |
||
| 2317 | */ |
||
| 2318 | 1 | public function divide(): self |
|
| 2329 | |||
| 2330 | /** |
||
| 2331 | * Iterate over the current array and modify the array's value. |
||
| 2332 | * |
||
| 2333 | * EXAMPLE: <code> |
||
| 2334 | * $result = A::create(); |
||
| 2335 | * $closure = function ($value) { |
||
| 2336 | * return ':' . $value . ':'; |
||
| 2337 | * }; |
||
| 2338 | * a(['foo', 'bar' => 'bis'])->each($closure); // Arrayy[':foo:', 'bar' => ':bis:'] |
||
| 2339 | * </code> |
||
| 2340 | * |
||
| 2341 | * @param \Closure $closure |
||
| 2342 | * |
||
| 2343 | * @return static |
||
| 2344 | * <p>(Immutable)</p> |
||
| 2345 | * |
||
| 2346 | * @phpstan-param \Closure(T=,?TKey=):T $closure |
||
| 2347 | * @phpstan-return static<TKey,T> |
||
| 2348 | * @psalm-mutation-free |
||
| 2349 | */ |
||
| 2350 | 5 | View Code Duplication | public function each(\Closure $closure): self |
| 2365 | |||
| 2366 | /** |
||
| 2367 | * Sets the internal iterator to the last element in the array and returns this element. |
||
| 2368 | * |
||
| 2369 | * @return false|mixed |
||
| 2370 | * |
||
| 2371 | * @phpstan-return T|false |
||
| 2372 | */ |
||
| 2373 | public function end() |
||
| 2392 | |||
| 2393 | /** |
||
| 2394 | * Check if a value is in the current array using a closure. |
||
| 2395 | * |
||
| 2396 | * EXAMPLE: <code> |
||
| 2397 | * $callable = function ($value, $key) { |
||
| 2398 | * return 2 === $key and 'two' === $value; |
||
| 2399 | * }; |
||
| 2400 | * a(['foo', 2 => 'two'])->exists($callable); // true |
||
| 2401 | * </code> |
||
| 2402 | * |
||
| 2403 | * @param \Closure $closure |
||
| 2404 | * |
||
| 2405 | * @return bool |
||
| 2406 | * <p>Returns true if the given value is found, false otherwise.</p> |
||
| 2407 | * |
||
| 2408 | * @phpstan-param \Closure(T=,TKey=):bool $closure |
||
| 2409 | */ |
||
| 2410 | 4 | public function exists(\Closure $closure): bool |
|
| 2425 | |||
| 2426 | /** |
||
| 2427 | * Fill the array until "$num" with "$default" values. |
||
| 2428 | * |
||
| 2429 | * EXAMPLE: <code> |
||
| 2430 | * a(['bar'])->fillWithDefaults(3, 'foo'); // Arrayy['bar', 'foo', 'foo'] |
||
| 2431 | * </code> |
||
| 2432 | * |
||
| 2433 | * @param int $num |
||
| 2434 | * @param mixed $default |
||
| 2435 | * |
||
| 2436 | * @return static |
||
| 2437 | * <p>(Immutable)</p> |
||
| 2438 | * |
||
| 2439 | * @phpstan-param T $default |
||
| 2440 | * @phpstan-return static<TKey,T> |
||
| 2441 | * @psalm-mutation-free |
||
| 2442 | */ |
||
| 2443 | 8 | public function fillWithDefaults(int $num, $default = null): self |
|
| 2466 | |||
| 2467 | /** |
||
| 2468 | * Find all items in an array that pass the truth test. |
||
| 2469 | * |
||
| 2470 | * EXAMPLE: <code> |
||
| 2471 | * $closure = function ($value) { |
||
| 2472 | * return $value % 2 !== 0; |
||
| 2473 | * } |
||
| 2474 | * a([1, 2, 3, 4])->filter($closure); // Arrayy[0 => 1, 2 => 3] |
||
| 2475 | * </code> |
||
| 2476 | * |
||
| 2477 | * @param \Closure|null $closure [optional] <p> |
||
| 2478 | * The callback function to use |
||
| 2479 | * </p> |
||
| 2480 | * <p> |
||
| 2481 | * If no callback is supplied, all entries of |
||
| 2482 | * input equal to false (see |
||
| 2483 | * converting to |
||
| 2484 | * boolean) will be removed. |
||
| 2485 | * </p> |
||
| 2486 | * @param int $flag [optional] <p> |
||
| 2487 | * Flag determining what arguments are sent to <i>callback</i>: |
||
| 2488 | * </p> |
||
| 2489 | * <ul> |
||
| 2490 | * <li> |
||
| 2491 | * <b>ARRAY_FILTER_USE_KEY</b> (1) - pass key as the only argument |
||
| 2492 | * to <i>callback</i> instead of the value |
||
| 2493 | * </li> |
||
| 2494 | * <li> |
||
| 2495 | * <b>ARRAY_FILTER_USE_BOTH</b> (2) - pass both value and key as |
||
| 2496 | * arguments to <i>callback</i> instead of the value |
||
| 2497 | * </li> |
||
| 2498 | * </ul> |
||
| 2499 | * |
||
| 2500 | * @return static |
||
| 2501 | * <p>(Immutable)</p> |
||
| 2502 | * |
||
| 2503 | * @phpstan-param null|(\Closure(T=,TKey=):bool)|(\Closure(T=):bool)|(\Closure(TKey=):bool) $closure |
||
| 2504 | * @phpstan-return static<TKey,T> |
||
| 2505 | * @psalm-mutation-free |
||
| 2506 | */ |
||
| 2507 | 13 | public function filter($closure = null, int $flag = \ARRAY_FILTER_USE_BOTH) |
|
| 2549 | |||
| 2550 | /** |
||
| 2551 | * Filters an array of objects (or a numeric array of associative arrays) based on the value of a particular |
||
| 2552 | * property within that. |
||
| 2553 | * |
||
| 2554 | * @param string $property |
||
| 2555 | * @param mixed $value |
||
| 2556 | * @param string|null $comparisonOp |
||
| 2557 | * <p> |
||
| 2558 | * 'eq' (equals),<br /> |
||
| 2559 | * 'gt' (greater),<br /> |
||
| 2560 | * 'gte' || 'ge' (greater or equals),<br /> |
||
| 2561 | * 'lt' (less),<br /> |
||
| 2562 | * 'lte' || 'le' (less or equals),<br /> |
||
| 2563 | * 'ne' (not equals),<br /> |
||
| 2564 | * 'contains',<br /> |
||
| 2565 | * 'notContains',<br /> |
||
| 2566 | * 'newer' (via strtotime),<br /> |
||
| 2567 | * 'older' (via strtotime),<br /> |
||
| 2568 | * </p> |
||
| 2569 | * |
||
| 2570 | * @return static |
||
| 2571 | * <p>(Immutable)</p> |
||
| 2572 | * |
||
| 2573 | * @phpstan-param array|T $value |
||
| 2574 | * @phpstan-return static<TKey,T> |
||
| 2575 | * @psalm-mutation-free |
||
| 2576 | * |
||
| 2577 | * @psalm-suppress MissingClosureReturnType |
||
| 2578 | * @psalm-suppress MissingClosureParamType |
||
| 2579 | */ |
||
| 2580 | 1 | public function filterBy( |
|
| 2652 | |||
| 2653 | /** |
||
| 2654 | * Find the first item in an array that passes the truth test, otherwise return false. |
||
| 2655 | * |
||
| 2656 | * EXAMPLE: <code> |
||
| 2657 | * $search = 'foo'; |
||
| 2658 | * $closure = function ($value, $key) use ($search) { |
||
| 2659 | * return $value === $search; |
||
| 2660 | * }; |
||
| 2661 | * a(['foo', 'bar', 'lall'])->find($closure); // 'foo' |
||
| 2662 | * </code> |
||
| 2663 | * |
||
| 2664 | * @param \Closure $closure |
||
| 2665 | * |
||
| 2666 | * @return false|mixed |
||
| 2667 | * <p>Return false if we did not find the value.</p> |
||
| 2668 | * |
||
| 2669 | * @phpstan-param \Closure(T=,TKey=):bool $closure |
||
| 2670 | * @phpstan-return T|false |
||
| 2671 | */ |
||
| 2672 | 8 | View Code Duplication | public function find(\Closure $closure) |
| 2682 | |||
| 2683 | /** |
||
| 2684 | * find by ... |
||
| 2685 | * |
||
| 2686 | * EXAMPLE: <code> |
||
| 2687 | * $array = [ |
||
| 2688 | * 0 => ['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01'], |
||
| 2689 | * 1 => ['id' => 456, 'name' => 'bar', 'group' => 'primary', 'value' => 1468, 'when' => '2014-07-15'], |
||
| 2690 | * ]; |
||
| 2691 | * a($array)->filterBy('name', 'foo'); // Arrayy[0 => ['id' => 123, 'name' => 'foo', 'group' => 'primary', 'value' => 123456, 'when' => '2014-01-01']] |
||
| 2692 | * </code> |
||
| 2693 | * |
||
| 2694 | * @param string $property |
||
| 2695 | * @param mixed $value |
||
| 2696 | * @param string $comparisonOp |
||
| 2697 | * |
||
| 2698 | * @return static |
||
| 2699 | * <p>(Immutable)</p> |
||
| 2700 | * |
||
| 2701 | * @phpstan-param array|T $value |
||
| 2702 | * @phpstan-return static<TKey,T> |
||
| 2703 | * @psalm-mutation-free |
||
| 2704 | */ |
||
| 2705 | 1 | public function findBy(string $property, $value, string $comparisonOp = 'eq'): self |
|
| 2709 | |||
| 2710 | /** |
||
| 2711 | * Get the first value from the current array. |
||
| 2712 | * |
||
| 2713 | * EXAMPLE: <code> |
||
| 2714 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->first(); // 'foo' |
||
| 2715 | * </code> |
||
| 2716 | * |
||
| 2717 | * @return mixed|null |
||
| 2718 | * <p>Return null if there wasn't a element.</p> |
||
| 2719 | * |
||
| 2720 | * @phpstan-return T|null |
||
| 2721 | * @psalm-mutation-free |
||
| 2722 | */ |
||
| 2723 | 22 | public function first() |
|
| 2732 | |||
| 2733 | /** |
||
| 2734 | * Get the first key from the current array. |
||
| 2735 | * |
||
| 2736 | * @return mixed|null |
||
| 2737 | * <p>Return null if there wasn't a element.</p> |
||
| 2738 | * |
||
| 2739 | * @phpstan-return TKey|null |
||
| 2740 | * |
||
| 2741 | * @psalm-mutation-free |
||
| 2742 | */ |
||
| 2743 | 29 | public function firstKey() |
|
| 2752 | |||
| 2753 | /** |
||
| 2754 | * Get the first value(s) from the current array. |
||
| 2755 | * And will return an empty array if there was no first entry. |
||
| 2756 | * |
||
| 2757 | * EXAMPLE: <code> |
||
| 2758 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->firstsImmutable(2); // Arrayy[0 => 'foo', 1 => 'bar'] |
||
| 2759 | * </code> |
||
| 2760 | * |
||
| 2761 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2762 | * |
||
| 2763 | * @return static |
||
| 2764 | * <p>(Immutable)</p> |
||
| 2765 | * |
||
| 2766 | * @phpstan-return static<TKey,T> |
||
| 2767 | * @psalm-mutation-free |
||
| 2768 | */ |
||
| 2769 | 37 | public function firstsImmutable(int $number = null): self |
|
| 2785 | |||
| 2786 | /** |
||
| 2787 | * Get the first value(s) from the current array. |
||
| 2788 | * And will return an empty array if there was no first entry. |
||
| 2789 | * |
||
| 2790 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2791 | * |
||
| 2792 | * @return static |
||
| 2793 | * <p>(Immutable)</p> |
||
| 2794 | * |
||
| 2795 | * @phpstan-return static<array-key,TKey> |
||
| 2796 | * @psalm-mutation-free |
||
| 2797 | */ |
||
| 2798 | 3 | View Code Duplication | public function firstsKeys(int $number = null): self |
| 2814 | |||
| 2815 | /** |
||
| 2816 | * Get and remove the first value(s) from the current array. |
||
| 2817 | * And will return an empty array if there was no first entry. |
||
| 2818 | * |
||
| 2819 | * EXAMPLE: <code> |
||
| 2820 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->firstsMutable(); // 'foo' |
||
| 2821 | * </code> |
||
| 2822 | * |
||
| 2823 | * @param int|null $number <p>How many values you will take?</p> |
||
| 2824 | * |
||
| 2825 | * @return $this |
||
| 2826 | * <p>(Mutable)</p> |
||
| 2827 | * |
||
| 2828 | * @phpstan-return static<TKey,T> |
||
| 2829 | */ |
||
| 2830 | 34 | public function firstsMutable(int $number = null): self |
|
| 2842 | |||
| 2843 | /** |
||
| 2844 | * Exchanges all keys with their associated values in an array. |
||
| 2845 | * |
||
| 2846 | * EXAMPLE: <code> |
||
| 2847 | * a([0 => 'foo', 1 => 'bar'])->flip(); // Arrayy['foo' => 0, 'bar' => 1] |
||
| 2848 | * </code> |
||
| 2849 | * |
||
| 2850 | * @return static |
||
| 2851 | * <p>(Immutable)</p> |
||
| 2852 | * |
||
| 2853 | * @phpstan-return static<array-key,TKey> |
||
| 2854 | * @psalm-mutation-free |
||
| 2855 | */ |
||
| 2856 | 1 | public function flip(): self |
|
| 2870 | |||
| 2871 | /** |
||
| 2872 | * Get a value from an array (optional using dot-notation). |
||
| 2873 | * |
||
| 2874 | * EXAMPLE: <code> |
||
| 2875 | * $arrayy = a(['user' => ['lastname' => 'Moelleken']]); |
||
| 2876 | * $arrayy->get('user.lastname'); // 'Moelleken' |
||
| 2877 | * // --- |
||
| 2878 | * $arrayy = new A(); |
||
| 2879 | * $arrayy['user'] = ['lastname' => 'Moelleken']; |
||
| 2880 | * $arrayy['user.firstname'] = 'Lars'; |
||
| 2881 | * $arrayy['user']['lastname']; // Moelleken |
||
| 2882 | * $arrayy['user.lastname']; // Moelleken |
||
| 2883 | * $arrayy['user.firstname']; // Lars |
||
| 2884 | * </code> |
||
| 2885 | * |
||
| 2886 | * @param int|string $key |
||
| 2887 | * <p>The key to look for.</p> |
||
| 2888 | * @param mixed $fallback |
||
| 2889 | * <p>Value to fallback to.</p> |
||
| 2890 | * @param array|null $array |
||
| 2891 | * <p>The array to get from, if it's set to "null" we use the current array from the |
||
| 2892 | * class.</p> |
||
| 2893 | * @param bool $useByReference |
||
| 2894 | * |
||
| 2895 | * @return mixed|static |
||
| 2896 | * |
||
| 2897 | * @phpstan-param array-key $key |
||
| 2898 | * @phpstan-param array<array-key,mixed>|array<TKey,T> $array |
||
| 2899 | * @psalm-mutation-free |
||
| 2900 | */ |
||
| 2901 | 249 | public function get( |
|
| 3074 | |||
| 3075 | /** |
||
| 3076 | * alias: for "Arrayy->toArray()" |
||
| 3077 | * |
||
| 3078 | * @return array |
||
| 3079 | * |
||
| 3080 | * @see Arrayy::getArray() |
||
| 3081 | * |
||
| 3082 | * @phpstan-return array<TKey,T> |
||
| 3083 | */ |
||
| 3084 | 15 | public function getAll(): array |
|
| 3088 | |||
| 3089 | /** |
||
| 3090 | * Get the current array from the "Arrayy"-object. |
||
| 3091 | * |
||
| 3092 | * alias for "toArray()" |
||
| 3093 | * |
||
| 3094 | * @param bool $convertAllArrayyElements <p> |
||
| 3095 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 3096 | * </p> |
||
| 3097 | * @param bool $preserveKeys <p> |
||
| 3098 | * e.g.: A generator maybe return the same key more then once, |
||
| 3099 | * so maybe you will ignore the keys. |
||
| 3100 | * </p> |
||
| 3101 | * |
||
| 3102 | * @return array |
||
| 3103 | * |
||
| 3104 | * @phpstan-return array<TKey,T> |
||
| 3105 | * @psalm-mutation-free |
||
| 3106 | * |
||
| 3107 | * @see Arrayy::toArray() |
||
| 3108 | */ |
||
| 3109 | 514 | public function getArray( |
|
| 3110 | bool $convertAllArrayyElements = false, |
||
| 3111 | bool $preserveKeys = true |
||
| 3112 | ): array { |
||
| 3113 | 514 | return $this->toArray( |
|
| 3114 | 514 | $convertAllArrayyElements, |
|
| 3115 | $preserveKeys |
||
| 3116 | ); |
||
| 3117 | } |
||
| 3118 | |||
| 3119 | /** |
||
| 3120 | * @param string $json |
||
| 3121 | * |
||
| 3122 | * @return $this |
||
| 3123 | */ |
||
| 3124 | 3 | public static function createFromJsonMapper(string $json) |
|
| 3140 | |||
| 3141 | /** |
||
| 3142 | * @return array<array-key,TypeCheckInterface>|TypeCheckArray<array-key,TypeCheckInterface> |
||
| 3143 | * |
||
| 3144 | * @internal |
||
| 3145 | */ |
||
| 3146 | 6 | public function getPhpDocPropertiesFromClass() |
|
| 3154 | |||
| 3155 | /** |
||
| 3156 | * Get the current array from the "Arrayy"-object as list. |
||
| 3157 | * |
||
| 3158 | * alias for "toList()" |
||
| 3159 | * |
||
| 3160 | * @param bool $convertAllArrayyElements <p> |
||
| 3161 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 3162 | * </p> |
||
| 3163 | * |
||
| 3164 | * @return array |
||
| 3165 | * |
||
| 3166 | * @phpstan-return list<T> |
||
| 3167 | * @psalm-mutation-free |
||
| 3168 | * |
||
| 3169 | * @see Arrayy::toList() |
||
| 3170 | */ |
||
| 3171 | 1 | public function getList(bool $convertAllArrayyElements = false): array |
|
| 3175 | |||
| 3176 | /** |
||
| 3177 | * Returns the values from a single column of the input array, identified by |
||
| 3178 | * the $columnKey, can be used to extract data-columns from multi-arrays. |
||
| 3179 | * |
||
| 3180 | * EXAMPLE: <code> |
||
| 3181 | * a([['foo' => 'bar', 'id' => 1], ['foo => 'lall', 'id' => 2]])->getColumn('foo', 'id'); // Arrayy[1 => 'bar', 2 => 'lall'] |
||
| 3182 | * </code> |
||
| 3183 | * |
||
| 3184 | * INFO: Optionally, you may provide an $indexKey to index the values in the returned |
||
| 3185 | * array by the values from the $indexKey column in the input array. |
||
| 3186 | * |
||
| 3187 | * @param int|string|null $columnKey |
||
| 3188 | * @param int|string|null $indexKey |
||
| 3189 | * |
||
| 3190 | * @return static |
||
| 3191 | * <p>(Immutable)</p> |
||
| 3192 | * |
||
| 3193 | * @phpstan-return static<TKey,T> |
||
| 3194 | * @psalm-mutation-free |
||
| 3195 | */ |
||
| 3196 | 1 | public function getColumn($columnKey = null, $indexKey = null): self |
|
| 3256 | |||
| 3257 | /** |
||
| 3258 | * Get the current array from the "Arrayy"-object as generator by reference. |
||
| 3259 | * |
||
| 3260 | * @return \Generator |
||
| 3261 | * |
||
| 3262 | * @phpstan-return \Generator<mixed,T>|\Generator<TKey,T> |
||
| 3263 | */ |
||
| 3264 | 75 | public function &getGeneratorByReference(): \Generator |
|
| 3280 | |||
| 3281 | /** |
||
| 3282 | * Get the current array from the "Arrayy"-object as generator. |
||
| 3283 | * |
||
| 3284 | * @return \Generator |
||
| 3285 | * |
||
| 3286 | * @phpstan-return \Generator<mixed,T>|\Generator<TKey,T> |
||
| 3287 | * @psalm-mutation-free |
||
| 3288 | */ |
||
| 3289 | 1075 | public function getGenerator(): \Generator |
|
| 3299 | |||
| 3300 | /** |
||
| 3301 | * alias: for "Arrayy->keys()" |
||
| 3302 | * |
||
| 3303 | * @return static |
||
| 3304 | * <p>(Immutable)</p> |
||
| 3305 | * |
||
| 3306 | * @see Arrayy::keys() |
||
| 3307 | * |
||
| 3308 | * @phpstan-return static<int,TKey> |
||
| 3309 | * @psalm-mutation-free |
||
| 3310 | */ |
||
| 3311 | 2 | public function getKeys() |
|
| 3315 | |||
| 3316 | /** |
||
| 3317 | * Get the current array from the "Arrayy"-object as object. |
||
| 3318 | * |
||
| 3319 | * @return \stdClass |
||
| 3320 | */ |
||
| 3321 | 4 | public function getObject(): \stdClass |
|
| 3325 | |||
| 3326 | /** |
||
| 3327 | * alias: for "Arrayy->randomImmutable()" |
||
| 3328 | * |
||
| 3329 | * @return static |
||
| 3330 | * <p>(Immutable)</p> |
||
| 3331 | * |
||
| 3332 | * @see Arrayy::randomImmutable() |
||
| 3333 | * |
||
| 3334 | * @phpstan-return static<array-key,T> |
||
| 3335 | */ |
||
| 3336 | 4 | public function getRandom(): self |
|
| 3340 | |||
| 3341 | /** |
||
| 3342 | * alias: for "Arrayy->randomKey()" |
||
| 3343 | * |
||
| 3344 | * @return mixed|null |
||
| 3345 | * <p>Get a key/index or null if there wasn't a key/index.</p> |
||
| 3346 | * |
||
| 3347 | * @phpstan-return null|TKey |
||
| 3348 | * |
||
| 3349 | * @see Arrayy::randomKey() |
||
| 3350 | */ |
||
| 3351 | 3 | public function getRandomKey() |
|
| 3355 | |||
| 3356 | /** |
||
| 3357 | * alias: for "Arrayy->randomKeys()" |
||
| 3358 | * |
||
| 3359 | * @param int $number |
||
| 3360 | * |
||
| 3361 | * @return static |
||
| 3362 | * <p>(Immutable)</p> |
||
| 3363 | * |
||
| 3364 | * @see Arrayy::randomKeys() |
||
| 3365 | * |
||
| 3366 | * @phpstan-return static<TKey,T> |
||
| 3367 | */ |
||
| 3368 | 8 | public function getRandomKeys(int $number): self |
|
| 3372 | |||
| 3373 | /** |
||
| 3374 | * alias: for "Arrayy->randomValue()" |
||
| 3375 | * |
||
| 3376 | * @return mixed|null |
||
| 3377 | * <p>Get a random value or null if there wasn't a value.</p> |
||
| 3378 | * |
||
| 3379 | * @phpstan-return null|T |
||
| 3380 | * |
||
| 3381 | * @see Arrayy::randomValue() |
||
| 3382 | */ |
||
| 3383 | 3 | public function getRandomValue() |
|
| 3387 | |||
| 3388 | /** |
||
| 3389 | * alias: for "Arrayy->randomValues()" |
||
| 3390 | * |
||
| 3391 | * @param int $number |
||
| 3392 | * |
||
| 3393 | * @return static |
||
| 3394 | * <p>(Immutable)</p> |
||
| 3395 | * |
||
| 3396 | * @see Arrayy::randomValues() |
||
| 3397 | * |
||
| 3398 | * @phpstan-return static<TKey,T> |
||
| 3399 | */ |
||
| 3400 | 6 | public function getRandomValues(int $number): self |
|
| 3404 | |||
| 3405 | /** |
||
| 3406 | * Gets all values. |
||
| 3407 | * |
||
| 3408 | * @return static |
||
| 3409 | * <p>The values of all elements in this array, in the order they |
||
| 3410 | * appear in the array.</p> |
||
| 3411 | * |
||
| 3412 | * @phpstan-return static<TKey,T> |
||
| 3413 | */ |
||
| 3414 | 4 | public function getValues() |
|
| 3424 | |||
| 3425 | /** |
||
| 3426 | * Gets all values via Generator. |
||
| 3427 | * |
||
| 3428 | * @return \Generator |
||
| 3429 | * <p>The values of all elements in this array, in the order they |
||
| 3430 | * appear in the array as Generator.</p> |
||
| 3431 | * |
||
| 3432 | * @phpstan-return \Generator<TKey,T> |
||
| 3433 | */ |
||
| 3434 | 4 | public function getValuesYield(): \Generator |
|
| 3438 | |||
| 3439 | /** |
||
| 3440 | * Group values from a array according to the results of a closure. |
||
| 3441 | * |
||
| 3442 | * @param callable|string $grouper <p>A callable function name.</p> |
||
| 3443 | * @param bool $saveKeys |
||
| 3444 | * |
||
| 3445 | * @return static |
||
| 3446 | * <p>(Immutable)</p> |
||
| 3447 | * |
||
| 3448 | * @phpstan-return static<TKey,T> |
||
| 3449 | * @psalm-mutation-free |
||
| 3450 | */ |
||
| 3451 | 4 | public function group($grouper, bool $saveKeys = false): self |
|
| 3492 | |||
| 3493 | /** |
||
| 3494 | * Check if an array has a given key. |
||
| 3495 | * |
||
| 3496 | * @param mixed $key |
||
| 3497 | * |
||
| 3498 | * @return bool |
||
| 3499 | * |
||
| 3500 | * @phpstan-param null|TKey|TKey[] $key |
||
| 3501 | */ |
||
| 3502 | 30 | public function has($key): bool |
|
| 3528 | |||
| 3529 | /** |
||
| 3530 | * Check if an array has a given value. |
||
| 3531 | * |
||
| 3532 | * INFO: If you need to search recursive please use ```contains($value, true)```. |
||
| 3533 | * |
||
| 3534 | * @param mixed $value |
||
| 3535 | * |
||
| 3536 | * @return bool |
||
| 3537 | * |
||
| 3538 | * @phpstan-param T $value |
||
| 3539 | */ |
||
| 3540 | 1 | public function hasValue($value): bool |
|
| 3544 | |||
| 3545 | /** |
||
| 3546 | * Implodes the values of this array. |
||
| 3547 | * |
||
| 3548 | * EXAMPLE: <code> |
||
| 3549 | * a([0 => -9, 1, 2])->implode('|'); // '-9|1|2' |
||
| 3550 | * </code> |
||
| 3551 | * |
||
| 3552 | * @param string $glue |
||
| 3553 | * @param string $prefix |
||
| 3554 | * |
||
| 3555 | * @return string |
||
| 3556 | * @psalm-mutation-free |
||
| 3557 | */ |
||
| 3558 | 29 | public function implode(string $glue = '', string $prefix = ''): string |
|
| 3562 | |||
| 3563 | /** |
||
| 3564 | * Implodes the keys of this array. |
||
| 3565 | * |
||
| 3566 | * @param string $glue |
||
| 3567 | * |
||
| 3568 | * @return string |
||
| 3569 | * @psalm-mutation-free |
||
| 3570 | */ |
||
| 3571 | 8 | public function implodeKeys(string $glue = ''): string |
|
| 3575 | |||
| 3576 | /** |
||
| 3577 | * Given a list and an iterate-function that returns |
||
| 3578 | * a key for each element in the list (or a property name), |
||
| 3579 | * returns an object with an index of each item. |
||
| 3580 | * |
||
| 3581 | * @param int|string $key |
||
| 3582 | * |
||
| 3583 | * @return static |
||
| 3584 | * <p>(Immutable)</p> |
||
| 3585 | * |
||
| 3586 | * @phpstan-param array-key $key |
||
| 3587 | * @phpstan-return static<TKey,T> |
||
| 3588 | * @psalm-mutation-free |
||
| 3589 | */ |
||
| 3590 | 4 | View Code Duplication | public function indexBy($key): self |
| 3607 | |||
| 3608 | /** |
||
| 3609 | * alias: for "Arrayy->searchIndex()" |
||
| 3610 | * |
||
| 3611 | * @param mixed $value |
||
| 3612 | * <p>The value to search for.</p> |
||
| 3613 | * |
||
| 3614 | * @return false|int|string |
||
| 3615 | * |
||
| 3616 | * @phpstan-param T $value |
||
| 3617 | * @phpstan-return false|TKey |
||
| 3618 | * |
||
| 3619 | * @see Arrayy::searchIndex() |
||
| 3620 | */ |
||
| 3621 | 4 | public function indexOf($value) |
|
| 3625 | |||
| 3626 | /** |
||
| 3627 | * Get everything but the last..$to items. |
||
| 3628 | * |
||
| 3629 | * EXAMPLE: <code> |
||
| 3630 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->initial(2); // Arrayy[0 => 'foo'] |
||
| 3631 | * </code> |
||
| 3632 | * |
||
| 3633 | * @param int $to |
||
| 3634 | * |
||
| 3635 | * @return static |
||
| 3636 | * <p>(Immutable)</p> |
||
| 3637 | * |
||
| 3638 | * @phpstan-return static<TKey,T> |
||
| 3639 | * @psalm-mutation-free |
||
| 3640 | */ |
||
| 3641 | 12 | public function initial(int $to = 1): self |
|
| 3645 | |||
| 3646 | /** |
||
| 3647 | * Return an array with all elements found in input array. |
||
| 3648 | * |
||
| 3649 | * EXAMPLE: <code> |
||
| 3650 | * a(['foo', 'bar'])->intersection(['bar', 'baz']); // Arrayy['bar'] |
||
| 3651 | * </code> |
||
| 3652 | * |
||
| 3653 | * @param array $search |
||
| 3654 | * @param bool $keepKeys |
||
| 3655 | * |
||
| 3656 | * @return static |
||
| 3657 | * <p>(Immutable)</p> |
||
| 3658 | * |
||
| 3659 | * @phpstan-param array<TKey,T> $search |
||
| 3660 | * @phpstan-return static<TKey,T> |
||
| 3661 | * @psalm-mutation-free |
||
| 3662 | */ |
||
| 3663 | 4 | public function intersection(array $search, bool $keepKeys = false): self |
|
| 3689 | |||
| 3690 | /** |
||
| 3691 | * Return an array with all elements found in input array. |
||
| 3692 | * |
||
| 3693 | * @param array ...$array |
||
| 3694 | * |
||
| 3695 | * @return static |
||
| 3696 | * <p>(Immutable)</p> |
||
| 3697 | * |
||
| 3698 | * @phpstan-param array<array<TKey,T>> ...$array |
||
| 3699 | * @phpstan-return static<TKey,T> |
||
| 3700 | * @psalm-mutation-free |
||
| 3701 | */ |
||
| 3702 | 1 | public function intersectionMulti(...$array): self |
|
| 3710 | |||
| 3711 | /** |
||
| 3712 | * Return a boolean flag which indicates whether the two input arrays have any common elements. |
||
| 3713 | * |
||
| 3714 | * EXAMPLE: <code> |
||
| 3715 | * a(['foo', 'bar'])->intersects(['föö', 'bär']); // false |
||
| 3716 | * </code> |
||
| 3717 | * |
||
| 3718 | * @param array $search |
||
| 3719 | * |
||
| 3720 | * @return bool |
||
| 3721 | * |
||
| 3722 | * @phpstan-param array<TKey,T> $search |
||
| 3723 | */ |
||
| 3724 | 1 | public function intersects(array $search): bool |
|
| 3728 | |||
| 3729 | /** |
||
| 3730 | * Invoke a function on all of an array's values. |
||
| 3731 | * |
||
| 3732 | * @param callable $callable |
||
| 3733 | * @param mixed $arguments |
||
| 3734 | * |
||
| 3735 | * @return static |
||
| 3736 | * <p>(Immutable)</p> |
||
| 3737 | * |
||
| 3738 | * @phpstan-param callable(T=,mixed):mixed $callable |
||
| 3739 | * @phpstan-return static<TKey,T> |
||
| 3740 | * @psalm-mutation-free |
||
| 3741 | */ |
||
| 3742 | 1 | public function invoke($callable, $arguments = []): self |
|
| 3766 | |||
| 3767 | /** |
||
| 3768 | * Check whether array is associative or not. |
||
| 3769 | * |
||
| 3770 | * EXAMPLE: <code> |
||
| 3771 | * a(['foo' => 'bar', 2, 3])->isAssoc(); // true |
||
| 3772 | * </code> |
||
| 3773 | * |
||
| 3774 | * @param bool $recursive |
||
| 3775 | * |
||
| 3776 | * @return bool |
||
| 3777 | * <p>Returns true if associative, false otherwise.</p> |
||
| 3778 | */ |
||
| 3779 | 15 | View Code Duplication | public function isAssoc(bool $recursive = false): bool |
| 3794 | |||
| 3795 | /** |
||
| 3796 | * Check if a given key or keys are empty. |
||
| 3797 | * |
||
| 3798 | * @param int|int[]|string|string[]|null $keys |
||
| 3799 | * |
||
| 3800 | * @return bool |
||
| 3801 | * <p>Returns true if empty, false otherwise.</p> |
||
| 3802 | * @psalm-mutation-free |
||
| 3803 | */ |
||
| 3804 | 45 | public function isEmpty($keys = null): bool |
|
| 3822 | |||
| 3823 | /** |
||
| 3824 | * Check if the current array is equal to the given "$array" or not. |
||
| 3825 | * |
||
| 3826 | * EXAMPLE: <code> |
||
| 3827 | * a(['💩'])->isEqual(['💩']); // true |
||
| 3828 | * </code> |
||
| 3829 | * |
||
| 3830 | * @param array $array |
||
| 3831 | * |
||
| 3832 | * @return bool |
||
| 3833 | * |
||
| 3834 | * @phpstan-param array<array-key,mixed> $array |
||
| 3835 | */ |
||
| 3836 | 1 | public function isEqual(array $array): bool |
|
| 3840 | |||
| 3841 | /** |
||
| 3842 | * Check if the current array is a multi-array. |
||
| 3843 | * |
||
| 3844 | * EXAMPLE: <code> |
||
| 3845 | * a(['foo' => [1, 2 , 3]])->isMultiArray(); // true |
||
| 3846 | * </code> |
||
| 3847 | * |
||
| 3848 | * @return bool |
||
| 3849 | */ |
||
| 3850 | 22 | public function isMultiArray(): bool |
|
| 3860 | |||
| 3861 | /** |
||
| 3862 | * Check whether array is numeric or not. |
||
| 3863 | * |
||
| 3864 | * @return bool |
||
| 3865 | * <p>Returns true if numeric, false otherwise.</p> |
||
| 3866 | */ |
||
| 3867 | 5 | View Code Duplication | public function isNumeric(): bool |
| 3882 | |||
| 3883 | /** |
||
| 3884 | * Check if the current array is sequential [0, 1, 2, 3, 4, 5 ...] or not. |
||
| 3885 | * |
||
| 3886 | * EXAMPLE: <code> |
||
| 3887 | * a([0 => 'foo', 1 => 'lall', 2 => 'foobar'])->isSequential(); // true |
||
| 3888 | * </code> |
||
| 3889 | * |
||
| 3890 | * INFO: If the array is empty we count it as non-sequential. |
||
| 3891 | * |
||
| 3892 | * @param bool $recursive |
||
| 3893 | * |
||
| 3894 | * @return bool |
||
| 3895 | * @psalm-mutation-free |
||
| 3896 | */ |
||
| 3897 | 10 | public function isSequential(bool $recursive = false): bool |
|
| 3924 | |||
| 3925 | /** |
||
| 3926 | * @return array |
||
| 3927 | * |
||
| 3928 | * @phpstan-return array<TKey,T> |
||
| 3929 | */ |
||
| 3930 | 2 | public function jsonSerialize(): array |
|
| 3934 | |||
| 3935 | /** |
||
| 3936 | * Gets the key/index of the element at the current internal iterator position. |
||
| 3937 | * |
||
| 3938 | * @return int|string|null |
||
| 3939 | * @phpstan-return array-key|null |
||
| 3940 | */ |
||
| 3941 | public function key() |
||
| 3949 | |||
| 3950 | /** |
||
| 3951 | * Checks if the given key exists in the provided array. |
||
| 3952 | * |
||
| 3953 | * INFO: This method only use "array_key_exists()" if you want to use "dot"-notation, |
||
| 3954 | * then you need to use "Arrayy->offsetExists()". |
||
| 3955 | * |
||
| 3956 | * @param int|string $key the key to look for |
||
| 3957 | * |
||
| 3958 | * @return bool |
||
| 3959 | * @psalm-mutation-free |
||
| 3960 | */ |
||
| 3961 | 174 | public function keyExists($key): bool |
|
| 3971 | |||
| 3972 | /** |
||
| 3973 | * Get all keys from the current array. |
||
| 3974 | * |
||
| 3975 | * EXAMPLE: <code> |
||
| 3976 | * a([1 => 'foo', 2 => 'foo2', 3 => 'bar'])->keys(); // Arrayy[1, 2, 3] |
||
| 3977 | * </code> |
||
| 3978 | * |
||
| 3979 | * @param bool $recursive |
||
| 3980 | * [optional] <p> |
||
| 3981 | * Get all keys, also from all sub-arrays from an multi-dimensional array. |
||
| 3982 | * </p> |
||
| 3983 | * @param mixed|null $search_values |
||
| 3984 | * [optional] <p> |
||
| 3985 | * If specified, then only keys containing these values are returned. |
||
| 3986 | * </p> |
||
| 3987 | * @param bool $strict |
||
| 3988 | * [optional] <p> |
||
| 3989 | * Determines if strict comparison (===) should be used during the search. |
||
| 3990 | * </p> |
||
| 3991 | * |
||
| 3992 | * @return static |
||
| 3993 | * <p>(Immutable) An array of all the keys in input.</p> |
||
| 3994 | * |
||
| 3995 | * @phpstan-param null|T|T[] $search_values |
||
| 3996 | * @phpstan-return static<int,TKey> |
||
| 3997 | * |
||
| 3998 | * @psalm-mutation-free |
||
| 3999 | */ |
||
| 4000 | 29 | public function keys( |
|
| 4001 | bool $recursive = false, |
||
| 4002 | $search_values = null, |
||
| 4003 | bool $strict = true |
||
| 4004 | ): self { |
||
| 4005 | |||
| 4006 | // recursive |
||
| 4007 | |||
| 4008 | 29 | if ($recursive === true) { |
|
| 4009 | 4 | $array = $this->array_keys_recursive( |
|
| 4010 | 4 | null, |
|
| 4011 | $search_values, |
||
| 4012 | $strict |
||
| 4013 | ); |
||
| 4014 | |||
| 4015 | 4 | return static::create( |
|
| 4016 | 4 | $array, |
|
| 4017 | 4 | $this->iteratorClass, |
|
| 4018 | 4 | false |
|
| 4019 | ); |
||
| 4020 | } |
||
| 4021 | |||
| 4022 | // non recursive |
||
| 4023 | |||
| 4024 | 28 | if ($search_values === null) { |
|
| 4025 | $arrayFunction = function (): \Generator { |
||
| 4026 | 28 | foreach ($this->getGenerator() as $key => $value) { |
|
| 4027 | 26 | yield $key; |
|
| 4028 | } |
||
| 4029 | 28 | }; |
|
| 4030 | } else { |
||
| 4031 | $arrayFunction = function () use ($search_values, $strict): \Generator { |
||
| 4032 | 1 | $is_array_tmp = \is_array($search_values); |
|
| 4033 | |||
| 4034 | /** @noinspection PhpParameterByRefIsNotUsedAsReferenceInspection */ |
||
| 4035 | 1 | foreach ($this->getGeneratorByReference() as $key => &$value) { |
|
| 4036 | View Code Duplication | if ( |
|
| 4037 | ( |
||
| 4038 | 1 | $is_array_tmp === false |
|
| 4039 | && |
||
| 4040 | 1 | $strict === true |
|
| 4041 | && |
||
| 4042 | 1 | $search_values === $value |
|
| 4043 | ) |
||
| 4044 | || |
||
| 4045 | ( |
||
| 4046 | 1 | $is_array_tmp === false |
|
| 4047 | && |
||
| 4048 | 1 | $strict === false |
|
| 4049 | && |
||
| 4050 | 1 | $search_values == $value |
|
| 4051 | ) |
||
| 4052 | || |
||
| 4053 | ( |
||
| 4054 | 1 | $is_array_tmp === true |
|
| 4055 | && |
||
| 4056 | 1 | \in_array($value, $search_values, $strict) |
|
| 4057 | ) |
||
| 4058 | ) { |
||
| 4059 | 1 | yield $key; |
|
| 4060 | } |
||
| 4061 | } |
||
| 4062 | 1 | }; |
|
| 4063 | } |
||
| 4064 | |||
| 4065 | 28 | return static::create( |
|
| 4066 | 28 | $arrayFunction, |
|
| 4067 | 28 | $this->iteratorClass, |
|
| 4068 | 28 | false |
|
| 4069 | ); |
||
| 4070 | } |
||
| 4071 | |||
| 4072 | /** |
||
| 4073 | * Sort an array by key in reverse order. |
||
| 4074 | * |
||
| 4075 | * @param int $sort_flags [optional] <p> |
||
| 4076 | * You may modify the behavior of the sort using the optional |
||
| 4077 | * parameter sort_flags, for details |
||
| 4078 | * see sort. |
||
| 4079 | * </p> |
||
| 4080 | * |
||
| 4081 | * @return $this |
||
| 4082 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 4083 | * |
||
| 4084 | * @phpstan-return static<TKey,T> |
||
| 4085 | */ |
||
| 4086 | 4 | public function krsort(int $sort_flags = 0): self |
|
| 4094 | |||
| 4095 | /** |
||
| 4096 | * Sort an array by key in reverse order. |
||
| 4097 | * |
||
| 4098 | * @param int $sort_flags [optional] <p> |
||
| 4099 | * You may modify the behavior of the sort using the optional |
||
| 4100 | * parameter sort_flags, for details |
||
| 4101 | * see sort. |
||
| 4102 | * </p> |
||
| 4103 | * |
||
| 4104 | * @return $this |
||
| 4105 | * <p>(Immutable)</p> |
||
| 4106 | * |
||
| 4107 | * @phpstan-return static<TKey,T> |
||
| 4108 | * @psalm-mutation-free |
||
| 4109 | */ |
||
| 4110 | 4 | public function krsortImmutable(int $sort_flags = 0): self |
|
| 4121 | |||
| 4122 | /** |
||
| 4123 | * Get the last value from the current array. |
||
| 4124 | * |
||
| 4125 | * EXAMPLE: <code> |
||
| 4126 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->last(); // 'lall' |
||
| 4127 | * </code> |
||
| 4128 | * |
||
| 4129 | * @return mixed|null |
||
| 4130 | * <p>Return null if there wasn't a element.</p> |
||
| 4131 | * |
||
| 4132 | * @phpstan-return T|null |
||
| 4133 | * @psalm-mutation-free |
||
| 4134 | */ |
||
| 4135 | 17 | public function last() |
|
| 4144 | |||
| 4145 | /** |
||
| 4146 | * Get the last key from the current array. |
||
| 4147 | * |
||
| 4148 | * @return mixed|null |
||
| 4149 | * <p>Return null if there wasn't a element.</p> |
||
| 4150 | * @psalm-mutation-free |
||
| 4151 | */ |
||
| 4152 | 21 | public function lastKey() |
|
| 4158 | |||
| 4159 | /** |
||
| 4160 | * Get the last value(s) from the current array. |
||
| 4161 | * |
||
| 4162 | * EXAMPLE: <code> |
||
| 4163 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->lasts(2); // Arrayy[0 => 'bar', 1 => 'lall'] |
||
| 4164 | * </code> |
||
| 4165 | * |
||
| 4166 | * @param int|null $number |
||
| 4167 | * |
||
| 4168 | * @return static |
||
| 4169 | * <p>(Immutable)</p> |
||
| 4170 | * |
||
| 4171 | * @phpstan-return static<TKey,T> |
||
| 4172 | * @psalm-mutation-free |
||
| 4173 | */ |
||
| 4174 | 13 | public function lastsImmutable(int $number = null): self |
|
| 4204 | |||
| 4205 | /** |
||
| 4206 | * Get the last value(s) from the current array. |
||
| 4207 | * |
||
| 4208 | * EXAMPLE: <code> |
||
| 4209 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->lasts(2); // Arrayy[0 => 'bar', 1 => 'lall'] |
||
| 4210 | * </code> |
||
| 4211 | * |
||
| 4212 | * @param int|null $number |
||
| 4213 | * |
||
| 4214 | * @return $this |
||
| 4215 | * <p>(Mutable)</p> |
||
| 4216 | * |
||
| 4217 | * @phpstan-return static<TKey,T> |
||
| 4218 | */ |
||
| 4219 | 13 | public function lastsMutable(int $number = null): self |
|
| 4230 | |||
| 4231 | /** |
||
| 4232 | * Count the values from the current array. |
||
| 4233 | * |
||
| 4234 | * alias: for "Arrayy->count()" |
||
| 4235 | * |
||
| 4236 | * @param int $mode |
||
| 4237 | * |
||
| 4238 | * @return int |
||
| 4239 | * |
||
| 4240 | * @see Arrayy::count() |
||
| 4241 | */ |
||
| 4242 | 20 | public function length(int $mode = \COUNT_NORMAL): int |
|
| 4246 | |||
| 4247 | /** |
||
| 4248 | * Apply the given function to the every element of the array, |
||
| 4249 | * collecting the results. |
||
| 4250 | * |
||
| 4251 | * EXAMPLE: <code> |
||
| 4252 | * a(['foo', 'Foo'])->map('mb_strtoupper'); // Arrayy['FOO', 'FOO'] |
||
| 4253 | * </code> |
||
| 4254 | * |
||
| 4255 | * @param callable $callable |
||
| 4256 | * @param bool $useKeyAsSecondParameter |
||
| 4257 | * @param mixed ...$arguments |
||
| 4258 | * |
||
| 4259 | * @return static |
||
| 4260 | * <p>(Immutable) Arrayy object with modified elements.</p> |
||
| 4261 | * |
||
| 4262 | * @template T2 |
||
| 4263 | * <p>The output value type.</p> |
||
| 4264 | * |
||
| 4265 | * @phpstan-param callable(T,TKey=,mixed=):T2 $callable |
||
| 4266 | * @phpstan-return static<TKey,T2> |
||
| 4267 | * @psalm-mutation-free |
||
| 4268 | */ |
||
| 4269 | 7 | public function map( |
|
| 4302 | |||
| 4303 | /** |
||
| 4304 | * Check if all items in current array match a truth test. |
||
| 4305 | * |
||
| 4306 | * EXAMPLE: <code> |
||
| 4307 | * $closure = function ($value, $key) { |
||
| 4308 | * return ($value % 2 === 0); |
||
| 4309 | * }; |
||
| 4310 | * a([2, 4, 8])->matches($closure); // true |
||
| 4311 | * </code> |
||
| 4312 | * |
||
| 4313 | * @param \Closure $closure |
||
| 4314 | * |
||
| 4315 | * @return bool |
||
| 4316 | * |
||
| 4317 | * @phpstan-param \Closure(T=,TKey=):bool $closure |
||
| 4318 | */ |
||
| 4319 | 15 | View Code Duplication | public function matches(\Closure $closure): bool |
| 4335 | |||
| 4336 | /** |
||
| 4337 | * Check if any item in the current array matches a truth test. |
||
| 4338 | * |
||
| 4339 | * EXAMPLE: <code> |
||
| 4340 | * $closure = function ($value, $key) { |
||
| 4341 | * return ($value % 2 === 0); |
||
| 4342 | * }; |
||
| 4343 | * a([1, 4, 7])->matches($closure); // true |
||
| 4344 | * </code> |
||
| 4345 | * |
||
| 4346 | * @param \Closure $closure |
||
| 4347 | * |
||
| 4348 | * @return bool |
||
| 4349 | * |
||
| 4350 | * @phpstan-param \Closure(T=,TKey=):bool $closure |
||
| 4351 | */ |
||
| 4352 | 14 | View Code Duplication | public function matchesAny(\Closure $closure): bool |
| 4368 | |||
| 4369 | /** |
||
| 4370 | * Get the max value from an array. |
||
| 4371 | * |
||
| 4372 | * EXAMPLE: <code> |
||
| 4373 | * a([-9, -8, -7, 1.32])->max(); // 1.32 |
||
| 4374 | * </code> |
||
| 4375 | * |
||
| 4376 | * @return false|float|int|string |
||
| 4377 | * <p>Will return false if there are no values.</p> |
||
| 4378 | */ |
||
| 4379 | 10 | View Code Duplication | public function max() |
| 4399 | |||
| 4400 | /** |
||
| 4401 | * Merge the new $array into the current array. |
||
| 4402 | * |
||
| 4403 | * - keep key,value from the current array, also if the index is in the new $array |
||
| 4404 | * |
||
| 4405 | * EXAMPLE: <code> |
||
| 4406 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4407 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4408 | * a($array1)->mergeAppendKeepIndex($array2); // Arrayy[1 => 'one', 'foo' => 'bar2', 3 => 'three'] |
||
| 4409 | * // --- |
||
| 4410 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4411 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4412 | * a($array1)->mergeAppendKeepIndex($array2); // Arrayy[0 => 'foo', 1 => 'bar2'] |
||
| 4413 | * </code> |
||
| 4414 | * |
||
| 4415 | * @param array $array |
||
| 4416 | * @param bool $recursive |
||
| 4417 | * |
||
| 4418 | * @return static |
||
| 4419 | * <p>(Immutable)</p> |
||
| 4420 | * |
||
| 4421 | * @phpstan-param array<int|TKey,T> $array |
||
| 4422 | * @phpstan-return static<int|TKey,T> |
||
| 4423 | * @psalm-mutation-free |
||
| 4424 | */ |
||
| 4425 | 33 | View Code Duplication | public function mergeAppendKeepIndex(array $array = [], bool $recursive = false): self |
| 4440 | |||
| 4441 | /** |
||
| 4442 | * Merge the new $array into the current array. |
||
| 4443 | * |
||
| 4444 | * - replace duplicate assoc-keys from the current array with the key,values from the new $array |
||
| 4445 | * - create new indexes |
||
| 4446 | * |
||
| 4447 | * EXAMPLE: <code> |
||
| 4448 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4449 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4450 | * a($array1)->mergeAppendNewIndex($array2); // Arrayy[0 => 'one', 'foo' => 'bar2', 1 => 'three'] |
||
| 4451 | * // --- |
||
| 4452 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4453 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4454 | * a($array1)->mergeAppendNewIndex($array2); // Arrayy[0 => 'one', 1 => 'foo', 2 => 'foo', 3 => 'bar2'] |
||
| 4455 | * </code> |
||
| 4456 | * |
||
| 4457 | * @param array $array |
||
| 4458 | * @param bool $recursive |
||
| 4459 | * |
||
| 4460 | * @return static |
||
| 4461 | * <p>(Immutable)</p> |
||
| 4462 | * |
||
| 4463 | * @phpstan-param array<TKey,T> $array |
||
| 4464 | * @phpstan-return static<int,T> |
||
| 4465 | * @psalm-mutation-free |
||
| 4466 | */ |
||
| 4467 | 20 | View Code Duplication | public function mergeAppendNewIndex(array $array = [], bool $recursive = false): self |
| 4482 | |||
| 4483 | /** |
||
| 4484 | * Merge the the current array into the $array. |
||
| 4485 | * |
||
| 4486 | * - use key,value from the new $array, also if the index is in the current array |
||
| 4487 | * |
||
| 4488 | * EXAMPLE: <code> |
||
| 4489 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4490 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4491 | * a($array1)->mergePrependKeepIndex($array2); // Arrayy['foo' => 'bar1', 3 => 'three', 1 => 'one'] |
||
| 4492 | * // --- |
||
| 4493 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4494 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4495 | * a($array1)->mergePrependKeepIndex($array2); // Arrayy[0 => 'one', 1 => 'foo'] |
||
| 4496 | * </code> |
||
| 4497 | * |
||
| 4498 | * @param array $array |
||
| 4499 | * @param bool $recursive |
||
| 4500 | * |
||
| 4501 | * @return static |
||
| 4502 | * <p>(Immutable)</p> |
||
| 4503 | * |
||
| 4504 | * @phpstan-param array<TKey,T> $array |
||
| 4505 | * @phpstan-return static<TKey,T> |
||
| 4506 | * @psalm-mutation-free |
||
| 4507 | */ |
||
| 4508 | 17 | View Code Duplication | public function mergePrependKeepIndex(array $array = [], bool $recursive = false): self |
| 4523 | |||
| 4524 | /** |
||
| 4525 | * Merge the current array into the new $array. |
||
| 4526 | * |
||
| 4527 | * - replace duplicate assoc-keys from new $array with the key,values from the current array |
||
| 4528 | * - create new indexes |
||
| 4529 | * |
||
| 4530 | * EXAMPLE: <code> |
||
| 4531 | * $array1 = [1 => 'one', 'foo' => 'bar1']; |
||
| 4532 | * $array2 = ['foo' => 'bar2', 3 => 'three']; |
||
| 4533 | * a($array1)->mergePrependNewIndex($array2); // Arrayy['foo' => 'bar1', 0 => 'three', 1 => 'one'] |
||
| 4534 | * // --- |
||
| 4535 | * $array1 = [0 => 'one', 1 => 'foo']; |
||
| 4536 | * $array2 = [0 => 'foo', 1 => 'bar2']; |
||
| 4537 | * a($array1)->mergePrependNewIndex($array2); // Arrayy[0 => 'foo', 1 => 'bar2', 2 => 'one', 3 => 'foo'] |
||
| 4538 | * </code> |
||
| 4539 | * |
||
| 4540 | * @param array $array |
||
| 4541 | * @param bool $recursive |
||
| 4542 | * |
||
| 4543 | * @return static |
||
| 4544 | * <p>(Immutable)</p> |
||
| 4545 | * |
||
| 4546 | * @phpstan-param array<TKey,T> $array |
||
| 4547 | * @phpstan-return static<int,T> |
||
| 4548 | * @psalm-mutation-free |
||
| 4549 | */ |
||
| 4550 | 21 | View Code Duplication | public function mergePrependNewIndex(array $array = [], bool $recursive = false): self |
| 4565 | |||
| 4566 | /** |
||
| 4567 | * @return ArrayyMeta|mixed|static |
||
| 4568 | */ |
||
| 4569 | 19 | public static function meta() |
|
| 4573 | |||
| 4574 | /** |
||
| 4575 | * Get the min value from an array. |
||
| 4576 | * |
||
| 4577 | * EXAMPLE: <code> |
||
| 4578 | * a([-9, -8, -7, 1.32])->min(); // -9 |
||
| 4579 | * </code> |
||
| 4580 | * |
||
| 4581 | * @return false|mixed |
||
| 4582 | * <p>Will return false if there are no values.</p> |
||
| 4583 | */ |
||
| 4584 | 10 | View Code Duplication | public function min() |
| 4604 | |||
| 4605 | /** |
||
| 4606 | * Get the most used value from the array. |
||
| 4607 | * |
||
| 4608 | * @return mixed|null |
||
| 4609 | * <p>(Immutable) Return null if there wasn't an element.</p> |
||
| 4610 | * |
||
| 4611 | * @phpstan-return T|null |
||
| 4612 | * @psalm-mutation-free |
||
| 4613 | */ |
||
| 4614 | 3 | public function mostUsedValue() |
|
| 4619 | |||
| 4620 | /** |
||
| 4621 | * Get the most used value from the array. |
||
| 4622 | * |
||
| 4623 | * @param int|null $number <p>How many values you will take?</p> |
||
| 4624 | * |
||
| 4625 | * @return static |
||
| 4626 | * <p>(Immutable)</p> |
||
| 4627 | * |
||
| 4628 | * @phpstan-return static<array-key,T> |
||
| 4629 | * @psalm-mutation-free |
||
| 4630 | */ |
||
| 4631 | 3 | public function mostUsedValues(int $number = null): self |
|
| 4635 | |||
| 4636 | /** |
||
| 4637 | * Move an array element to a new index. |
||
| 4638 | * |
||
| 4639 | * EXAMPLE: <code> |
||
| 4640 | * $arr2 = new A(['A' => 'a', 'B' => 'b', 'C' => 'c', 'D' => 'd', 'E' => 'e']); |
||
| 4641 | * $newArr2 = $arr2->moveElement('D', 1); // Arrayy['A' => 'a', 'D' => 'd', 'B' => 'b', 'C' => 'c', 'E' => 'e'] |
||
| 4642 | * </code> |
||
| 4643 | * |
||
| 4644 | * @param int|string $from |
||
| 4645 | * @param int $to |
||
| 4646 | * |
||
| 4647 | * @return static |
||
| 4648 | * <p>(Immutable)</p> |
||
| 4649 | * |
||
| 4650 | * @phpstan-return static<TKey,T> |
||
| 4651 | * @psalm-mutation-free |
||
| 4652 | */ |
||
| 4653 | 1 | public function moveElement($from, $to): self |
|
| 4686 | |||
| 4687 | /** |
||
| 4688 | * Move an array element to the first place. |
||
| 4689 | * |
||
| 4690 | * INFO: Instead of "Arrayy->moveElement()" this method will NOT |
||
| 4691 | * loss the keys of an indexed array. |
||
| 4692 | * |
||
| 4693 | * @param int|string $key |
||
| 4694 | * |
||
| 4695 | * @return static |
||
| 4696 | * <p>(Immutable)</p> |
||
| 4697 | * |
||
| 4698 | * @phpstan-return static<TKey,T> |
||
| 4699 | * @psalm-mutation-free |
||
| 4700 | */ |
||
| 4701 | 1 | View Code Duplication | public function moveElementToFirstPlace($key): self |
| 4717 | |||
| 4718 | /** |
||
| 4719 | * Move an array element to the last place. |
||
| 4720 | * |
||
| 4721 | * INFO: Instead of "Arrayy->moveElement()" this method will NOT |
||
| 4722 | * loss the keys of an indexed array. |
||
| 4723 | * |
||
| 4724 | * @param int|string $key |
||
| 4725 | * |
||
| 4726 | * @return static |
||
| 4727 | * <p>(Immutable)</p> |
||
| 4728 | * |
||
| 4729 | * @phpstan-return static<TKey,T> |
||
| 4730 | * @psalm-mutation-free |
||
| 4731 | */ |
||
| 4732 | 1 | View Code Duplication | public function moveElementToLastPlace($key): self |
| 4748 | |||
| 4749 | /** |
||
| 4750 | * Moves the internal iterator position to the next element and returns this element. |
||
| 4751 | * |
||
| 4752 | * @return false|mixed |
||
| 4753 | * <p>(Mutable) Will return false if there are no values.</p> |
||
| 4754 | * |
||
| 4755 | * @phpstan-return false|T |
||
| 4756 | */ |
||
| 4757 | public function next() |
||
| 4767 | |||
| 4768 | /** |
||
| 4769 | * Get the next nth keys and values from the array. |
||
| 4770 | * |
||
| 4771 | * @param int $step |
||
| 4772 | * @param int $offset |
||
| 4773 | * |
||
| 4774 | * @return static |
||
| 4775 | * <p>(Immutable)</p> |
||
| 4776 | * |
||
| 4777 | * @phpstan-return static<TKey,T> |
||
| 4778 | * @psalm-mutation-free |
||
| 4779 | */ |
||
| 4780 | 1 | public function nth(int $step, int $offset = 0): self |
|
| 4799 | |||
| 4800 | /** |
||
| 4801 | * Get a subset of the items from the given array. |
||
| 4802 | * |
||
| 4803 | * @param int[]|string[] $keys |
||
| 4804 | * |
||
| 4805 | * @return static |
||
| 4806 | * <p>(Immutable)</p> |
||
| 4807 | * |
||
| 4808 | * @phpstan-param array-key[] $keys |
||
| 4809 | * @phpstan-return static<TKey,T> |
||
| 4810 | * @psalm-mutation-free |
||
| 4811 | */ |
||
| 4812 | 1 | View Code Duplication | public function only(array $keys): self |
| 4830 | |||
| 4831 | /** |
||
| 4832 | * Pad array to the specified size with a given value. |
||
| 4833 | * |
||
| 4834 | * @param int $size <p>Size of the result array.</p> |
||
| 4835 | * @param mixed $value <p>Empty value by default.</p> |
||
| 4836 | * |
||
| 4837 | * @return static |
||
| 4838 | * <p>(Immutable) Arrayy object padded to $size with $value.</p> |
||
| 4839 | * |
||
| 4840 | * @phpstan-return static<TKey,T> |
||
| 4841 | * @psalm-mutation-free |
||
| 4842 | */ |
||
| 4843 | 5 | public function pad(int $size, $value): self |
|
| 4851 | |||
| 4852 | /** |
||
| 4853 | * Partitions this array in two array according to a predicate. |
||
| 4854 | * Keys are preserved in the resulting array. |
||
| 4855 | * |
||
| 4856 | * @param \Closure $closure |
||
| 4857 | * <p>The predicate on which to partition.</p> |
||
| 4858 | * |
||
| 4859 | * @return array<int, static> |
||
| 4860 | * <p>An array with two elements. The first element contains the array |
||
| 4861 | * of elements where the predicate returned TRUE, the second element |
||
| 4862 | * contains the array of elements where the predicate returned FALSE.</p> |
||
| 4863 | * |
||
| 4864 | * @phpstan-param \Closure(T=,TKey=):bool $closure |
||
| 4865 | * @phpstan-return array<int, static<TKey,T>> |
||
| 4866 | */ |
||
| 4867 | 1 | public function partition(\Closure $closure): array |
|
| 4883 | |||
| 4884 | /** |
||
| 4885 | * Pop a specified value off the end of the current array. |
||
| 4886 | * |
||
| 4887 | * @return mixed|null |
||
| 4888 | * <p>(Mutable) The popped element from the current array or null if the array is e.g. empty.</p> |
||
| 4889 | * |
||
| 4890 | * @phpstan-return T|null |
||
| 4891 | */ |
||
| 4892 | 5 | public function pop() |
|
| 4898 | |||
| 4899 | /** |
||
| 4900 | * Prepend a (key) + value to the current array. |
||
| 4901 | * |
||
| 4902 | * EXAMPLE: <code> |
||
| 4903 | * a(['fòô' => 'bàř'])->prepend('foo'); // Arrayy[0 => 'foo', 'fòô' => 'bàř'] |
||
| 4904 | * </code> |
||
| 4905 | * |
||
| 4906 | * @param mixed $value |
||
| 4907 | * @param mixed $key |
||
| 4908 | * |
||
| 4909 | * @return $this |
||
| 4910 | * <p>(Mutable) Return this Arrayy object, with the prepended value.</p> |
||
| 4911 | * |
||
| 4912 | * @phpstan-param T $value |
||
| 4913 | * @phpstan-param TKey|null $key |
||
| 4914 | * @phpstan-return static<TKey,T> |
||
| 4915 | */ |
||
| 4916 | 11 | public function prepend($value, $key = null) |
|
| 4932 | |||
| 4933 | /** |
||
| 4934 | * Prepend a (key) + value to the current array. |
||
| 4935 | * |
||
| 4936 | * EXAMPLE: <code> |
||
| 4937 | * a(['fòô' => 'bàř'])->prependImmutable('foo')->getArray(); // [0 => 'foo', 'fòô' => 'bàř'] |
||
| 4938 | * </code> |
||
| 4939 | * |
||
| 4940 | * @param mixed $value |
||
| 4941 | * @param mixed $key |
||
| 4942 | * |
||
| 4943 | * @return $this |
||
| 4944 | * <p>(Immutable) Return this Arrayy object, with the prepended value.</p> |
||
| 4945 | * |
||
| 4946 | * @phpstan-param T $value |
||
| 4947 | * @phpstan-param TKey $key |
||
| 4948 | * @phpstan-return static<TKey,T> |
||
| 4949 | * @psalm-mutation-free |
||
| 4950 | */ |
||
| 4951 | 1 | View Code Duplication | public function prependImmutable($value, $key = null) |
| 4975 | |||
| 4976 | /** |
||
| 4977 | * Add a suffix to each key. |
||
| 4978 | * |
||
| 4979 | * @param float|int|string $suffix |
||
| 4980 | * |
||
| 4981 | * @return static |
||
| 4982 | * <p>(Immutable) Return an Arrayy object, with the prepended keys.</p> |
||
| 4983 | * |
||
| 4984 | * @phpstan-return static<TKey,T> |
||
| 4985 | * @psalm-mutation-free |
||
| 4986 | */ |
||
| 4987 | 10 | View Code Duplication | public function prependToEachKey($suffix): self |
| 5013 | |||
| 5014 | /** |
||
| 5015 | * Add a suffix to each value. |
||
| 5016 | * |
||
| 5017 | * @param float|int|string $suffix |
||
| 5018 | * |
||
| 5019 | * @return static |
||
| 5020 | * <p>(Immutable) Return an Arrayy object, with the prepended values.</p> |
||
| 5021 | * |
||
| 5022 | * @phpstan-return static<TKey,T> |
||
| 5023 | * @psalm-mutation-free |
||
| 5024 | */ |
||
| 5025 | 10 | View Code Duplication | public function prependToEachValue($suffix): self |
| 5053 | |||
| 5054 | /** |
||
| 5055 | * Return the value of a given key and |
||
| 5056 | * delete the key. |
||
| 5057 | * |
||
| 5058 | * @param int|int[]|string|string[]|null $keyOrKeys |
||
| 5059 | * @param mixed $fallback |
||
| 5060 | * |
||
| 5061 | * @return mixed |
||
| 5062 | */ |
||
| 5063 | 6 | public function pull($keyOrKeys = null, $fallback = null) |
|
| 5085 | |||
| 5086 | /** |
||
| 5087 | * Push one or more values onto the end of array at once. |
||
| 5088 | * |
||
| 5089 | * @param mixed ...$args |
||
| 5090 | * |
||
| 5091 | * @return $this |
||
| 5092 | * <p>(Mutable) Return this Arrayy object, with pushed elements to the end of array.</p> |
||
| 5093 | * |
||
| 5094 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 5095 | * |
||
| 5096 | * @phpstan-param array<TKey,T> ...$args |
||
| 5097 | * @phpstan-return static<TKey,T> |
||
| 5098 | */ |
||
| 5099 | 9 | View Code Duplication | public function push(...$args) |
| 5117 | |||
| 5118 | /** |
||
| 5119 | * Get a random value from the current array. |
||
| 5120 | * |
||
| 5121 | * EXAMPLE: <code> |
||
| 5122 | * a([1, 2, 3, 4])->randomImmutable(2); // e.g.: Arrayy[1, 4] |
||
| 5123 | * </code> |
||
| 5124 | * |
||
| 5125 | * @param int|null $number <p>How many values you will take?</p> |
||
| 5126 | * |
||
| 5127 | * @return static |
||
| 5128 | * <p>(Immutable)</p> |
||
| 5129 | * |
||
| 5130 | * @phpstan-return static<array-key,T> |
||
| 5131 | */ |
||
| 5132 | 19 | public function randomImmutable(int $number = null): self |
|
| 5163 | |||
| 5164 | /** |
||
| 5165 | * Pick a random key/index from the keys of this array. |
||
| 5166 | * |
||
| 5167 | * EXAMPLE: <code> |
||
| 5168 | * $arrayy = A::create([1 => 'one', 2 => 'two']); |
||
| 5169 | * $arrayy->randomKey(); // e.g. 2 |
||
| 5170 | * </code> |
||
| 5171 | * |
||
| 5172 | * @throws \RangeException If array is empty |
||
| 5173 | * |
||
| 5174 | * @return mixed|null |
||
| 5175 | * <p>Get a key/index or null if there wasn't a key/index.</p> |
||
| 5176 | * |
||
| 5177 | * @phpstan-return null|TKey |
||
| 5178 | */ |
||
| 5179 | 4 | public function randomKey() |
|
| 5189 | |||
| 5190 | /** |
||
| 5191 | * Pick a given number of random keys/indexes out of this array. |
||
| 5192 | * |
||
| 5193 | * EXAMPLE: <code> |
||
| 5194 | * a([1 => 'one', 2 => 'two'])->randomKeys(); // e.g. Arrayy[1, 2] |
||
| 5195 | * </code> |
||
| 5196 | * |
||
| 5197 | * @param int $number <p>The number of keys/indexes (should be <= \count($this->array))</p> |
||
| 5198 | * |
||
| 5199 | * @throws \RangeException If array is empty |
||
| 5200 | * |
||
| 5201 | * @return static |
||
| 5202 | * <p>(Immutable)</p> |
||
| 5203 | * |
||
| 5204 | * @phpstan-return static<TKey,T> |
||
| 5205 | */ |
||
| 5206 | 13 | public function randomKeys(int $number): self |
|
| 5234 | |||
| 5235 | /** |
||
| 5236 | * Get a random value from the current array. |
||
| 5237 | * |
||
| 5238 | * EXAMPLE: <code> |
||
| 5239 | * a([1, 2, 3, 4])->randomMutable(2); // e.g.: Arrayy[1, 4] |
||
| 5240 | * </code> |
||
| 5241 | * |
||
| 5242 | * @param int|null $number <p>How many values you will take?</p> |
||
| 5243 | * |
||
| 5244 | * @return $this |
||
| 5245 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5246 | * |
||
| 5247 | * @phpstan-return static<TKey,T> |
||
| 5248 | */ |
||
| 5249 | 17 | public function randomMutable(int $number = null): self |
|
| 5272 | |||
| 5273 | /** |
||
| 5274 | * Pick a random value from the values of this array. |
||
| 5275 | * |
||
| 5276 | * EXAMPLE: <code> |
||
| 5277 | * a([1 => 'one', 2 => 'two'])->randomValue(); // e.g. 'one' |
||
| 5278 | * </code> |
||
| 5279 | * |
||
| 5280 | * @return mixed |
||
| 5281 | * <p>Get a random value or null if there wasn't a value.</p> |
||
| 5282 | * |
||
| 5283 | * @phpstan-return T|null |
||
| 5284 | */ |
||
| 5285 | 4 | public function randomValue() |
|
| 5295 | |||
| 5296 | /** |
||
| 5297 | * Pick a given number of random values out of this array. |
||
| 5298 | * |
||
| 5299 | * EXAMPLE: <code> |
||
| 5300 | * a([1 => 'one', 2 => 'two'])->randomValues(); // e.g. Arrayy['one', 'two'] |
||
| 5301 | * </code> |
||
| 5302 | * |
||
| 5303 | * @param int $number |
||
| 5304 | * |
||
| 5305 | * @return static |
||
| 5306 | * <p>(Mutable)</p> |
||
| 5307 | * |
||
| 5308 | * @phpstan-return static<TKey,T> |
||
| 5309 | */ |
||
| 5310 | 7 | public function randomValues(int $number): self |
|
| 5314 | |||
| 5315 | /** |
||
| 5316 | * Get a random value from an array, with the ability to skew the results. |
||
| 5317 | * |
||
| 5318 | * EXAMPLE: <code> |
||
| 5319 | * a([0 => 3, 1 => 4])->randomWeighted([1 => 4]); // e.g.: Arrayy[4] (has a 66% chance of returning 4) |
||
| 5320 | * </code> |
||
| 5321 | * |
||
| 5322 | * @param array $array |
||
| 5323 | * @param int|null $number <p>How many values you will take?</p> |
||
| 5324 | * |
||
| 5325 | * @return static<int,mixed> |
||
| 5326 | * <p>(Immutable)</p> |
||
| 5327 | * |
||
| 5328 | * @phpstan-param array<T,int> $array |
||
| 5329 | * @phpstan-return static<array-key,T> |
||
| 5330 | */ |
||
| 5331 | 9 | public function randomWeighted(array $array, int $number = null): self |
|
| 5346 | |||
| 5347 | /** |
||
| 5348 | * Reduce the current array via callable e.g. anonymous-function and return the end result. |
||
| 5349 | * |
||
| 5350 | * EXAMPLE: <code> |
||
| 5351 | * a([1, 2, 3, 4])->reduce( |
||
| 5352 | * function ($carry, $item) { |
||
| 5353 | * return $carry * $item; |
||
| 5354 | * }, |
||
| 5355 | * 1 |
||
| 5356 | * ); // Arrayy[24] |
||
| 5357 | * </code> |
||
| 5358 | * |
||
| 5359 | * @param callable $callable |
||
| 5360 | * @param mixed $initial |
||
| 5361 | * |
||
| 5362 | * @return static |
||
| 5363 | * <p>(Immutable)</p> |
||
| 5364 | * |
||
| 5365 | * @template T2 |
||
| 5366 | * <p>The output value type.</p> |
||
| 5367 | * |
||
| 5368 | * @phpstan-param callable(T2, T, TKey): T2 $callable |
||
| 5369 | * @phpstan-param T2 $initial |
||
| 5370 | * |
||
| 5371 | * @phpstan-return static<TKey,T2> |
||
| 5372 | * @psalm-mutation-free |
||
| 5373 | */ |
||
| 5374 | 18 | View Code Duplication | public function reduce($callable, $initial = []): self |
| 5389 | |||
| 5390 | /** |
||
| 5391 | * @param bool $unique |
||
| 5392 | * |
||
| 5393 | * @return static |
||
| 5394 | * <p>(Immutable)</p> |
||
| 5395 | * |
||
| 5396 | * @phpstan-return static<int,mixed> |
||
| 5397 | * @psalm-mutation-free |
||
| 5398 | */ |
||
| 5399 | 14 | public function reduce_dimension(bool $unique = true): self |
|
| 5422 | |||
| 5423 | /** |
||
| 5424 | * Create a numerically re-indexed Arrayy object. |
||
| 5425 | * |
||
| 5426 | * EXAMPLE: <code> |
||
| 5427 | * a([2 => 1, 3 => 2])->reindex(); // Arrayy[0 => 1, 1 => 2] |
||
| 5428 | * </code> |
||
| 5429 | * |
||
| 5430 | * @return $this |
||
| 5431 | * <p>(Mutable) Return this Arrayy object, with re-indexed array-elements.</p> |
||
| 5432 | * |
||
| 5433 | * @phpstan-return static<TKey,T> |
||
| 5434 | */ |
||
| 5435 | 9 | public function reindex(): self |
|
| 5443 | |||
| 5444 | /** |
||
| 5445 | * Return all items that fail the truth test. |
||
| 5446 | * |
||
| 5447 | * EXAMPLE: <code> |
||
| 5448 | * $closure = function ($value) { |
||
| 5449 | * return $value % 2 !== 0; |
||
| 5450 | * } |
||
| 5451 | * a([1, 2, 3, 4])->reject($closure); // Arrayy[1 => 2, 3 => 4] |
||
| 5452 | * </code> |
||
| 5453 | * |
||
| 5454 | * @param \Closure $closure |
||
| 5455 | * |
||
| 5456 | * @return static |
||
| 5457 | * <p>(Immutable)</p> |
||
| 5458 | * |
||
| 5459 | * @phpstan-param \Closure(T=,TKey=):bool $closure |
||
| 5460 | * @phpstan-return static<TKey,T> |
||
| 5461 | * @psalm-mutation-free |
||
| 5462 | */ |
||
| 5463 | 1 | View Code Duplication | public function reject(\Closure $closure): self |
| 5480 | |||
| 5481 | /** |
||
| 5482 | * Remove a value from the current array (optional using dot-notation). |
||
| 5483 | * |
||
| 5484 | * EXAMPLE: <code> |
||
| 5485 | * a([1 => 'bar', 'foo' => 'foo'])->remove(1); // Arrayy['foo' => 'foo'] |
||
| 5486 | * </code> |
||
| 5487 | * |
||
| 5488 | * @param mixed $key |
||
| 5489 | * |
||
| 5490 | * @return static |
||
| 5491 | * <p>(Mutable)</p> |
||
| 5492 | * |
||
| 5493 | * @phpstan-param TKey|TKey[] $key |
||
| 5494 | * @phpstan-return static<TKey,T> |
||
| 5495 | */ |
||
| 5496 | 22 | public function remove($key) |
|
| 5519 | |||
| 5520 | /** |
||
| 5521 | * alias: for "Arrayy->removeValue()" |
||
| 5522 | * |
||
| 5523 | * @param mixed $element |
||
| 5524 | * |
||
| 5525 | * @return static |
||
| 5526 | * <p>(Immutable)</p> |
||
| 5527 | * |
||
| 5528 | * @phpstan-param T $element |
||
| 5529 | * @phpstan-return static<TKey,T> |
||
| 5530 | * @psalm-mutation-free |
||
| 5531 | */ |
||
| 5532 | 8 | public function removeElement($element) |
|
| 5536 | |||
| 5537 | /** |
||
| 5538 | * Remove the first value from the current array. |
||
| 5539 | * |
||
| 5540 | * EXAMPLE: <code> |
||
| 5541 | * a([1 => 'bar', 'foo' => 'foo'])->removeFirst(); // Arrayy['foo' => 'foo'] |
||
| 5542 | * </code> |
||
| 5543 | * |
||
| 5544 | * @return static |
||
| 5545 | * <p>(Immutable)</p> |
||
| 5546 | * |
||
| 5547 | * @phpstan-return static<TKey,T> |
||
| 5548 | * @psalm-mutation-free |
||
| 5549 | */ |
||
| 5550 | 7 | View Code Duplication | public function removeFirst(): self |
| 5562 | |||
| 5563 | /** |
||
| 5564 | * Remove the last value from the current array. |
||
| 5565 | * |
||
| 5566 | * EXAMPLE: <code> |
||
| 5567 | * a([1 => 'bar', 'foo' => 'foo'])->removeLast(); // Arrayy[1 => 'bar'] |
||
| 5568 | * </code> |
||
| 5569 | * |
||
| 5570 | * @return static |
||
| 5571 | * <p>(Immutable)</p> |
||
| 5572 | * |
||
| 5573 | * @phpstan-return static<TKey,T> |
||
| 5574 | * @psalm-mutation-free |
||
| 5575 | */ |
||
| 5576 | 7 | View Code Duplication | public function removeLast(): self |
| 5588 | |||
| 5589 | /** |
||
| 5590 | * Removes a particular value from an array (numeric or associative). |
||
| 5591 | * |
||
| 5592 | * EXAMPLE: <code> |
||
| 5593 | * a([1 => 'bar', 'foo' => 'foo'])->removeValue('foo'); // Arrayy[1 => 'bar'] |
||
| 5594 | * </code> |
||
| 5595 | * |
||
| 5596 | * @param mixed $value |
||
| 5597 | * |
||
| 5598 | * @return static |
||
| 5599 | * <p>(Immutable)</p> |
||
| 5600 | * |
||
| 5601 | * @phpstan-param T $value |
||
| 5602 | * @phpstan-return static<TKey,T> |
||
| 5603 | * @psalm-mutation-free |
||
| 5604 | */ |
||
| 5605 | 8 | public function removeValue($value): self |
|
| 5629 | |||
| 5630 | /** |
||
| 5631 | * Generate array of repeated arrays. |
||
| 5632 | * |
||
| 5633 | * @param int $times <p>How many times has to be repeated.</p> |
||
| 5634 | * |
||
| 5635 | * @return static |
||
| 5636 | * <p>(Immutable)</p> |
||
| 5637 | * |
||
| 5638 | * @phpstan-return static<TKey,T> |
||
| 5639 | * @psalm-mutation-free |
||
| 5640 | */ |
||
| 5641 | 1 | public function repeat($times): self |
|
| 5653 | |||
| 5654 | /** |
||
| 5655 | * Replace a key with a new key/value pair. |
||
| 5656 | * |
||
| 5657 | * EXAMPLE: <code> |
||
| 5658 | * $arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']); |
||
| 5659 | * $arrayy->replace(2, 'notfoo', 'notbar'); // Arrayy[1 => 'foo', 'notfoo' => 'notbar', 3 => 'bar'] |
||
| 5660 | * </code> |
||
| 5661 | * |
||
| 5662 | * @param mixed $oldKey |
||
| 5663 | * @param mixed $newKey |
||
| 5664 | * @param mixed $newValue |
||
| 5665 | * |
||
| 5666 | * @return static |
||
| 5667 | * <p>(Immutable)</p> |
||
| 5668 | * |
||
| 5669 | * @phpstan-return static<TKey,T> |
||
| 5670 | * @psalm-mutation-free |
||
| 5671 | */ |
||
| 5672 | 5 | public function replace($oldKey, $newKey, $newValue): self |
|
| 5682 | |||
| 5683 | /** |
||
| 5684 | * Create an array using the current array as values and the other array as keys. |
||
| 5685 | * |
||
| 5686 | * EXAMPLE: <code> |
||
| 5687 | * $firstArray = [ |
||
| 5688 | * 1 => 'one', |
||
| 5689 | * 2 => 'two', |
||
| 5690 | * 3 => 'three', |
||
| 5691 | * ]; |
||
| 5692 | * $secondArray = [ |
||
| 5693 | * 'one' => 1, |
||
| 5694 | * 1 => 'one', |
||
| 5695 | * 2 => 2, |
||
| 5696 | * ]; |
||
| 5697 | * $arrayy = a($firstArray); |
||
| 5698 | * $arrayy->replaceAllKeys($secondArray); // Arrayy[1 => "one", 'one' => "two", 2 => "three"] |
||
| 5699 | * </code> |
||
| 5700 | * |
||
| 5701 | * @param int[]|string[] $keys <p>An array of keys.</p> |
||
| 5702 | * |
||
| 5703 | * @return static |
||
| 5704 | * <p>(Immutable) Arrayy object with keys from the other array, empty Arrayy object if the number of elements |
||
| 5705 | * for each array isn't equal or if the arrays are empty. |
||
| 5706 | * </p> |
||
| 5707 | * |
||
| 5708 | * @phpstan-param array<array-key,TKey> $keys |
||
| 5709 | * @phpstan-return static<TKey,T> |
||
| 5710 | * @psalm-mutation-free |
||
| 5711 | */ |
||
| 5712 | 2 | View Code Duplication | public function replaceAllKeys(array $keys): self |
| 5725 | |||
| 5726 | /** |
||
| 5727 | * Create an array using the current array as keys and the other array as values. |
||
| 5728 | * |
||
| 5729 | * EXAMPLE: <code> |
||
| 5730 | * $firstArray = [ |
||
| 5731 | * 1 => 'one', |
||
| 5732 | * 2 => 'two', |
||
| 5733 | * 3 => 'three', |
||
| 5734 | * ]; |
||
| 5735 | * $secondArray = [ |
||
| 5736 | * 'one' => 1, |
||
| 5737 | * 1 => 'one', |
||
| 5738 | * 2 => 2, |
||
| 5739 | * ]; |
||
| 5740 | * $arrayy = a($firstArray); |
||
| 5741 | * $arrayy->replaceAllValues($secondArray); // Arrayy['one' => 1, 'two' => 'one', 'three' => 2] |
||
| 5742 | * </code> |
||
| 5743 | * |
||
| 5744 | * @param array $array <p>An array of values.</p> |
||
| 5745 | * |
||
| 5746 | * @return static |
||
| 5747 | * <p>(Immutable) Arrayy object with values from the other array, empty Arrayy object if the number of elements |
||
| 5748 | * for each array isn't equal or if the arrays are empty. |
||
| 5749 | * </p> |
||
| 5750 | * |
||
| 5751 | * @phpstan-param array<array-key,T> $array |
||
| 5752 | * @phpstan-return static<TKey,T> |
||
| 5753 | * @psalm-mutation-free |
||
| 5754 | */ |
||
| 5755 | 2 | View Code Duplication | public function replaceAllValues(array $array): self |
| 5768 | |||
| 5769 | /** |
||
| 5770 | * Replace the keys in an array with another set. |
||
| 5771 | * |
||
| 5772 | * EXAMPLE: <code> |
||
| 5773 | * a([1 => 'bar', 'foo' => 'foo'])->replaceKeys([1 => 2, 'foo' => 'replaced']); // Arrayy[2 => 'bar', 'replaced' => 'foo'] |
||
| 5774 | * </code> |
||
| 5775 | * |
||
| 5776 | * @param array $keys <p>An array of keys matching the array's size.</p> |
||
| 5777 | * |
||
| 5778 | * @return static |
||
| 5779 | * <p>(Immutable)</p> |
||
| 5780 | * |
||
| 5781 | * @phpstan-param array<array-key,TKey> $keys |
||
| 5782 | * @phpstan-return static<TKey,T> |
||
| 5783 | * @psalm-mutation-free |
||
| 5784 | */ |
||
| 5785 | 1 | View Code Duplication | public function replaceKeys(array $keys): self |
| 5799 | |||
| 5800 | /** |
||
| 5801 | * Replace the first matched value in an array. |
||
| 5802 | * |
||
| 5803 | * EXAMPLE: <code> |
||
| 5804 | * $testArray = ['bar', 'foo' => 'foo', 'foobar' => 'foobar']; |
||
| 5805 | * a($testArray)->replaceOneValue('foo', 'replaced'); // Arrayy['bar', 'foo' => 'replaced', 'foobar' => 'foobar'] |
||
| 5806 | * </code> |
||
| 5807 | * |
||
| 5808 | * @param mixed $search <p>The value to replace.</p> |
||
| 5809 | * @param mixed $replacement <p>The value to replace.</p> |
||
| 5810 | * |
||
| 5811 | * @return static |
||
| 5812 | * <p>(Immutable)</p> |
||
| 5813 | * |
||
| 5814 | * @phpstan-return static<TKey,T> |
||
| 5815 | * @psalm-mutation-free |
||
| 5816 | */ |
||
| 5817 | 3 | public function replaceOneValue($search, $replacement = ''): self |
|
| 5832 | |||
| 5833 | /** |
||
| 5834 | * Replace values in the current array. |
||
| 5835 | * |
||
| 5836 | * EXAMPLE: <code> |
||
| 5837 | * $testArray = ['bar', 'foo' => 'foo', 'foobar' => 'foobar']; |
||
| 5838 | * a($testArray)->replaceValues('foo', 'replaced'); // Arrayy['bar', 'foo' => 'replaced', 'foobar' => 'replacedbar'] |
||
| 5839 | * </code> |
||
| 5840 | * |
||
| 5841 | * @param string $search <p>The value to replace.</p> |
||
| 5842 | * @param string $replacement <p>What to replace it with.</p> |
||
| 5843 | * |
||
| 5844 | * @return static |
||
| 5845 | * <p>(Immutable)</p> |
||
| 5846 | * |
||
| 5847 | * @phpstan-return static<TKey,T> |
||
| 5848 | * @psalm-mutation-free |
||
| 5849 | */ |
||
| 5850 | 1 | public function replaceValues($search, $replacement = ''): self |
|
| 5859 | |||
| 5860 | /** |
||
| 5861 | * Get the last elements from index $from until the end of this array. |
||
| 5862 | * |
||
| 5863 | * EXAMPLE: <code> |
||
| 5864 | * a([2 => 'foo', 3 => 'bar', 4 => 'lall'])->rest(2); // Arrayy[0 => 'lall'] |
||
| 5865 | * </code> |
||
| 5866 | * |
||
| 5867 | * @param int $from |
||
| 5868 | * |
||
| 5869 | * @return static |
||
| 5870 | * <p>(Immutable)</p> |
||
| 5871 | * |
||
| 5872 | * @phpstan-return static<TKey,T> |
||
| 5873 | * @psalm-mutation-free |
||
| 5874 | */ |
||
| 5875 | 15 | View Code Duplication | public function rest(int $from = 1): self |
| 5885 | |||
| 5886 | /** |
||
| 5887 | * Return the array in the reverse order. |
||
| 5888 | * |
||
| 5889 | * EXAMPLE: <code> |
||
| 5890 | * a([1, 2, 3])->reverse(); // self[3, 2, 1] |
||
| 5891 | * </code> |
||
| 5892 | * |
||
| 5893 | * @return $this |
||
| 5894 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5895 | * |
||
| 5896 | * @phpstan-return static<TKey,T> |
||
| 5897 | */ |
||
| 5898 | 9 | public function reverse(): self |
|
| 5906 | |||
| 5907 | /** |
||
| 5908 | * Sort an array in reverse order. |
||
| 5909 | * |
||
| 5910 | * @param int $sort_flags [optional] <p> |
||
| 5911 | * You may modify the behavior of the sort using the optional |
||
| 5912 | * parameter sort_flags, for details |
||
| 5913 | * see sort. |
||
| 5914 | * </p> |
||
| 5915 | * |
||
| 5916 | * @return $this |
||
| 5917 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 5918 | * |
||
| 5919 | * @phpstan-return static<TKey,T> |
||
| 5920 | */ |
||
| 5921 | 4 | public function rsort(int $sort_flags = 0): self |
|
| 5929 | |||
| 5930 | /** |
||
| 5931 | * Sort an array in reverse order. |
||
| 5932 | * |
||
| 5933 | * @param int $sort_flags [optional] <p> |
||
| 5934 | * You may modify the behavior of the sort using the optional |
||
| 5935 | * parameter sort_flags, for details |
||
| 5936 | * see sort. |
||
| 5937 | * </p> |
||
| 5938 | * |
||
| 5939 | * @return $this |
||
| 5940 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 5941 | * |
||
| 5942 | * @phpstan-return static<TKey,T> |
||
| 5943 | * @psalm-mutation-free |
||
| 5944 | */ |
||
| 5945 | 4 | public function rsortImmutable(int $sort_flags = 0): self |
|
| 5956 | |||
| 5957 | /** |
||
| 5958 | * Search for the first index of the current array via $value. |
||
| 5959 | * |
||
| 5960 | * EXAMPLE: <code> |
||
| 5961 | * a(['fòô' => 'bàř', 'lall' => 'bàř'])->searchIndex('bàř'); // Arrayy[0 => 'fòô'] |
||
| 5962 | * </code> |
||
| 5963 | * |
||
| 5964 | * @param mixed $value |
||
| 5965 | * |
||
| 5966 | * @return false|int|string |
||
| 5967 | * <p>Will return <b>FALSE</b> if the value can't be found.</p> |
||
| 5968 | * |
||
| 5969 | * @phpstan-param T $value |
||
| 5970 | * @phpstan-return false|TKey |
||
| 5971 | * |
||
| 5972 | * @psalm-mutation-free |
||
| 5973 | */ |
||
| 5974 | 21 | public function searchIndex($value) |
|
| 5984 | |||
| 5985 | /** |
||
| 5986 | * Search for the value of the current array via $index. |
||
| 5987 | * |
||
| 5988 | * EXAMPLE: <code> |
||
| 5989 | * a(['fòô' => 'bàř'])->searchValue('fòô'); // Arrayy[0 => 'bàř'] |
||
| 5990 | * </code> |
||
| 5991 | * |
||
| 5992 | * @param mixed $index |
||
| 5993 | * |
||
| 5994 | * @return static |
||
| 5995 | * <p>(Immutable) Will return a empty Arrayy if the value wasn't found.</p> |
||
| 5996 | * |
||
| 5997 | * @phpstan-return static<TKey,T> |
||
| 5998 | * @psalm-mutation-free |
||
| 5999 | */ |
||
| 6000 | 9 | public function searchValue($index): self |
|
| 6030 | |||
| 6031 | /** |
||
| 6032 | * Set a value for the current array (optional using dot-notation). |
||
| 6033 | * |
||
| 6034 | * EXAMPLE: <code> |
||
| 6035 | * $arrayy = a(['Lars' => ['lastname' => 'Moelleken']]); |
||
| 6036 | * $arrayy->set('Lars.lastname', 'Müller'); // Arrayy['Lars', ['lastname' => 'Müller']]] |
||
| 6037 | * </code> |
||
| 6038 | * |
||
| 6039 | * @param string $key <p>The key to set.</p> |
||
| 6040 | * @param mixed $value <p>Its value.</p> |
||
| 6041 | * |
||
| 6042 | * @return $this |
||
| 6043 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 6044 | * |
||
| 6045 | * @phpstan-param TKey $key |
||
| 6046 | * @phpstan-param T $value |
||
| 6047 | * @phpstan-return static<TKey,T> |
||
| 6048 | */ |
||
| 6049 | 28 | public function set($key, $value): self |
|
| 6055 | |||
| 6056 | /** |
||
| 6057 | * Get a value from a array and set it if it was not. |
||
| 6058 | * |
||
| 6059 | * WARNING: this method only set the value, if the $key is not already set |
||
| 6060 | * |
||
| 6061 | * EXAMPLE: <code> |
||
| 6062 | * $arrayy = a([1 => 1, 2 => 2, 3 => 3]); |
||
| 6063 | * $arrayy->setAndGet(1, 4); // 1 |
||
| 6064 | * $arrayy->setAndGet(0, 4); // 4 |
||
| 6065 | * </code> |
||
| 6066 | * |
||
| 6067 | * @param mixed $key <p>The key</p> |
||
| 6068 | * @param mixed $fallback <p>The default value to set if it isn't.</p> |
||
| 6069 | * |
||
| 6070 | * @return mixed |
||
| 6071 | * <p>(Mutable)</p> |
||
| 6072 | */ |
||
| 6073 | 11 | public function setAndGet($key, $fallback = null) |
|
| 6084 | |||
| 6085 | /** |
||
| 6086 | * Shifts a specified value off the beginning of array. |
||
| 6087 | * |
||
| 6088 | * @return mixed |
||
| 6089 | * <p>(Mutable) A shifted element from the current array.</p> |
||
| 6090 | */ |
||
| 6091 | 5 | public function shift() |
|
| 6097 | |||
| 6098 | /** |
||
| 6099 | * Shuffle the current array. |
||
| 6100 | * |
||
| 6101 | * EXAMPLE: <code> |
||
| 6102 | * a([1 => 'bar', 'foo' => 'foo'])->shuffle(); // e.g.: Arrayy[['foo' => 'foo', 1 => 'bar']] |
||
| 6103 | * </code> |
||
| 6104 | * |
||
| 6105 | * @param bool $secure <p>using a CSPRNG | @see https://paragonie.com/b/JvICXzh_jhLyt4y3</p> |
||
| 6106 | * @param array|null $array [optional] |
||
| 6107 | * |
||
| 6108 | * @return static |
||
| 6109 | * <p>(Immutable)</p> |
||
| 6110 | * |
||
| 6111 | * @phpstan-param array<TKey,T> $array |
||
| 6112 | * @phpstan-return static<TKey,T> |
||
| 6113 | */ |
||
| 6114 | 2 | public function shuffle(bool $secure = false, array $array = null): self |
|
| 6156 | |||
| 6157 | /** |
||
| 6158 | * Count the values from the current array. |
||
| 6159 | * |
||
| 6160 | * alias: for "Arrayy->count()" |
||
| 6161 | * |
||
| 6162 | * @param int $mode |
||
| 6163 | * |
||
| 6164 | * @return int |
||
| 6165 | */ |
||
| 6166 | 20 | public function size(int $mode = \COUNT_NORMAL): int |
|
| 6170 | |||
| 6171 | /** |
||
| 6172 | * Checks whether array has exactly $size items. |
||
| 6173 | * |
||
| 6174 | * @param int $size |
||
| 6175 | * |
||
| 6176 | * @return bool |
||
| 6177 | */ |
||
| 6178 | 1 | public function sizeIs(int $size): bool |
|
| 6194 | |||
| 6195 | /** |
||
| 6196 | * Checks whether array has between $fromSize to $toSize items. $toSize can be |
||
| 6197 | * smaller than $fromSize. |
||
| 6198 | * |
||
| 6199 | * @param int $fromSize |
||
| 6200 | * @param int $toSize |
||
| 6201 | * |
||
| 6202 | * @return bool |
||
| 6203 | */ |
||
| 6204 | 1 | public function sizeIsBetween(int $fromSize, int $toSize): bool |
|
| 6225 | |||
| 6226 | /** |
||
| 6227 | * Checks whether array has more than $size items. |
||
| 6228 | * |
||
| 6229 | * @param int $size |
||
| 6230 | * |
||
| 6231 | * @return bool |
||
| 6232 | */ |
||
| 6233 | 1 | View Code Duplication | public function sizeIsGreaterThan(int $size): bool |
| 6248 | |||
| 6249 | /** |
||
| 6250 | * Checks whether array has less than $size items. |
||
| 6251 | * |
||
| 6252 | * @param int $size |
||
| 6253 | * |
||
| 6254 | * @return bool |
||
| 6255 | */ |
||
| 6256 | 1 | View Code Duplication | public function sizeIsLessThan(int $size): bool |
| 6271 | |||
| 6272 | /** |
||
| 6273 | * Counts all elements in an array, or something in an object. |
||
| 6274 | * |
||
| 6275 | * <p> |
||
| 6276 | * For objects, if you have SPL installed, you can hook into count() by implementing interface {@see Countable}. |
||
| 6277 | * The interface has exactly one method, {@see Countable::count()}, which returns the return value for the count() |
||
| 6278 | * function. Please see the {@see Array} section of the manual for a detailed explanation of how arrays are |
||
| 6279 | * implemented and used in PHP. |
||
| 6280 | * </p> |
||
| 6281 | * |
||
| 6282 | * @return int |
||
| 6283 | * <p> |
||
| 6284 | * The number of elements in var, which is |
||
| 6285 | * typically an array, since anything else will have one |
||
| 6286 | * element. |
||
| 6287 | * </p> |
||
| 6288 | * <p> |
||
| 6289 | * If var is not an array or an object with |
||
| 6290 | * implemented Countable interface, |
||
| 6291 | * 1 will be returned. |
||
| 6292 | * There is one exception, if var is &null;, |
||
| 6293 | * 0 will be returned. |
||
| 6294 | * </p> |
||
| 6295 | * <p> |
||
| 6296 | * Caution: count may return 0 for a variable that isn't set, |
||
| 6297 | * but it may also return 0 for a variable that has been initialized with an |
||
| 6298 | * empty array. Use isset to test if a variable is set. |
||
| 6299 | * </p> |
||
| 6300 | */ |
||
| 6301 | 10 | public function sizeRecursive(): int |
|
| 6305 | |||
| 6306 | /** |
||
| 6307 | * Extract a slice of the array. |
||
| 6308 | * |
||
| 6309 | * @param int $offset <p>Slice begin index.</p> |
||
| 6310 | * @param int|null $length <p>Length of the slice.</p> |
||
| 6311 | * @param bool $preserveKeys <p>Whether array keys are preserved or no.</p> |
||
| 6312 | * |
||
| 6313 | * @return static |
||
| 6314 | * <p>(Immutable) A slice of the original array with length $length.</p> |
||
| 6315 | * |
||
| 6316 | * @phpstan-return static<TKey,T> |
||
| 6317 | * @psalm-mutation-free |
||
| 6318 | */ |
||
| 6319 | 5 | public function slice(int $offset, int $length = null, bool $preserveKeys = false) |
|
| 6332 | |||
| 6333 | /** |
||
| 6334 | * Sort the current array and optional you can keep the keys. |
||
| 6335 | * |
||
| 6336 | * EXAMPLE: <code> |
||
| 6337 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sort(SORT_ASC, SORT_NATURAL, false); // Arrayy[0 => 'a', 1 => 'd', 2 => 'f'] |
||
| 6338 | * </code> |
||
| 6339 | * |
||
| 6340 | * @param int|string $direction |
||
| 6341 | * <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6342 | * @param int $strategy |
||
| 6343 | * <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6344 | * <strong>SORT_NATURAL</strong></p> |
||
| 6345 | * @param bool $keepKeys |
||
| 6346 | * |
||
| 6347 | * @return static |
||
| 6348 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 6349 | * |
||
| 6350 | * @phpstan-return static<int|TKey,T> |
||
| 6351 | */ |
||
| 6352 | 20 | public function sort( |
|
| 6353 | $direction = \SORT_ASC, |
||
| 6354 | int $strategy = \SORT_REGULAR, |
||
| 6355 | bool $keepKeys = false |
||
| 6356 | ): self { |
||
| 6357 | 20 | $this->generatorToArray(); |
|
| 6358 | |||
| 6359 | 20 | return $this->sorting( |
|
| 6360 | 20 | $this->array, |
|
| 6361 | $direction, |
||
| 6362 | $strategy, |
||
| 6363 | $keepKeys |
||
| 6364 | ); |
||
| 6365 | } |
||
| 6366 | |||
| 6367 | /** |
||
| 6368 | * Sort the current array and optional you can keep the keys. |
||
| 6369 | * |
||
| 6370 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6371 | * @param int $strategy <p>sort_flags => use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6372 | * <strong>SORT_NATURAL</strong></p> |
||
| 6373 | * @param bool $keepKeys |
||
| 6374 | * |
||
| 6375 | * @return static |
||
| 6376 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 6377 | * |
||
| 6378 | * @phpstan-return static<int|TKey,T> |
||
| 6379 | */ |
||
| 6380 | 12 | public function sortImmutable( |
|
| 6381 | $direction = \SORT_ASC, |
||
| 6382 | int $strategy = \SORT_REGULAR, |
||
| 6383 | bool $keepKeys = false |
||
| 6384 | ): self { |
||
| 6385 | 12 | $that = clone $this; |
|
| 6386 | |||
| 6387 | 12 | $that->generatorToArray(); |
|
| 6388 | |||
| 6389 | 12 | return $that->sorting( |
|
| 6390 | 12 | $that->array, |
|
| 6391 | $direction, |
||
| 6392 | $strategy, |
||
| 6393 | $keepKeys |
||
| 6394 | ); |
||
| 6395 | } |
||
| 6396 | |||
| 6397 | /** |
||
| 6398 | * Sort the current array by key. |
||
| 6399 | * |
||
| 6400 | * EXAMPLE: <code> |
||
| 6401 | * a([1 => 2, 0 => 1])->sortKeys(\SORT_ASC); // Arrayy[0 => 1, 1 => 2] |
||
| 6402 | * </code> |
||
| 6403 | * |
||
| 6404 | * @see http://php.net/manual/en/function.ksort.php |
||
| 6405 | * @see http://php.net/manual/en/function.krsort.php |
||
| 6406 | * |
||
| 6407 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6408 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6409 | * <strong>SORT_NATURAL</strong></p> |
||
| 6410 | * |
||
| 6411 | * @return $this |
||
| 6412 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 6413 | * |
||
| 6414 | * @phpstan-return static<TKey,T> |
||
| 6415 | */ |
||
| 6416 | 18 | public function sortKeys( |
|
| 6426 | |||
| 6427 | /** |
||
| 6428 | * Sort the current array by key. |
||
| 6429 | * |
||
| 6430 | * @see http://php.net/manual/en/function.ksort.php |
||
| 6431 | * @see http://php.net/manual/en/function.krsort.php |
||
| 6432 | * |
||
| 6433 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6434 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6435 | * <strong>SORT_NATURAL</strong></p> |
||
| 6436 | * |
||
| 6437 | * @return $this |
||
| 6438 | * <p>(Immutable) Return this Arrayy object.</p> |
||
| 6439 | * |
||
| 6440 | * @phpstan-return static<TKey,T> |
||
| 6441 | * @psalm-mutation-free |
||
| 6442 | */ |
||
| 6443 | 8 | public function sortKeysImmutable( |
|
| 6456 | |||
| 6457 | /** |
||
| 6458 | * Sort the current array by value. |
||
| 6459 | * |
||
| 6460 | * EXAMPLE: <code> |
||
| 6461 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sortValueKeepIndex(SORT_ASC, SORT_REGULAR); // Arrayy[0 => 'a', 3 => 'd', 2 => 'f'] |
||
| 6462 | * </code> |
||
| 6463 | * |
||
| 6464 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6465 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6466 | * <strong>SORT_NATURAL</strong></p> |
||
| 6467 | * |
||
| 6468 | * @return static |
||
| 6469 | * <p>(Mutable)</p> |
||
| 6470 | * |
||
| 6471 | * @phpstan-return static<int|TKey,T> |
||
| 6472 | */ |
||
| 6473 | 1 | public function sortValueKeepIndex( |
|
| 6479 | |||
| 6480 | /** |
||
| 6481 | * Sort the current array by value. |
||
| 6482 | * |
||
| 6483 | * EXAMPLE: <code> |
||
| 6484 | * a(3 => 'd', 2 => 'f', 0 => 'a')->sortValueNewIndex(SORT_ASC, SORT_NATURAL); // Arrayy[0 => 'a', 1 => 'd', 2 => 'f'] |
||
| 6485 | * </code> |
||
| 6486 | * |
||
| 6487 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 6488 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6489 | * <strong>SORT_NATURAL</strong></p> |
||
| 6490 | * |
||
| 6491 | * @return static |
||
| 6492 | * <p>(Mutable)</p> |
||
| 6493 | * |
||
| 6494 | * @phpstan-return static<int|TKey,T> |
||
| 6495 | */ |
||
| 6496 | 1 | public function sortValueNewIndex($direction = \SORT_ASC, int $strategy = \SORT_REGULAR): self |
|
| 6500 | |||
| 6501 | /** |
||
| 6502 | * Sort a array by value or by a closure. |
||
| 6503 | * |
||
| 6504 | * - If the sorter is null, the array is sorted naturally. |
||
| 6505 | * - Associative (string) keys will be maintained, but numeric keys will be re-indexed. |
||
| 6506 | * |
||
| 6507 | * EXAMPLE: <code> |
||
| 6508 | * $testArray = range(1, 5); |
||
| 6509 | * $under = a($testArray)->sorter( |
||
| 6510 | * function ($value) { |
||
| 6511 | * return $value % 2 === 0; |
||
| 6512 | * } |
||
| 6513 | * ); |
||
| 6514 | * var_dump($under); // Arrayy[1, 3, 5, 2, 4] |
||
| 6515 | * </code> |
||
| 6516 | * |
||
| 6517 | * @param callable|mixed|null $sorter |
||
| 6518 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or |
||
| 6519 | * <strong>SORT_DESC</strong></p> |
||
| 6520 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 6521 | * <strong>SORT_NATURAL</strong></p> |
||
| 6522 | * |
||
| 6523 | * @return static |
||
| 6524 | * <p>(Immutable)</p> |
||
| 6525 | * |
||
| 6526 | * @pslam-param callable|T|null $sorter |
||
| 6527 | * @phpstan-return static<TKey,T> |
||
| 6528 | * @psalm-mutation-free |
||
| 6529 | */ |
||
| 6530 | 1 | public function sorter($sorter = null, $direction = \SORT_ASC, int $strategy = \SORT_REGULAR): self |
|
| 6571 | |||
| 6572 | /** |
||
| 6573 | * @param int $offset |
||
| 6574 | * @param int|null $length |
||
| 6575 | * @param array $replacement |
||
| 6576 | * |
||
| 6577 | * @return static |
||
| 6578 | * <p>(Immutable)</p> |
||
| 6579 | * |
||
| 6580 | * @phpstan-param array<array-key,T> $replacement |
||
| 6581 | * @phpstan-return static<TKey,T> |
||
| 6582 | * @psalm-mutation-free |
||
| 6583 | */ |
||
| 6584 | 1 | public function splice(int $offset, int $length = null, $replacement = []): self |
|
| 6601 | |||
| 6602 | /** |
||
| 6603 | * Split an array in the given amount of pieces. |
||
| 6604 | * |
||
| 6605 | * EXAMPLE: <code> |
||
| 6606 | * a(['a' => 1, 'b' => 2])->split(2, true); // Arrayy[['a' => 1], ['b' => 2]] |
||
| 6607 | * </code> |
||
| 6608 | * |
||
| 6609 | * @param int $numberOfPieces |
||
| 6610 | * @param bool $keepKeys |
||
| 6611 | * |
||
| 6612 | * @return static |
||
| 6613 | * <p>(Immutable)</p> |
||
| 6614 | * |
||
| 6615 | * @phpstan-return static<TKey,T> |
||
| 6616 | * @psalm-mutation-free |
||
| 6617 | */ |
||
| 6618 | 1 | public function split(int $numberOfPieces = 2, bool $keepKeys = false): self |
|
| 6674 | |||
| 6675 | /** |
||
| 6676 | * Strip all empty items from the current array. |
||
| 6677 | * |
||
| 6678 | * EXAMPLE: <code> |
||
| 6679 | * a(['a' => 1, 'b' => ''])->stripEmpty(); // Arrayy[['a' => 1]] |
||
| 6680 | * </code> |
||
| 6681 | * |
||
| 6682 | * @return static |
||
| 6683 | * <p>(Immutable)</p> |
||
| 6684 | * |
||
| 6685 | * @phpstan-return static<TKey,T> |
||
| 6686 | * @psalm-mutation-free |
||
| 6687 | */ |
||
| 6688 | 1 | public function stripEmpty(): self |
|
| 6700 | |||
| 6701 | /** |
||
| 6702 | * Swap two values between positions by key. |
||
| 6703 | * |
||
| 6704 | * EXAMPLE: <code> |
||
| 6705 | * a(['a' => 1, 'b' => ''])->swap('a', 'b'); // Arrayy[['a' => '', 'b' => 1]] |
||
| 6706 | * </code> |
||
| 6707 | * |
||
| 6708 | * @param int|string $swapA <p>a key in the array</p> |
||
| 6709 | * @param int|string $swapB <p>a key in the array</p> |
||
| 6710 | * |
||
| 6711 | * @return static |
||
| 6712 | * <p>(Immutable)</p> |
||
| 6713 | * |
||
| 6714 | * @phpstan-return static<TKey,T> |
||
| 6715 | * @psalm-mutation-free |
||
| 6716 | */ |
||
| 6717 | 1 | public function swap($swapA, $swapB): self |
|
| 6729 | |||
| 6730 | /** |
||
| 6731 | * Get the current array from the "Arrayy"-object. |
||
| 6732 | * alias for "getArray()" |
||
| 6733 | * |
||
| 6734 | * @param bool $convertAllArrayyElements <p> |
||
| 6735 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 6736 | * </p> |
||
| 6737 | * @param bool $preserveKeys <p> |
||
| 6738 | * e.g.: A generator maybe return the same key more then once, |
||
| 6739 | * so maybe you will ignore the keys. |
||
| 6740 | * </p> |
||
| 6741 | * |
||
| 6742 | * @return array |
||
| 6743 | * |
||
| 6744 | * @phpstan-return array<TKey,T> |
||
| 6745 | * @psalm-mutation-free |
||
| 6746 | */ |
||
| 6747 | 944 | public function toArray( |
|
| 6748 | bool $convertAllArrayyElements = false, |
||
| 6749 | bool $preserveKeys = true |
||
| 6750 | ): array { |
||
| 6751 | // init |
||
| 6752 | 944 | $array = []; |
|
| 6753 | |||
| 6754 | 944 | if ($convertAllArrayyElements) { |
|
| 6755 | 3 | foreach ($this->getGenerator() as $key => $value) { |
|
| 6756 | 3 | if ($value instanceof self) { |
|
| 6757 | 2 | $value = $value->toArray( |
|
| 6758 | 2 | $convertAllArrayyElements, |
|
| 6759 | $preserveKeys |
||
| 6760 | ); |
||
| 6761 | } |
||
| 6762 | |||
| 6763 | 3 | if ($preserveKeys) { |
|
| 6764 | 2 | $array[$key] = $value; |
|
| 6765 | } else { |
||
| 6766 | 1 | $array[] = $value; |
|
| 6767 | } |
||
| 6768 | } |
||
| 6769 | } else { |
||
| 6770 | 943 | $array = \iterator_to_array($this->getGenerator(), $preserveKeys); |
|
| 6771 | } |
||
| 6772 | |||
| 6773 | /** @phpstan-ignore-next-line - depends on the $convertAllArrayyElements parameter :/ */ |
||
| 6774 | 944 | return $array; |
|
| 6775 | } |
||
| 6776 | |||
| 6777 | /** |
||
| 6778 | * Get the current array from the "Arrayy"-object as list. |
||
| 6779 | * |
||
| 6780 | * @param bool $convertAllArrayyElements <p> |
||
| 6781 | * Convert all Child-"Arrayy" objects also to arrays. |
||
| 6782 | * </p> |
||
| 6783 | * |
||
| 6784 | * @return array |
||
| 6785 | * |
||
| 6786 | * @phpstan-return list<T> |
||
| 6787 | * @psalm-mutation-free |
||
| 6788 | */ |
||
| 6789 | 1 | public function toList(bool $convertAllArrayyElements = false): array |
|
| 6797 | |||
| 6798 | /** |
||
| 6799 | * Convert the current array to JSON. |
||
| 6800 | * |
||
| 6801 | * EXAMPLE: <code> |
||
| 6802 | * a(['bar', ['foo']])->toJson(); // '["bar",{"1":"foo"}]' |
||
| 6803 | * </code> |
||
| 6804 | * |
||
| 6805 | * @param int $options [optional] <p>e.g. JSON_PRETTY_PRINT</p> |
||
| 6806 | * @param int $depth [optional] <p>Set the maximum depth. Must be greater than zero.</p> |
||
| 6807 | * |
||
| 6808 | * @return string |
||
| 6809 | */ |
||
| 6810 | 13 | public function toJson(int $options = 0, int $depth = 512): string |
|
| 6819 | |||
| 6820 | /** |
||
| 6821 | * @param string[]|null $items [optional] |
||
| 6822 | * @param string[] $helper [optional] |
||
| 6823 | * |
||
| 6824 | * @return static|static[] |
||
| 6825 | * |
||
| 6826 | * @phpstan-return static<int, static<TKey,T>> |
||
| 6827 | */ |
||
| 6828 | 1 | public function toPermutation(array $items = null, array $helper = []): self |
|
| 6864 | |||
| 6865 | /** |
||
| 6866 | * Implodes array to a string with specified separator. |
||
| 6867 | * |
||
| 6868 | * @param string $separator [optional] <p>The element's separator.</p> |
||
| 6869 | * |
||
| 6870 | * @return string |
||
| 6871 | * <p>The string representation of array, separated by ",".</p> |
||
| 6872 | */ |
||
| 6873 | 19 | public function toString(string $separator = ','): string |
|
| 6877 | |||
| 6878 | /** |
||
| 6879 | * Return a duplicate free copy of the current array. |
||
| 6880 | * |
||
| 6881 | * EXAMPLE: <code> |
||
| 6882 | * a([2 => 1, 3 => 2, 4 => 2])->uniqueNewIndex(); // Arrayy[1, 2] |
||
| 6883 | * </code> |
||
| 6884 | * |
||
| 6885 | * @return $this |
||
| 6886 | * <p>(Mutable)</p> |
||
| 6887 | * |
||
| 6888 | * @phpstan-return static<int,T> |
||
| 6889 | */ |
||
| 6890 | 13 | public function uniqueNewIndex(): self |
|
| 6908 | |||
| 6909 | /** |
||
| 6910 | * Return a duplicate free copy of the current array. (with the old keys) |
||
| 6911 | * |
||
| 6912 | * EXAMPLE: <code> |
||
| 6913 | * a([2 => 1, 3 => 2, 4 => 2])->uniqueNewIndex(); // Arrayy[2 => 1, 3 => 2] |
||
| 6914 | * </code> |
||
| 6915 | * |
||
| 6916 | * @return $this |
||
| 6917 | * <p>(Mutable)</p> |
||
| 6918 | * |
||
| 6919 | * @phpstan-return static<TKey,T> |
||
| 6920 | */ |
||
| 6921 | 11 | public function uniqueKeepIndex(): self |
|
| 6947 | |||
| 6948 | /** |
||
| 6949 | * alias: for "Arrayy->uniqueNewIndex()" |
||
| 6950 | * |
||
| 6951 | * @return static |
||
| 6952 | * <p>(Mutable) Return this Arrayy object, with the appended values.</p> |
||
| 6953 | * |
||
| 6954 | * @see Arrayy::unique() |
||
| 6955 | * |
||
| 6956 | * @phpstan-return static<int,T> |
||
| 6957 | */ |
||
| 6958 | 13 | public function unique(): self |
|
| 6962 | |||
| 6963 | /** |
||
| 6964 | * Prepends one or more values to the beginning of array at once. |
||
| 6965 | * |
||
| 6966 | * @param mixed ...$args |
||
| 6967 | * |
||
| 6968 | * @return $this |
||
| 6969 | * <p>(Mutable) Return this Arrayy object, with prepended elements to the beginning of array.</p> |
||
| 6970 | * |
||
| 6971 | * @phpstan-param array<TKey,T> ...$args |
||
| 6972 | * @phpstan-return static<TKey,T> |
||
| 6973 | */ |
||
| 6974 | 6 | View Code Duplication | public function unshift(...$args): self |
| 6992 | |||
| 6993 | /** |
||
| 6994 | * Tests whether the given closure return something valid for all elements of this array. |
||
| 6995 | * |
||
| 6996 | * @param \Closure $closure the predicate |
||
| 6997 | * |
||
| 6998 | * @return bool |
||
| 6999 | * <p>TRUE, if the predicate yields TRUE for all elements, FALSE otherwise.</p> |
||
| 7000 | * |
||
| 7001 | * @phpstan-param \Closure(T=,TKey=):bool $closure |
||
| 7002 | */ |
||
| 7003 | 1 | View Code Duplication | public function validate(\Closure $closure): bool |
| 7013 | |||
| 7014 | /** |
||
| 7015 | * Get all values from a array. |
||
| 7016 | * |
||
| 7017 | * EXAMPLE: <code> |
||
| 7018 | * $arrayy = a([1 => 'foo', 2 => 'foo2', 3 => 'bar']); |
||
| 7019 | * $arrayyTmp->values(); // Arrayy[0 => 'foo', 1 => 'foo2', 2 => 'bar'] |
||
| 7020 | * </code> |
||
| 7021 | * |
||
| 7022 | * @return static |
||
| 7023 | * <p>(Immutable)</p> |
||
| 7024 | * |
||
| 7025 | * @phpstan-return static<TKey,T> |
||
| 7026 | * @psalm-mutation-free |
||
| 7027 | */ |
||
| 7028 | 2 | public function values(): self |
|
| 7040 | |||
| 7041 | /** |
||
| 7042 | * Apply the given function to every element in the array, discarding the results. |
||
| 7043 | * |
||
| 7044 | * EXAMPLE: <code> |
||
| 7045 | * $callable = function (&$value, $key) { |
||
| 7046 | * $value = $key; |
||
| 7047 | * }; |
||
| 7048 | * $arrayy = a([1, 2, 3]); |
||
| 7049 | * $arrayy->walk($callable); // Arrayy[0, 1, 2] |
||
| 7050 | * </code> |
||
| 7051 | * |
||
| 7052 | * @param callable $callable |
||
| 7053 | * @param bool $recursive |
||
| 7054 | * [optional] <p>Whether array will be walked recursively or no</p> |
||
| 7055 | * @param mixed $userData |
||
| 7056 | * [optional] <p> |
||
| 7057 | * If the optional $userData parameter is supplied, |
||
| 7058 | * it will be passed as the third parameter to the $callable. |
||
| 7059 | * </p> |
||
| 7060 | * |
||
| 7061 | * @return $this |
||
| 7062 | * <p>(Mutable) Return this Arrayy object, with modified elements.</p> |
||
| 7063 | * |
||
| 7064 | * @template TExtra |
||
| 7065 | * <p>The extra input value type.</p> |
||
| 7066 | * |
||
| 7067 | * @phostan-param TExtra $userData |
||
| 7068 | * @phpstan-param callable(T,TKey,?TExtra):void $callable |
||
| 7069 | * @phpstan-return static<TKey,T> |
||
| 7070 | */ |
||
| 7071 | 12 | public function walk( |
|
| 7097 | |||
| 7098 | /** |
||
| 7099 | * Returns a collection of matching items. |
||
| 7100 | * |
||
| 7101 | * @param string $keyOrPropertyOrMethod |
||
| 7102 | * <p>The property or method to evaluate.</p> |
||
| 7103 | * @param mixed $value |
||
| 7104 | * <p>The value to match.</p> |
||
| 7105 | * |
||
| 7106 | * @throws \InvalidArgumentException if property or method is not defined |
||
| 7107 | * |
||
| 7108 | * @return static |
||
| 7109 | * |
||
| 7110 | * @phpstan-return static<TKey,T> |
||
| 7111 | */ |
||
| 7112 | 1 | public function where(string $keyOrPropertyOrMethod, $value): self |
|
| 7113 | { |
||
| 7114 | 1 | return $this->filter( |
|
| 7115 | function ($item) use ($keyOrPropertyOrMethod, $value) { |
||
| 7116 | $accessorValue = $this->extractValue( |
||
| 7117 | $item, |
||
| 7118 | $keyOrPropertyOrMethod |
||
| 7119 | ); |
||
| 7120 | |||
| 7121 | return $accessorValue === $value; |
||
| 7122 | 1 | } |
|
| 7123 | ); |
||
| 7124 | } |
||
| 7125 | |||
| 7126 | /** |
||
| 7127 | * Convert an array into a object. |
||
| 7128 | * |
||
| 7129 | * @param array $array |
||
| 7130 | * |
||
| 7131 | * @return \stdClass |
||
| 7132 | * |
||
| 7133 | * @phpstan-param array<int|string,mixed> $array |
||
| 7134 | */ |
||
| 7135 | 4 | final protected static function arrayToObject(array $array = []): \stdClass |
|
| 7154 | |||
| 7155 | /** |
||
| 7156 | * @param array|\Generator|null $input <p> |
||
| 7157 | * An array containing keys to return. |
||
| 7158 | * </p> |
||
| 7159 | * @param mixed|null $search_values [optional] <p> |
||
| 7160 | * If specified, then only keys containing these values are returned. |
||
| 7161 | * </p> |
||
| 7162 | * @param bool $strict [optional] <p> |
||
| 7163 | * Determines if strict comparison (===) should be used during the |
||
| 7164 | * search. |
||
| 7165 | * </p> |
||
| 7166 | * |
||
| 7167 | * @return array |
||
| 7168 | * <p>An array of all the keys in input.</p> |
||
| 7169 | * |
||
| 7170 | * @template TInput |
||
| 7171 | * |
||
| 7172 | * @phpstan-param array<array-key,TInput>|\Generator<array-key,TInput>|null $input |
||
| 7173 | * @phpstan-return array<array-key>|array<TKey> |
||
| 7174 | * |
||
| 7175 | * @psalm-mutation-free |
||
| 7176 | */ |
||
| 7177 | 11 | protected function array_keys_recursive( |
|
| 7238 | |||
| 7239 | /** |
||
| 7240 | * @param string $path |
||
| 7241 | * @param callable $callable |
||
| 7242 | * @param array|null $currentOffset |
||
| 7243 | * |
||
| 7244 | * @return void |
||
| 7245 | * |
||
| 7246 | * @phpstan-param array<TKey,T>|null $currentOffset |
||
| 7247 | * @psalm-mutation-free |
||
| 7248 | */ |
||
| 7249 | 10 | protected function callAtPath($path, $callable, &$currentOffset = null) |
|
| 7250 | { |
||
| 7251 | 10 | $this->generatorToArray(); |
|
| 7252 | |||
| 7253 | 10 | if ($currentOffset === null) { |
|
| 7254 | 10 | $currentOffset = &$this->array; |
|
| 7255 | } |
||
| 7256 | |||
| 7257 | 10 | $explodedPath = \explode($this->pathSeparator, $path); |
|
| 7258 | 10 | if ($explodedPath === false) { |
|
| 7259 | return; |
||
| 7260 | } |
||
| 7261 | |||
| 7262 | 10 | $nextPath = \array_shift($explodedPath); |
|
| 7263 | |||
| 7264 | 10 | if (!isset($currentOffset[$nextPath])) { |
|
| 7265 | 1 | return; |
|
| 7266 | } |
||
| 7267 | |||
| 7268 | 9 | if (!empty($explodedPath)) { |
|
| 7269 | 1 | $this->callAtPath( |
|
| 7270 | 1 | \implode($this->pathSeparator, $explodedPath), |
|
| 7271 | $callable, |
||
| 7272 | 1 | $currentOffset[$nextPath] |
|
| 7273 | ); |
||
| 7274 | } else { |
||
| 7275 | 9 | $callable($currentOffset[$nextPath]); |
|
| 7276 | } |
||
| 7277 | 9 | } |
|
| 7278 | |||
| 7279 | /** |
||
| 7280 | * Extracts the value of the given property or method from the object. |
||
| 7281 | * |
||
| 7282 | * @param static<T> $object |
||
| 7283 | * <p>The object to extract the value from.</p> |
||
| 7284 | * @param string $keyOrPropertyOrMethod |
||
| 7285 | * <p>The property or method for which the |
||
| 7286 | * value should be extracted.</p> |
||
| 7287 | * |
||
| 7288 | * @throws \InvalidArgumentException if the method or property is not defined |
||
| 7289 | * |
||
| 7290 | * @return mixed |
||
| 7291 | * <p>The value extracted from the specified property or method.</p> |
||
| 7292 | * |
||
| 7293 | * @phpstan-param self<TKey,T> $object |
||
| 7294 | */ |
||
| 7295 | 1 | final protected function extractValue(self $object, string $keyOrPropertyOrMethod) |
|
| 7317 | |||
| 7318 | /** |
||
| 7319 | * create a fallback for array |
||
| 7320 | * |
||
| 7321 | * 1. use the current array, if it's a array |
||
| 7322 | * 2. fallback to empty array, if there is nothing |
||
| 7323 | * 3. call "getArray()" on object, if there is a "Arrayy"-object |
||
| 7324 | * 4. call "createFromObject()" on object, if there is a "\Traversable"-object |
||
| 7325 | * 5. call "__toArray()" on object, if the method exists |
||
| 7326 | * 6. cast a string or object with "__toString()" into an array |
||
| 7327 | * 7. throw a "InvalidArgumentException"-Exception |
||
| 7328 | * |
||
| 7329 | * @param mixed $data |
||
| 7330 | * |
||
| 7331 | * @throws \InvalidArgumentException |
||
| 7332 | * |
||
| 7333 | * @return array |
||
| 7334 | * |
||
| 7335 | * @phpstan-return array<mixed>|array<TKey,T> |
||
| 7336 | */ |
||
| 7337 | 1215 | protected function fallbackForArray(&$data): array |
|
| 7347 | |||
| 7348 | /** |
||
| 7349 | * @param bool $preserveKeys <p> |
||
| 7350 | * e.g.: A generator maybe return the same key more then once, |
||
| 7351 | * so maybe you will ignore the keys. |
||
| 7352 | * </p> |
||
| 7353 | * |
||
| 7354 | * @return bool |
||
| 7355 | * |
||
| 7356 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 7357 | * @psalm-mutation-free :/ |
||
| 7358 | */ |
||
| 7359 | 1124 | protected function generatorToArray(bool $preserveKeys = true) |
|
| 7370 | |||
| 7371 | /** |
||
| 7372 | * Get correct PHP constant for direction. |
||
| 7373 | * |
||
| 7374 | * @param int|string $direction |
||
| 7375 | * |
||
| 7376 | * @return int |
||
| 7377 | * @psalm-mutation-free |
||
| 7378 | */ |
||
| 7379 | 43 | protected function getDirection($direction): int |
|
| 7401 | |||
| 7402 | /** |
||
| 7403 | * @return TypeCheckInterface[] |
||
| 7404 | * |
||
| 7405 | * @noinspection ReturnTypeCanBeDeclaredInspection |
||
| 7406 | */ |
||
| 7407 | 25 | protected function getPropertiesFromPhpDoc() |
|
| 7462 | |||
| 7463 | /** |
||
| 7464 | * @param string $glue |
||
| 7465 | * @param mixed $pieces |
||
| 7466 | * @param bool $useKeys |
||
| 7467 | * |
||
| 7468 | * @return string |
||
| 7469 | * |
||
| 7470 | * @phpstan-param scalar|object|self<TKey|T>|array<TKey,T> $pieces |
||
| 7471 | * @psalm-mutation-free |
||
| 7472 | */ |
||
| 7473 | 37 | protected function implode_recursive( |
|
| 7510 | |||
| 7511 | /** |
||
| 7512 | * @param mixed $needle <p> |
||
| 7513 | * The searched value. |
||
| 7514 | * </p> |
||
| 7515 | * <p> |
||
| 7516 | * If needle is a string, the comparison is done |
||
| 7517 | * in a case-sensitive manner. |
||
| 7518 | * </p> |
||
| 7519 | * @param array|\Generator|null $haystack <p> |
||
| 7520 | * The array. |
||
| 7521 | * </p> |
||
| 7522 | * @param bool $strict [optional] <p> |
||
| 7523 | * If the third parameter strict is set to true |
||
| 7524 | * then the in_array function will also check the |
||
| 7525 | * types of the |
||
| 7526 | * needle in the haystack. |
||
| 7527 | * </p> |
||
| 7528 | * |
||
| 7529 | * @return bool |
||
| 7530 | * <p>true if needle is found in the array, false otherwise</p> |
||
| 7531 | * |
||
| 7532 | * @phpstan-param (array&T)|array<TKey,T>|\Generator<TKey,T>|null $haystack |
||
| 7533 | * |
||
| 7534 | * @psalm-mutation-free |
||
| 7535 | */ |
||
| 7536 | 18 | protected function in_array_recursive($needle, $haystack = null, $strict = true): bool |
|
| 7561 | |||
| 7562 | /** |
||
| 7563 | * @param mixed $data |
||
| 7564 | * |
||
| 7565 | * @return array<mixed>|null |
||
| 7566 | */ |
||
| 7567 | 1215 | protected function internalGetArray(&$data) |
|
| 7618 | |||
| 7619 | /** |
||
| 7620 | * Internal mechanics of remove method. |
||
| 7621 | * |
||
| 7622 | * @param float|int|string $key |
||
| 7623 | * |
||
| 7624 | * @return bool |
||
| 7625 | */ |
||
| 7626 | 22 | protected function internalRemove($key): bool |
|
| 7659 | |||
| 7660 | /** |
||
| 7661 | * Internal mechanic of set method. |
||
| 7662 | * |
||
| 7663 | * @param int|string|null $key |
||
| 7664 | * @param mixed $value |
||
| 7665 | * @param bool $checkProperties |
||
| 7666 | * |
||
| 7667 | * @return bool |
||
| 7668 | */ |
||
| 7669 | 1065 | protected function internalSet( |
|
| 7727 | |||
| 7728 | /** |
||
| 7729 | * Convert a object into an array. |
||
| 7730 | * |
||
| 7731 | * @param mixed|object $object |
||
| 7732 | * |
||
| 7733 | * @return array|mixed |
||
| 7734 | * |
||
| 7735 | * @psalm-mutation-free |
||
| 7736 | */ |
||
| 7737 | 5 | protected static function objectToArray($object) |
|
| 7750 | |||
| 7751 | /** |
||
| 7752 | * @param array $data |
||
| 7753 | * @param bool $checkPropertiesInConstructor |
||
| 7754 | * |
||
| 7755 | * @return void |
||
| 7756 | * |
||
| 7757 | * @phpstan-param array<mixed,T> $data |
||
| 7758 | */ |
||
| 7759 | 1213 | protected function setInitialValuesAndProperties(array &$data, bool $checkPropertiesInConstructor) |
|
| 7760 | { |
||
| 7761 | 1213 | $checkPropertiesInConstructor = $this->checkForMissingPropertiesInConstructor === true |
|
| 7762 | && |
||
| 7763 | 1213 | $checkPropertiesInConstructor === true; |
|
| 7764 | |||
| 7765 | 1213 | if ($this->properties !== []) { |
|
| 7766 | 105 | foreach ($data as $key => &$valueInner) { |
|
| 7767 | 105 | $this->internalSet( |
|
| 7768 | 105 | $key, |
|
| 7769 | $valueInner, |
||
| 7770 | $checkPropertiesInConstructor |
||
| 7771 | ); |
||
| 7772 | } |
||
| 7773 | } else { |
||
| 7774 | if ( |
||
| 7775 | 1128 | $this->checkPropertyTypes === true |
|
| 7776 | || |
||
| 7777 | 1128 | $checkPropertiesInConstructor === true |
|
| 7778 | ) { |
||
| 7779 | 24 | $this->properties = $this->getPropertiesFromPhpDoc(); |
|
| 7780 | } |
||
| 7781 | |||
| 7782 | /** @var TypeCheckInterface[] $properties */ |
||
| 7783 | 1128 | $properties = $this->properties; |
|
| 7784 | |||
| 7785 | if ( |
||
| 7786 | 1128 | $this->checkPropertiesMismatchInConstructor === true |
|
| 7787 | && |
||
| 7788 | 1128 | \count($data) !== 0 |
|
| 7789 | && |
||
| 7790 | 1128 | \count(\array_diff_key($properties, $data)) > 0 |
|
| 7791 | ) { |
||
| 7792 | 1 | throw new \TypeError('Property mismatch - input: ' . \print_r(\array_keys($data), true) . ' | expected: ' . \print_r(\array_keys($properties), true)); |
|
| 7793 | } |
||
| 7794 | |||
| 7795 | 1127 | foreach ($data as $key => &$valueInner) { |
|
| 7796 | 956 | $this->internalSet( |
|
| 7797 | 956 | $key, |
|
| 7798 | $valueInner, |
||
| 7799 | $checkPropertiesInConstructor |
||
| 7800 | ); |
||
| 7801 | } |
||
| 7802 | } |
||
| 7803 | 1205 | } |
|
| 7804 | |||
| 7805 | /** |
||
| 7806 | * sorting keys |
||
| 7807 | * |
||
| 7808 | * @param array $elements |
||
| 7809 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 7810 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 7811 | * <strong>SORT_NATURAL</strong></p> |
||
| 7812 | * |
||
| 7813 | * @return $this |
||
| 7814 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 7815 | * |
||
| 7816 | * @phpstan-param array<mixed|TKey,T> $elements |
||
| 7817 | * @phpstan-return static<TKey,T> |
||
| 7818 | */ |
||
| 7819 | 18 | protected function sorterKeys( |
|
| 7840 | |||
| 7841 | /** |
||
| 7842 | * @param array $elements <p>Warning: used as reference</p> |
||
| 7843 | * @param int|string $direction <p>use <strong>SORT_ASC</strong> (default) or <strong>SORT_DESC</strong></p> |
||
| 7844 | * @param int $strategy <p>use e.g.: <strong>SORT_REGULAR</strong> (default) or |
||
| 7845 | * <strong>SORT_NATURAL</strong></p> |
||
| 7846 | * @param bool $keepKeys |
||
| 7847 | * |
||
| 7848 | * @return $this |
||
| 7849 | * <p>(Mutable) Return this Arrayy object.</p> |
||
| 7850 | * |
||
| 7851 | * @phpstan-param array<mixed|TKey,T> $elements |
||
| 7852 | * @phpstan-return static<int|TKey,T> |
||
| 7853 | */ |
||
| 7854 | 24 | protected function sorting( |
|
| 7888 | |||
| 7889 | /** |
||
| 7890 | * @param array $array |
||
| 7891 | * |
||
| 7892 | * @return array |
||
| 7893 | * |
||
| 7894 | * @psalm-mutation-free |
||
| 7895 | */ |
||
| 7896 | 25 | private function getArrayRecursiveHelperArrayy(array $array) |
|
| 7897 | { |
||
| 7898 | 25 | if ($array === []) { |
|
| 7899 | return []; |
||
| 7900 | } |
||
| 7901 | |||
| 7902 | 25 | \array_walk_recursive( |
|
| 7903 | 25 | $array, |
|
| 7904 | /** |
||
| 7905 | * @param array|self $item |
||
| 7906 | * |
||
| 7907 | * @return void |
||
| 7908 | */ |
||
| 7909 | 25 | static function (&$item) { |
|
| 7910 | 25 | if ($item instanceof self) { |
|
| 7911 | 1 | $item = $item->getArray(); |
|
| 7912 | } |
||
| 7913 | 25 | } |
|
| 7914 | ); |
||
| 7915 | |||
| 7916 | 25 | return $array; |
|
| 7917 | } |
||
| 7918 | |||
| 7919 | /** |
||
| 7920 | * @param int|string|null $key |
||
| 7921 | * @param mixed $value |
||
| 7922 | * |
||
| 7923 | * @return void |
||
| 7924 | */ |
||
| 7925 | 118 | private function checkType($key, $value) |
|
| 7943 | } |
||
| 7944 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..