|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace BenTools\IterableFunctions; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use EmptyIterator; |
| 7 | +use InvalidArgumentException; |
| 8 | +use IteratorAggregate; |
| 9 | +use Traversable; |
| 10 | + |
| 11 | +final class IterableObject implements IteratorAggregate |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var array|Traversable |
| 15 | + */ |
| 16 | + private $iterable; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var callable |
| 20 | + */ |
| 21 | + private $filter; |
| 22 | + |
| 23 | + /** |
| 24 | + * @var callable |
| 25 | + */ |
| 26 | + private $map; |
| 27 | + |
| 28 | + /** |
| 29 | + * IterableObject constructor. |
| 30 | + * @param $iterable |
| 31 | + * @param callable|null $filter |
| 32 | + * @param callable|null $map |
| 33 | + * @throws InvalidArgumentException |
| 34 | + */ |
| 35 | + public function __construct($iterable, $filter = null, $map = null) |
| 36 | + { |
| 37 | + if (null === $iterable) { |
| 38 | + $iterable = new EmptyIterator(); |
| 39 | + } |
| 40 | + if (!is_iterable($iterable)) { |
| 41 | + throw new InvalidArgumentException( |
| 42 | + sprintf('Expected array or Traversable, got %s', is_object($iterable) ? get_class($iterable) : gettype($iterable)) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + // Cannot rely on callable type-hint on PHP 5.3 |
| 47 | + if (null !== $filter && !is_callable($filter) && !$filter instanceof Closure) { |
| 48 | + throw new InvalidArgumentException( |
| 49 | + sprintf('Expected callable, got %s', is_object($filter) ? get_class($filter) : gettype($filter)) |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + if (null !== $map && !is_callable($map) && !$map instanceof Closure) { |
| 54 | + throw new InvalidArgumentException( |
| 55 | + sprintf('Expected callable, got %s', is_object($map) ? get_class($map) : gettype($map)) |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + $this->iterable = $iterable; |
| 60 | + $this->filter = $filter; |
| 61 | + $this->map = $map; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param callable $filter |
| 66 | + * @return self |
| 67 | + */ |
| 68 | + public function withFilter($filter) |
| 69 | + { |
| 70 | + return new self($this->iterable, $filter, $this->map); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * @param callable $map |
| 75 | + * @return self |
| 76 | + */ |
| 77 | + public function withMap($map) |
| 78 | + { |
| 79 | + return new self($this->iterable, $this->filter, $map); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @inheritdoc |
| 84 | + */ |
| 85 | + public function getIterator() |
| 86 | + { |
| 87 | + $iterable = $this->iterable; |
| 88 | + if (null !== $this->filter) { |
| 89 | + $iterable = iterable_filter($iterable, $this->filter); |
| 90 | + } |
| 91 | + if (null !== $this->map) { |
| 92 | + $iterable = iterable_map($iterable, $this->map); |
| 93 | + } |
| 94 | + return iterable_to_traversable($iterable); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @return array |
| 99 | + */ |
| 100 | + public function asArray() |
| 101 | + { |
| 102 | + $iterable = $this->iterable; |
| 103 | + if (null !== $this->filter) { |
| 104 | + $iterable = iterable_filter($iterable, $this->filter); |
| 105 | + } |
| 106 | + if (null !== $this->map) { |
| 107 | + $iterable = iterable_map($iterable, $this->map); |
| 108 | + } |
| 109 | + return iterable_to_array($iterable); |
| 110 | + } |
| 111 | +} |
0 commit comments