| 1 | <?php |
||
| 8 | trait StrictIterableTrait |
||
| 9 | { |
||
| 10 | use CommonMutableContainerTrait; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * {@inheritDoc} |
||
| 14 | * @return $this |
||
| 15 | */ |
||
| 16 | 1 | public function map(callable $callable) |
|
| 17 | { |
||
| 18 | 1 | $res = new static(); |
|
| 19 | 1 | foreach ($this as $v) { |
|
|
|
|||
| 20 | 1 | $res[] = $callable($v); |
|
| 21 | 1 | } |
|
| 22 | |||
| 23 | 1 | return $res; |
|
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * {@inheritDoc} |
||
| 28 | * @return $this |
||
| 29 | */ |
||
| 30 | 2 | public function filter(callable $callable) |
|
| 31 | { |
||
| 32 | 2 | $res = new static(); |
|
| 33 | 2 | foreach ($this as $v) { |
|
| 34 | 2 | if ($callable($v)) { |
|
| 35 | 2 | $res[] = $v; |
|
| 36 | 2 | } |
|
| 37 | 2 | } |
|
| 38 | |||
| 39 | 2 | return $res; |
|
| 40 | } |
||
| 41 | |||
| 42 | 2 | public function zip(Iterable $iterable) |
|
| 43 | 2 | { |
|
| 44 | $res = new static(); |
||
| 45 | $it = $iterable->getIterator(); |
||
| 46 | foreach ($this as $v) { |
||
| 47 | if (!$it->valid()) { |
||
| 48 | break; |
||
| 49 | } |
||
| 50 | $res[] = new Pair($v, $it->current()); |
||
| 51 | $it->next(); |
||
| 52 | } |
||
| 53 | |||
| 54 | return $res; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * {@inheritDoc} |
||
| 59 | * @return $this |
||
| 60 | */ |
||
| 61 | 1 | public function take($size = 1) |
|
| 62 | { |
||
| 63 | 1 | $res = new static(); |
|
| 64 | |||
| 65 | 1 | if ($size <= 0) { |
|
| 66 | return $res; |
||
| 67 | } |
||
| 68 | |||
| 69 | 1 | foreach ($this as $v) { |
|
| 70 | 1 | $res[] = $v; |
|
| 71 | 1 | if (--$size === 0) { |
|
| 72 | 1 | break; |
|
| 73 | } |
||
| 74 | 1 | } |
|
| 75 | |||
| 76 | 1 | return $res; |
|
| 77 | } |
||
| 78 | |||
| 79 | public function takeWhile(callable $callable) |
||
| 80 | { |
||
| 81 | $res = new static(); |
||
| 82 | foreach ($this as $v) { |
||
| 83 | if (!$callable($v)) { |
||
| 84 | break; |
||
| 85 | } |
||
| 86 | $res[] = $v; |
||
| 87 | } |
||
| 88 | |||
| 89 | return $res; |
||
| 90 | } |
||
| 91 | |||
| 92 | public function skip($n) |
||
| 93 | { |
||
| 94 | $res = new static(); |
||
| 95 | foreach ($this as $v) { |
||
| 96 | if ($n <= 0) { |
||
| 97 | $res[] = $v; |
||
| 98 | } else { |
||
| 99 | --$n; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | |||
| 103 | return $res; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function skipWhile(callable $callable) |
||
| 107 | { |
||
| 108 | $res = new static(); |
||
| 109 | $skip = true; |
||
| 110 | foreach ($this as $v) { |
||
| 111 | if ($skip) { |
||
| 112 | if ($callable($v)) { |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | $skip = false; |
||
| 116 | } |
||
| 117 | $res[] = $v; |
||
| 118 | } |
||
| 119 | |||
| 120 | return $res; |
||
| 121 | } |
||
| 122 | |||
| 123 | public function slice($start, $length) |
||
| 124 | { |
||
| 125 | $res = new static(); |
||
| 126 | if ($length <= 0) { |
||
| 127 | return $res; |
||
| 128 | } |
||
| 129 | foreach ($this as $v) { |
||
| 130 | if ($start !== 0) { |
||
| 131 | --$start; |
||
| 132 | continue; |
||
| 133 | } |
||
| 134 | $res[] = $v; |
||
| 135 | if (--$length === 0) { |
||
| 136 | break; |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | return $res; |
||
| 141 | } |
||
| 142 | |||
| 143 | 1 | public function concat(\Traversable $iterable) |
|
| 144 | { |
||
| 145 | 1 | $res = []; |
|
| 146 | |||
| 147 | 1 | foreach ($this as $v) { |
|
| 148 | 1 | $res[] = $v; |
|
| 149 | 1 | } |
|
| 150 | |||
| 151 | 1 | foreach ($iterable as $v) { |
|
| 152 | 1 | $res[] = $v; |
|
| 153 | 1 | } |
|
| 154 | 1 | $this->container = $res; |
|
| 155 | |||
| 156 | 1 | return $this; |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * {@inheritDoc} |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | 2 | public function first() |
|
| 164 | { |
||
| 165 | 2 | foreach ($this as $v) { |
|
| 166 | 1 | return $v; |
|
| 167 | 1 | } |
|
| 168 | |||
| 169 | 1 | return null; |
|
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritDoc} |
||
| 174 | * @return $this |
||
| 175 | */ |
||
| 176 | 2 | public function last() |
|
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritDoc} |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | public function each(callable $callable) |
||
| 188 | { |
||
| 189 | foreach ($this as $v) { |
||
| 190 | $callable($v); |
||
| 191 | } |
||
| 192 | |||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | 1 | public function exists(callable $fn) |
|
| 209 | |||
| 210 | public function concatAll() |
||
| 222 | } |
||
| 223 |