PageRenderTime 38ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Functional/Each.php

http://github.com/lstrojny/functional-php
PHP | 32 lines | 11 code | 5 blank | 16 comment | 0 complexity | 017efae489bb7ea67bd77567d5c16b35 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /**
  3. * @package Functional-php
  4. * @author Lars Strojny <lstrojny@php.net>
  5. * @copyright 2011-2021 Lars Strojny
  6. * @license https://opensource.org/licenses/MIT MIT
  7. * @link https://github.com/lstrojny/functional-php
  8. */
  9. namespace Functional;
  10. use Functional\Exceptions\InvalidArgumentException;
  11. use Traversable;
  12. /**
  13. * Iterates over a collection of elements, yielding each in turn to a callback function. Each invocation of $callback
  14. * is called with three arguments: (element, index, collection)
  15. *
  16. * @param Traversable|array $collection
  17. * @param callable $callback
  18. * @return null
  19. * @no-named-arguments
  20. */
  21. function each($collection, callable $callback)
  22. {
  23. InvalidArgumentException::assertCollection($collection, __FUNCTION__, 1);
  24. foreach ($collection as $index => $element) {
  25. $callback($element, $index, $collection);
  26. }
  27. }