Passed
Push — master ( 3a94f6...b04e78 )
by Magnar Ovedal
03:48
created

FormatterCombiner::applyFormatters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
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\WordFormatter;
6
7
use Stadly\PasswordPolice\WordFormatter;
8
use Traversable;
9
10
final class FormatterCombiner extends ChainableFormatter
11
{
12
    /**
13
     * @var WordFormatter[] Word formatters.
14
     */
15
    private $wordFormatters;
16
17
    /**
18
     * @param WordFormatter[] $wordFormatters Word formatters.
19
     * @param bool $includeUnformatted Whether the result should also include the words unformatted.
20
     * @param bool $filterUnique Whether the result should only contain unique words.
21
     */
22 5
    public function __construct(array $wordFormatters, bool $includeUnformatted = true, bool $filterUnique = true)
23
    {
24 5
        if ($filterUnique) {
25 2
            $formatterCombiner = new FormatterCombiner($wordFormatters, $includeUnformatted, false);
26 2
            $formatterCombiner->setNext(new UniqueFilter());
27 2
            $wordFormatters = [$formatterCombiner];
28 5
        } elseif ($includeUnformatted) {
29 3
            $wordFormatters[] = new Unformatter();
30
        }
31
32 5
        $this->wordFormatters = $wordFormatters;
33 5
    }
34
35
    /**
36
     * @param iterable<string> $words Words to format.
37
     * @return Traversable<string> The words formatted by all the word formatters in the formatter combiner.
38
     */
39 5
    protected function applyCurrent(iterable $words): Traversable
40
    {
41 5
        foreach ($this->wordFormatters as $wordFormatter) {
42 5
            yield from $wordFormatter->apply($words);
43
        }
44 5
    }
45
}
46