Passed
Push — master ( 8e1c26...6f90eb )
by Magnar Ovedal
03:35
created

FormatterCombiner::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\DateFormatter;
6
7
use DateTimeInterface;
8
use Stadly\PasswordPolice\DateFormatter;
9
use Stadly\PasswordPolice\WordFormatter\UniqueFilter;
10
use Traversable;
11
12
final class FormatterCombiner extends ChainableFormatter
13
{
14
    /**
15
     * @var DateFormatter[] Date formatters.
16
     */
17
    private $dateFormatters;
18
19
    /**
20
     * @param DateFormatter[] $dateFormatters Date formatters.
21
     * @param bool $filterUnique Whether the result should only contain unique words.
22
     */
23 3
    public function __construct(array $dateFormatters, bool $filterUnique = true)
24
    {
25 3
        if ($filterUnique) {
26 1
            $formatterCombiner = new FormatterCombiner($dateFormatters, false);
27 1
            $formatterCombiner->setNext(new UniqueFilter());
28 1
            $dateFormatters = [$formatterCombiner];
29
        }
30
31 3
        $this->dateFormatters = $dateFormatters;
32 3
    }
33
34
    /**
35
     * @param iterable<DateTimeInterface> $dates Dates to format.
36
     * @return Traversable<string> The date formatted by all the date formatters in the formatter combiner.
37
     */
38 3
    protected function applyCurrent(iterable $dates): Traversable
39
    {
40 3
        foreach ($this->dateFormatters as $dateFormatter) {
41 3
            yield from $dateFormatter->apply($dates);
42
        }
43 3
    }
44
}
45