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

FormatterCombiner   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 30
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

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