SelectIterator::getIterator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace SubjectivePHP\Linq;
4
5
use Traversable;
6
use IteratorAggregate;
7
8
final class SelectIterator implements IteratorAggregate
9
{
10
    /**
11
     * @var Traversable
12
     */
13
    private $collection;
14
15
    /**
16
     * @var callable
17
     */
18
    private $selector;
19
20
    /**
21
     * Create a SelectIterator from another iterator.
22
     *
23
     * @param Traversable $collection The iterator to be filtered.
24
     * @param callable    $selector   The callback which should return values from each item in the iterator.
25
     */
26
    public function __construct(Traversable $collection, callable $selector)
27
    {
28
        $this->collection = $collection;
29
        $this->selector = $selector;
30
    }
31
32
    /**
33
     * Returns an external iterator.
34
     *
35
     * @return Traversable
36
     */
37
    public function getIterator() : Traversable
38
    {
39
        foreach ($this->collection as $item) {
40
            yield ($this->selector)($item);
41
        }
42
    }
43
}
44