|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the Nette Framework (https://nette.org) |
| 5 | + * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Nette\Utils; |
| 11 | + |
| 12 | +use Nette; |
| 13 | + |
| 14 | + |
| 15 | +/** |
| 16 | + * Utilities for iterables. |
| 17 | + */ |
| 18 | +final class Iterables |
| 19 | +{ |
| 20 | + use Nette\StaticClass; |
| 21 | + |
| 22 | + /** |
| 23 | + * Tests for the presence of value. |
| 24 | + */ |
| 25 | + public static function contains(iterable $iterable, mixed $value): bool |
| 26 | + { |
| 27 | + foreach ($iterable as $v) { |
| 28 | + if ($v === $value) { |
| 29 | + return true; |
| 30 | + } |
| 31 | + } |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + |
| 36 | + /** |
| 37 | + * Tests for the presence of key. |
| 38 | + */ |
| 39 | + public static function containsKey(iterable $iterable, mixed $key): bool |
| 40 | + { |
| 41 | + foreach ($iterable as $k => $v) { |
| 42 | + if ($k === $key) { |
| 43 | + return true; |
| 44 | + } |
| 45 | + } |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Returns the first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null. |
| 52 | + * The $predicate has the signature `function ($value, $key, $iterable): bool`. |
| 53 | + * @template T |
| 54 | + * @param iterable<T> $iterable |
| 55 | + * @return ?T |
| 56 | + */ |
| 57 | + public static function first(iterable $iterable, ?callable $predicate = null, ?callable $else = null): mixed |
| 58 | + { |
| 59 | + foreach ($iterable as $k => $v) { |
| 60 | + if (!$predicate || $predicate($v, $k, $iterable)) { |
| 61 | + return $v; |
| 62 | + } |
| 63 | + } |
| 64 | + return $else ? $else() : null; |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * Returns the key of first item (matching the specified predicate if given). If there is no such item, it returns result of invoking $else or null. |
| 70 | + * The $predicate has the signature `function ($value, $key, $iterable): bool`. |
| 71 | + * @template T |
| 72 | + * @param iterable<T, mixed> $iterable |
| 73 | + * @return ?T |
| 74 | + */ |
| 75 | + public static function firstKey(iterable $iterable, ?callable $predicate = null, ?callable $else = null): mixed |
| 76 | + { |
| 77 | + foreach ($iterable as $k => $v) { |
| 78 | + if (!$predicate || $predicate($v, $k, $iterable)) { |
| 79 | + return $k; |
| 80 | + } |
| 81 | + } |
| 82 | + return $else ? $else() : null; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + /** |
| 87 | + * Tests whether at least one element in the array passes the test implemented by the |
| 88 | + * provided callback with signature `function ($value, $key, $iterable): bool`. |
| 89 | + * @template K |
| 90 | + * @template V |
| 91 | + * @param iterable<K, V> $iterable |
| 92 | + * @param callable(V, K, iterable<K, V>): bool $predicate |
| 93 | + */ |
| 94 | + public static function some(iterable $iterable, callable $predicate): bool |
| 95 | + { |
| 96 | + foreach ($iterable as $k => $v) { |
| 97 | + if ($predicate($v, $k, $iterable)) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + } |
| 101 | + return false; |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | + /** |
| 106 | + * Tests whether all elements in the array pass the test implemented by the provided function, |
| 107 | + * which has the signature `function ($value, $key, $iterable): bool`. |
| 108 | + * @template K |
| 109 | + * @template V |
| 110 | + * @param iterable<K, V> $iterable |
| 111 | + * @param callable(V, K, iterable<K, V>): bool $predicate |
| 112 | + */ |
| 113 | + public static function every(iterable $iterable, callable $predicate): bool |
| 114 | + { |
| 115 | + foreach ($iterable as $k => $v) { |
| 116 | + if (!$predicate($v, $k, $iterable)) { |
| 117 | + return false; |
| 118 | + } |
| 119 | + } |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns a new array containing all key-value pairs matching the given $predicate. |
| 126 | + * The callback has the signature `function ($value, $key, $iterable): bool`. |
| 127 | + * @template K |
| 128 | + * @template V |
| 129 | + * @param iterable<K, V> $iterable |
| 130 | + * @param callable(V, K, iterable<K, V>): bool $predicate |
| 131 | + * @return \Generator<K, V> |
| 132 | + */ |
| 133 | + public static function filter(iterable $iterable, callable $predicate): \Generator |
| 134 | + { |
| 135 | + foreach ($iterable as $k => $v) { |
| 136 | + if ($predicate($v, $k, $iterable)) { |
| 137 | + yield $k => $v; |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + /** |
| 144 | + * Calls $transform on all elements in the array and returns the array of return values. |
| 145 | + * The callback has the signature `function ($value, $key, $iterable): bool`. |
| 146 | + * @template K |
| 147 | + * @template V |
| 148 | + * @template R |
| 149 | + * @param iterable<K, V> $iterable |
| 150 | + * @param callable(V, K, iterable<K, V>): R $transformer |
| 151 | + * @return \Generator<K, R> |
| 152 | + */ |
| 153 | + public static function map(iterable $iterable, callable $transformer): \Generator |
| 154 | + { |
| 155 | + foreach ($iterable as $k => $v) { |
| 156 | + yield $k => $transformer($v, $k, $iterable); |
| 157 | + } |
| 158 | + } |
| 159 | +} |
0 commit comments