Passed
Push — master ( b72b81...29b0fb )
by Magnar Ovedal
04:04
created

FormatterCombiner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
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
     * @var bool Whether the result should also include the words unformatted.
19
     */
20
    private $includeUnformatted;
21
22
    /**
23
     * @var bool Whether the result should only contain unique words.
24
     */
25
    private $filterUnique;
26
27
    /**
28
     * @param WordFormatter[] $wordFormatters Word formatters.
29
     * @param bool $includeUnformatted Whether the result should also include the words unformatted.
30
     * @param bool $filterUnique Whether the result should only contain unique words.
31
     */
32 4
    public function __construct(array $wordFormatters, bool $includeUnformatted = true, bool $filterUnique = true)
33
    {
34 4
        $this->wordFormatters = $wordFormatters;
35 4
        $this->includeUnformatted = $includeUnformatted;
36 4
        $this->filterUnique = $filterUnique;
37 4
    }
38
39
    /**
40
     * @param iterable<string> $words Words to format.
41
     * @return Traversable<string> The words formatted by all the word formatters in the formatter combiner.
42
     */
43 4
    protected function applyCurrent(iterable $words): Traversable
44
    {
45 4
        $formatted = $this->applyFormatters($words);
46
47 4
        if ($this->filterUnique) {
48 2
            $uniqueFilter = new UniqueFilter();
49 2
            yield from $uniqueFilter->apply($formatted);
50
        } else {
51 2
            yield from $formatted;
52
        }
53 4
    }
54
55
    /**
56
     * @param iterable<string> $words Words to format.
57
     * @return Traversable<string> The words formatted by all the word formatters in the formatter combiner.
58
     */
59 4
    private function applyFormatters(iterable $words): Traversable
60
    {
61 4
        if ($this->includeUnformatted) {
62 2
            yield from $words;
63
        }
64
65 4
        foreach ($this->wordFormatters as $wordFormatter) {
66 4
            yield from $wordFormatter->apply($words);
67
        }
68 4
    }
69
}
70