SelectIterator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 7
dl 0
loc 33
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 4 2
A __construct() 0 4 1
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