Combiner::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\Formatter;
6
7
use Stadly\PasswordPolice\CharTree;
8
use Stadly\PasswordPolice\Formatter;
9
10
final class Combiner implements Formatter
11
{
12
    use Chaining;
13
14
    /**
15
     * @var array<Formatter> Character tree formatters.
16
     */
17
    private $formatters;
18
19
    /**
20
     * @var bool Whether the result should also include the character tree unformatted.
21
     */
22
    private $includeUnformatted;
23
24
    /**
25
     * @param array<Formatter> $formatters Character tree formatters.
26
     * @param bool $includeUnformatted Whether the result should also include the character tree unformatted.
27
     */
28 3
    public function __construct(array $formatters, bool $includeUnformatted = true)
29
    {
30 3
        $this->formatters = $formatters;
31 3
        $this->includeUnformatted = $includeUnformatted;
32 3
    }
33
34
    /**
35
     * @param CharTree $charTree Character tree to format.
36
     * @return CharTree The character tree formatted by all the formatters in the combiner.
37
     */
38 3
    protected function applyCurrent(CharTree $charTree): CharTree
39
    {
40 3
        $charTrees = [];
41
42 3
        if ($this->includeUnformatted) {
43 1
            $charTrees[] = $charTree;
44
        }
45
46 3
        foreach ($this->formatters as $formatter) {
47 3
            $charTrees[] = $formatter->apply($charTree);
48
        }
49
50 3
        return CharTree::fromArray($charTrees);
51
    }
52
}
53