Complex classes like CollectionTrait 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 CollectionTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | trait CollectionTrait |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Converts $collection to array. If there are multiple items with the same key, only the last will be preserved. |
||
| 11 | * |
||
| 12 | * @return array |
||
| 13 | */ |
||
| 14 | 60 | public function toArray() |
|
| 15 | { |
||
| 16 | 60 | return toArray($this->getItems()); |
|
| 17 | } |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Returns a lazy collection of items for which $function returned true. |
||
| 21 | * |
||
| 22 | * @param callable $function ($value, $key) |
||
| 23 | * @return Collection |
||
| 24 | */ |
||
| 25 | 1 | public function filter(callable $function) |
|
| 26 | { |
||
| 27 | 1 | return filter($this->getItems(), $function); |
|
| 28 | } |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Returns a lazy collection of distinct items. The comparison is the same as in in_array. |
||
| 32 | * |
||
| 33 | * @return Collection |
||
| 34 | */ |
||
| 35 | 1 | public function distinct() |
|
| 36 | { |
||
| 37 | 1 | return distinct($this->getItems()); |
|
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Returns a lazy collection with items from all $collections passed as argument appended together |
||
| 42 | * |
||
| 43 | * @param \Traversable[]|array ...$collections |
||
| 44 | * @return Collection |
||
| 45 | */ |
||
| 46 | 1 | public function concat(...$collections) |
|
| 47 | { |
||
| 48 | 1 | return concat($this, ...$collections); |
|
| 49 | } |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Returns collection where each item is changed to the output of executing $function on each key/item. |
||
| 53 | * |
||
| 54 | * @param callable $function |
||
| 55 | * @return Collection |
||
| 56 | */ |
||
| 57 | 3 | public function map(callable $function) |
|
| 58 | { |
||
| 59 | 3 | return map($this->getItems(), $function); |
|
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Reduces the collection to single value by iterating over the collection and calling $function while |
||
| 64 | * passing $startValue and current key/item as parameters. The output of $function is used as $startValue in |
||
| 65 | * next iteration. The output of $function on last element is the return value of this function. |
||
| 66 | * |
||
| 67 | * @param mixed $startValue |
||
| 68 | * @param callable $function ($tmpValue, $value, $key) |
||
| 69 | * @return mixed |
||
| 70 | */ |
||
| 71 | 2 | public function reduce(callable $function, $startValue) |
|
| 72 | { |
||
| 73 | 2 | $result = reduce($this->getItems(), $function, $startValue); |
|
| 74 | |||
| 75 | 2 | return $result; |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Returns a lazy collection with one or multiple levels of nesting flattened. Removes all nesting when no value |
||
| 80 | * is passed. |
||
| 81 | * |
||
| 82 | * @param int $depth How many levels should be flatten, default (-1) is infinite. |
||
| 83 | * @return Collection |
||
| 84 | */ |
||
| 85 | 1 | public function flatten($depth = -1) |
|
| 86 | { |
||
| 87 | 1 | return flatten($this->getItems(), $depth); |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Returns a non-lazy collection sorted using $function($item1, $item2, $key1, $key2 ). $function should |
||
| 92 | * return true if first item is larger than the second and false otherwise. |
||
| 93 | * |
||
| 94 | * @param callable $function ($value1, $value2, $key1, $key2) |
||
| 95 | * @return Collection |
||
| 96 | */ |
||
| 97 | 1 | public function sort(callable $function) |
|
| 98 | { |
||
| 99 | 1 | return \DusanKasan\Knapsack\sort($this->getItems(), $function); |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Returns lazy collection items of which are part of the original collection from item number $from to item |
||
| 104 | * number $to. The items before $from are also iterated over, just not returned. |
||
| 105 | * |
||
| 106 | * @param int $from |
||
| 107 | * @param int $to If omitted, will slice until end |
||
| 108 | * @return Collection |
||
| 109 | */ |
||
| 110 | 1 | public function slice($from, $to = -1) |
|
| 111 | { |
||
| 112 | 1 | return slice($this->getItems(), $from, $to); |
|
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * Returns collection which items are separated into groups indexed by the return value of $function. |
||
| 117 | * |
||
| 118 | * @param callable $function ($value, $key) |
||
| 119 | * @return Collection |
||
| 120 | */ |
||
| 121 | 1 | public function groupBy(callable $function) |
|
| 122 | { |
||
| 123 | 1 | return groupBy($this->getItems(), $function); |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Returns collection where items are separated into groups indexed by the value at given key. |
||
| 128 | * |
||
| 129 | * @param $key |
||
| 130 | * @return Collection |
||
| 131 | */ |
||
| 132 | 1 | public function groupByKey($key) |
|
| 133 | { |
||
| 134 | 1 | return groupByKey($this->getItems(), $key); |
|
| 135 | } |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Returns a lazy collection in which $function is executed for each item. |
||
| 139 | * |
||
| 140 | * @param callable $function ($value, $key) |
||
| 141 | * @return Collection |
||
| 142 | */ |
||
| 143 | 1 | public function each(callable $function) |
|
| 144 | { |
||
| 145 | 1 | return \DusanKasan\Knapsack\each($this->getItems(), $function); |
|
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Returns the number of items in this collection. |
||
| 150 | * |
||
| 151 | * @return int |
||
| 152 | */ |
||
| 153 | 6 | public function size() |
|
| 154 | { |
||
| 155 | 6 | return size($this->getItems()); |
|
| 156 | 1 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Returns value at the key $key. If multiple values have this key, return first. If no value has this key, throw |
||
| 160 | * ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable an |
||
| 161 | * instance of Collection will be returned. |
||
| 162 | * |
||
| 163 | * @param mixed $key |
||
| 164 | * @param bool $convertToCollection |
||
| 165 | * @return Collection|mixed |
||
| 166 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 167 | */ |
||
| 168 | 3 | public function get($key, $convertToCollection = false) |
|
| 169 | { |
||
| 170 | 3 | $result = get($this->getItems(), $key); |
|
| 171 | |||
| 172 | 3 | return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
|
| 173 | 2 | } |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Returns item at the key $key. If multiple items have this key, return first. If no item has this key, return |
||
| 177 | * $ifNotFound. If no value has this key, throw ItemNotFound. If $convertToCollection is true and the return value |
||
| 178 | * is a collection (array|Traversable) an instance of Collection will be returned. |
||
| 179 | * |
||
| 180 | * @param mixed $key |
||
| 181 | * @param mixed $default |
||
| 182 | * @param bool $convertToCollection |
||
| 183 | * @return mixed |
||
| 184 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 185 | */ |
||
| 186 | 1 | public function getOrDefault($key, $default = null, $convertToCollection = false) |
|
| 187 | { |
||
| 188 | 1 | $result = getOrDefault($this->getItems(), $key, $default); |
|
| 189 | |||
| 190 | 1 | return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Returns nth item in the collection starting from 0. If the size of this collection is smaller than $position, |
||
| 195 | * throw ItemNotFound. If $convertToCollection is true and the return value is a collection (array|Traversable an |
||
| 196 | * instance of Collection will be returned. |
||
| 197 | * |
||
| 198 | * @param int $position |
||
| 199 | * @param bool $convertToCollection |
||
| 200 | * @return Collection|mixed |
||
| 201 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 202 | */ |
||
| 203 | 6 | public function getNth($position, $convertToCollection = false) |
|
| 204 | { |
||
| 205 | 6 | $result = getNth($this->getItems(), $position); |
|
| 206 | |||
| 207 | 6 | return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
|
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Returns first value matched by $function. If no value matches, return $default. If $convertToCollection is true |
||
| 212 | * and the return value is a collection (array|Traversable an instance of Collection will be returned. |
||
| 213 | * |
||
| 214 | * @param callable $function |
||
| 215 | * @param mixed|null $default |
||
| 216 | * @param bool $convertToCollection |
||
| 217 | * @return Collection|mixed |
||
| 218 | */ |
||
| 219 | 1 | public function find(callable $function, $default = null, $convertToCollection = false) |
|
| 220 | { |
||
| 221 | 1 | $result = find($this->getItems(), $function, $default); |
|
| 222 | |||
| 223 | 1 | return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
|
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns a non-lazy collection of items whose keys are the return values of $function and values are the number of |
||
| 228 | * items in this collection for which the $function returned this value. |
||
| 229 | * |
||
| 230 | * @param callable $function |
||
| 231 | * @return Collection |
||
| 232 | */ |
||
| 233 | 1 | public function countBy(callable $function) |
|
| 234 | { |
||
| 235 | 1 | return countBy($this->getItems(), $function); |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Returns a lazy collection by changing keys of this collection for each item to the result of $function for |
||
| 240 | * that item. |
||
| 241 | * |
||
| 242 | * @param callable $function |
||
| 243 | * @return Collection |
||
| 244 | */ |
||
| 245 | 1 | public function indexBy(callable $function) |
|
| 246 | { |
||
| 247 | 1 | return indexBy($this->getItems(), $function); |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Returns true if $function returns true for every item in this collection, false otherwise. |
||
| 252 | * |
||
| 253 | * @param callable $function |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | 1 | public function every(callable $function) |
|
| 257 | { |
||
| 258 | 1 | return every($this->getItems(), $function); |
|
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns true if $function returns true for at least one item in this collection, false otherwise. |
||
| 263 | * |
||
| 264 | * @param callable $function |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | 1 | public function some(callable $function) |
|
| 268 | { |
||
| 269 | 1 | return some($this->getItems(), $function); |
|
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Returns true if $value is present in the collection. |
||
| 274 | * |
||
| 275 | * @param mixed $value |
||
| 276 | * @return bool |
||
| 277 | */ |
||
| 278 | 1 | public function contains($value) |
|
| 279 | { |
||
| 280 | 1 | return contains($this->getItems(), $value); |
|
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Returns collection of items in this collection in reverse order. |
||
| 285 | * |
||
| 286 | * @return Collection |
||
| 287 | */ |
||
| 288 | 1 | public function reverse() |
|
| 289 | { |
||
| 290 | 1 | return reverse($this->getItems()); |
|
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Reduce the collection to single value. Walks from right to left. |
||
| 295 | * |
||
| 296 | * @param callable $function Must take 2 arguments, intermediate value and item from the iterator. |
||
| 297 | * @param mixed $startValue |
||
| 298 | * @return mixed |
||
| 299 | */ |
||
| 300 | 1 | public function reduceRight(callable $function, $startValue) |
|
| 301 | { |
||
| 302 | 1 | return reduceRight($this->getItems(), $function, $startValue); |
|
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * A form of slice that returns first $numberOfItems items. |
||
| 307 | * |
||
| 308 | * @param int $numberOfItems |
||
| 309 | * @return Collection |
||
| 310 | */ |
||
| 311 | 9 | public function take($numberOfItems) |
|
| 312 | { |
||
| 313 | 9 | return take($this->getItems(), $numberOfItems); |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * A form of slice that returns all but first $numberOfItems items. |
||
| 318 | * |
||
| 319 | * @param int $numberOfItems |
||
| 320 | * @return Collection |
||
| 321 | */ |
||
| 322 | 2 | public function drop($numberOfItems) |
|
| 323 | { |
||
| 324 | 2 | return drop($this->getItems(), $numberOfItems); |
|
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Returns collection of values from this collection but with keys being numerical from 0 upwards. |
||
| 329 | * |
||
| 330 | * @return Collection |
||
| 331 | */ |
||
| 332 | 8 | public function values() |
|
| 333 | { |
||
| 334 | 8 | return values($this->getItems()); |
|
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Returns a lazy collection without elements matched by $function. |
||
| 339 | * |
||
| 340 | * @param callable $function |
||
| 341 | * @return Collection |
||
| 342 | */ |
||
| 343 | 1 | public function reject(callable $function) |
|
| 344 | { |
||
| 345 | 1 | return reject($this->getItems(), $function); |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Returns a lazy collection of the keys of this collection. |
||
| 350 | * |
||
| 351 | * @return Collection |
||
| 352 | */ |
||
| 353 | 1 | public function keys() |
|
| 354 | { |
||
| 355 | 1 | return keys($this->getItems()); |
|
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Returns a lazy collection of items of this collection separated by $separator |
||
| 360 | * |
||
| 361 | * @param mixed $separator |
||
| 362 | * @return Collection |
||
| 363 | */ |
||
| 364 | 1 | public function interpose($separator) |
|
| 365 | { |
||
| 366 | 1 | return interpose($this->getItems(), $separator); |
|
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Returns a lazy collection with last $numberOfItems items skipped. These are still iterated over, just skipped. |
||
| 371 | * |
||
| 372 | * @param int $numberOfItems |
||
| 373 | * @return Collection |
||
| 374 | */ |
||
| 375 | 1 | public function dropLast($numberOfItems = 1) |
|
| 376 | { |
||
| 377 | 1 | return dropLast($this->getItems(), $numberOfItems); |
|
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Returns a lazy collection of first item from first collection, first item from second, second from first and |
||
| 382 | * so on. Accepts any number of collections. |
||
| 383 | * |
||
| 384 | * @param array|\Traversable ...$collections |
||
| 385 | * @return Collection |
||
| 386 | */ |
||
| 387 | 1 | public function interleave(...$collections) |
|
| 388 | { |
||
| 389 | 1 | return interleave($this->getItems(), ...$collections); |
|
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Returns an infinite lazy collection of items in this collection repeated infinitely. |
||
| 394 | * |
||
| 395 | * @return Collection |
||
| 396 | */ |
||
| 397 | 1 | public function cycle() |
|
| 398 | { |
||
| 399 | 1 | return cycle($this->getItems()); |
|
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Returns a lazy collection of items of this collection with $value added as first element. If $key is not provided |
||
| 404 | * it will be next integer index. |
||
| 405 | * |
||
| 406 | * @param mixed $value |
||
| 407 | * @param mixed|null $key |
||
| 408 | * @return Collection |
||
| 409 | */ |
||
| 410 | 2 | public function prepend($value, $key = null) |
|
| 411 | { |
||
| 412 | 2 | return prepend($this->getItems(), $value, $key); |
|
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Returns a lazy collection of items of this collection with $value added as last element. If $key is not provided |
||
| 417 | * it will be next integer index. |
||
| 418 | * |
||
| 419 | * @param mixed $value |
||
| 420 | * @param mixed $key |
||
| 421 | * @return Collection |
||
| 422 | */ |
||
| 423 | 8 | public function append($value, $key = null) |
|
| 424 | { |
||
| 425 | 8 | return append($this->getItems(), $value, $key); |
|
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Returns a lazy collection by removing items from this collection until first item for which $function returns |
||
| 430 | * false. |
||
| 431 | * |
||
| 432 | * @param callable $function |
||
| 433 | * @return Collection |
||
| 434 | */ |
||
| 435 | 1 | public function dropWhile(callable $function) |
|
| 436 | { |
||
| 437 | 1 | return dropWhile($this->getItems(), $function); |
|
| 438 | } |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Returns a lazy collection which is a result of calling map($function) and then flatten(1) |
||
| 442 | * |
||
| 443 | * @param callable $function |
||
| 444 | * @return Collection |
||
| 445 | */ |
||
| 446 | 1 | public function mapcat(callable $function) |
|
| 447 | { |
||
| 448 | 1 | return mapcat($this->getItems(), $function); |
|
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Returns a lazy collection of items from the start of the ollection until the first item for which $function |
||
| 453 | * returns false. |
||
| 454 | * |
||
| 455 | * @param callable $function |
||
| 456 | * @return Collection |
||
| 457 | */ |
||
| 458 | 1 | public function takeWhile(callable $function) |
|
| 459 | { |
||
| 460 | 1 | return takeWhile($this->getItems(), $function); |
|
| 461 | } |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns a collection of [take($position), drop($position)] |
||
| 465 | * |
||
| 466 | * @param int $position |
||
| 467 | * @return Collection |
||
| 468 | */ |
||
| 469 | 1 | public function splitAt($position) |
|
| 470 | { |
||
| 471 | 1 | return splitAt($this->getItems(), $position); |
|
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Returns a collection of [takeWhile($predicament), dropWhile($predicament] |
||
| 476 | * |
||
| 477 | * @param callable $function |
||
| 478 | * @return Collection |
||
| 479 | */ |
||
| 480 | 1 | public function splitWith(callable $function) |
|
| 481 | { |
||
| 482 | 1 | return splitWith($this->getItems(), $function); |
|
| 483 | } |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns a lazy collection with items from this collection but values that are found in keys of $replacementMap |
||
| 487 | * are replaced by their values. |
||
| 488 | * |
||
| 489 | * @param \Traversable|array $replacementMap |
||
| 490 | * @return Collection |
||
| 491 | */ |
||
| 492 | 1 | public function replace($replacementMap) |
|
| 493 | { |
||
| 494 | 1 | return replace($this->getItems(), $replacementMap); |
|
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Returns a lazy collection of reduction steps. |
||
| 499 | * |
||
| 500 | * @param callable $function |
||
| 501 | * @param mixed $startValue |
||
| 502 | * @return Collection |
||
| 503 | */ |
||
| 504 | 2 | public function reductions(callable $function, $startValue) |
|
| 505 | { |
||
| 506 | 2 | return reductions($this->getItems(), $function, $startValue); |
|
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Returns a lazy collection of every nth item in this collection |
||
| 511 | * |
||
| 512 | * @param int $step |
||
| 513 | * @return Collection |
||
| 514 | */ |
||
| 515 | 1 | public function takeNth($step) |
|
| 516 | { |
||
| 517 | 1 | return takeNth($this->getItems(), $step); |
|
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Returns a non-collection of shuffled items from this collection |
||
| 522 | * |
||
| 523 | * @return Collection |
||
| 524 | */ |
||
| 525 | 1 | public function shuffle() |
|
| 526 | { |
||
| 527 | 1 | return \DusanKasan\Knapsack\shuffle($this->getItems()); |
|
| 528 | } |
||
| 529 | |||
| 530 | /** |
||
| 531 | * Returns a lazy collection of collections of $numberOfItems items each, at $step step |
||
| 532 | * apart. If $step is not supplied, defaults to $numberOfItems, i.e. the partitions |
||
| 533 | * do not overlap. If a $padding collection is supplied, use its elements as |
||
| 534 | * necessary to complete last partition up to $numberOfItems items. In case there are |
||
| 535 | * not enough padding elements, return a partition with less than $numberOfItems items. |
||
| 536 | * |
||
| 537 | * @param int $numberOfItems |
||
| 538 | * @param int $step |
||
| 539 | * @param array|\Traversable $padding |
||
| 540 | * @return Collection |
||
| 541 | */ |
||
| 542 | 1 | public function partition($numberOfItems, $step = 0, $padding = []) |
|
| 543 | { |
||
| 544 | 1 | return partition($this->getItems(), $numberOfItems, $step, $padding); |
|
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Creates a lazy collection of collections created by partitioning this collection every time $function will |
||
| 549 | * return different result. |
||
| 550 | * |
||
| 551 | * @param callable $function |
||
| 552 | * @return Collection |
||
| 553 | */ |
||
| 554 | 1 | public function partitionBy(callable $function) |
|
| 555 | { |
||
| 556 | 1 | return partitionBy($this->getItems(), $function); |
|
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Returns true if this collection is empty. False otherwise. |
||
| 561 | * |
||
| 562 | * @return bool |
||
| 563 | */ |
||
| 564 | 2 | public function isEmpty() |
|
| 565 | { |
||
| 566 | 2 | return isEmpty($this->getItems()); |
|
| 567 | } |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Opposite of isEmpty. |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | */ |
||
| 574 | 2 | public function isNotEmpty() |
|
| 575 | { |
||
| 576 | 2 | return isNotEmpty($this->getItems()); |
|
| 577 | 1 | } |
|
| 578 | |||
| 579 | /** |
||
| 580 | * Returns a collection where keys are distinct items from this collection and their values are number of |
||
| 581 | * occurrences of each value. |
||
| 582 | * |
||
| 583 | * @return Collection |
||
| 584 | */ |
||
| 585 | 1 | public function frequencies() |
|
| 586 | { |
||
| 587 | 1 | return frequencies($this->getItems()); |
|
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Returns first item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
||
| 592 | * is true and the return value is a collection (array|Traversable an instance of Collection is returned. |
||
| 593 | * |
||
| 594 | * @param bool $convertToCollection |
||
| 595 | * @return mixed|Collection |
||
| 596 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 597 | */ |
||
| 598 | 11 | public function first($convertToCollection = false) |
|
| 599 | { |
||
| 600 | 11 | $result = first($this->getItems()); |
|
| 601 | 10 | return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
|
| 602 | } |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Returns last item of this collection. If the collection is empty, throws ItemNotFound. If $convertToCollection |
||
| 606 | * is true and the return value is a collection (array|Traversable an |
||
| 607 | * |
||
| 608 | * @param bool $convertToCollection |
||
| 609 | * @return mixed|Collection |
||
| 610 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 611 | */ |
||
| 612 | 2 | public function last($convertToCollection = false) |
|
| 613 | { |
||
| 614 | 2 | $result = last($this->getItems()); |
|
| 615 | 1 | return (isCollection($result) && $convertToCollection) ? new Collection($result) : $result; |
|
| 616 | } |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Realizes collection - turns lazy collection into non-lazy one by iterating over it and storing the key/values. |
||
| 620 | * |
||
| 621 | * @return Collection |
||
| 622 | */ |
||
| 623 | 1 | public function realize() |
|
| 624 | { |
||
| 625 | 1 | return realize($this->getItems()); |
|
| 626 | } |
||
| 627 | |||
| 628 | /** |
||
| 629 | * Returns the second item in this collection or throws ItemNotFound if the collection is empty or has 1 item. |
||
| 630 | * |
||
| 631 | * @return mixed |
||
| 632 | */ |
||
| 633 | 2 | public function second() |
|
| 634 | { |
||
| 635 | 2 | return second($this->getItems()); |
|
| 636 | } |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Combines the values of this collection as keys, with values of $collection as values. The resulting collection |
||
| 640 | * has length equal to the size of smaller collection. |
||
| 641 | * |
||
| 642 | * @param array|\Traversable $collection |
||
| 643 | * @return Collection |
||
| 644 | * @throws \DusanKasan\Knapsack\Exceptions\ItemNotFound |
||
| 645 | */ |
||
| 646 | 1 | public function combine($collection) |
|
| 647 | { |
||
| 648 | 1 | return combine($this->getItems(), $collection); |
|
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Returns a lazy collection without the items associated to any of the keys from $keys. |
||
| 653 | * |
||
| 654 | * @param array|\Traversable $keys |
||
| 655 | * @return Collection |
||
| 656 | */ |
||
| 657 | 1 | public function except($keys) |
|
| 661 | |||
| 662 | /** |
||
| 663 | * Returns a lazy collection of items associated to any of the keys from $keys. |
||
| 664 | * |
||
| 665 | * @param array|\Traversable $keys |
||
| 666 | * @return Collection |
||
| 667 | */ |
||
| 668 | 1 | public function only($keys) |
|
| 672 | |||
| 673 | /** |
||
| 674 | * Returns a lazy collection of items that are in $this but are not in any of the other arguments. Note that the |
||
| 675 | * ...$collections are iterated non-lazily. |
||
| 676 | * |
||
| 677 | * @param array|\Traversable ...$collections |
||
| 678 | * @return Collection |
||
| 679 | */ |
||
| 680 | 1 | public function difference(...$collections) |
|
| 684 | |||
| 685 | |||
| 686 | /** |
||
| 687 | * Returns a lazy collection where keys and values are flipped. |
||
| 688 | * |
||
| 689 | * @return Collection |
||
| 690 | */ |
||
| 691 | 1 | public function flip() |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Checks for the existence of an item with$key in this collection. |
||
| 698 | * |
||
| 699 | * @param mixed $key |
||
| 700 | * @return bool |
||
| 701 | */ |
||
| 702 | 1 | public function has($key) |
|
| 706 | |||
| 707 | |||
| 708 | /** |
||
| 709 | * Returns a lazy collection of non-lazy collections of items from nth position from this collection and each |
||
| 710 | * passed collection. Stops when any of the collections don't have an item at the nth position. |
||
| 711 | * |
||
| 712 | * @param array|\Traversable[] ...$collections |
||
| 713 | * @return Collection |
||
| 714 | */ |
||
| 715 | 1 | public function zip(...$collections) |
|
| 716 | { |
||
| 717 | 1 | array_unshift($collections, $this->getItems()); |
|
| 718 | 1 | return zip(...$collections); |
|
| 719 | } |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Uses a $transformer callable that takes a Collection and returns Collection on itself. |
||
| 723 | * |
||
| 724 | * @param callable $transformer Collection => Collection |
||
| 725 | * @return mixed |
||
| 726 | * @throws InvalidReturnValue |
||
| 727 | */ |
||
| 728 | 1 | public function transform(callable $transformer) |
|
| 729 | { |
||
| 730 | 1 | $transformed = $transformer($this->getItems()); |
|
| 731 | |||
| 732 | 1 | if (!($transformed instanceof Collection)) { |
|
| 733 | 1 | throw new InvalidReturnValue; |
|
| 734 | } |
||
| 735 | |||
| 736 | 1 | return $transformed; |
|
| 737 | } |
||
| 738 | |||
| 739 | /** |
||
| 740 | * Extracts data from collection items by dot separated key path. Supports the * wildcard. If a key contains \ or |
||
| 741 | * it must be escaped using \ character. |
||
| 742 | * |
||
| 743 | * @param mixed $keyPath |
||
| 744 | * @return Collection |
||
| 745 | */ |
||
| 746 | 1 | public function extract($keyPath) |
|
| 747 | { |
||
| 748 | 1 | return \DusanKasan\Knapsack\extract($this->getItems(), $keyPath); |
|
| 749 | } |
||
| 750 | |||
| 751 | /** |
||
| 752 | * @return array|\Traversable |
||
| 753 | */ |
||
| 754 | 79 | protected function getItems() |
|
| 755 | { |
||
| 756 | 79 | return $this; |
|
| 757 | } |
||
| 758 | } |
||
| 759 |