Capitalizer::__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 0
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\CharTree\Cutter;
9
use Stadly\PasswordPolice\Formatter;
10
11
final class Capitalizer implements Formatter
12
{
13
    use Chaining;
14
15
    /**
16
     * @var LowerCaseConverter Lower case converter.
17
     */
18
    private $lowerCaseConverter;
19
20
    /**
21
     * @var Cutter Character tree cutter for extracting the first character.
22
     */
23
    private $charExtractor;
24
25 7
    public function __construct()
26
    {
27 7
        $this->lowerCaseConverter = new LowerCaseConverter();
28 7
        $this->charExtractor = new Cutter();
29 7
    }
30
31
    /**
32
     * @param CharTree $charTree Character tree to format.
33
     * @return CharTree Capitalized variant of the character tree.
34
     */
35 7
    protected function applyCurrent(CharTree $charTree): CharTree
36
    {
37 7
        if ($charTree->getRoot() === null) {
38 1
            return $charTree;
39
        }
40
41 6
        $formatted = [];
42
43 6
        foreach ($this->charExtractor->cut($charTree, 1) as [$root, $tree]) {
44 5
            assert(is_string($root));
45 5
            assert(is_object($tree));
46
47 5
            $branches = [$this->lowerCaseConverter->apply($tree)];
48 5
            $formatted[] = CharTree::fromString(mb_strtoupper($root), $branches);
49
        }
50
51 6
        return CharTree::fromString('', $formatted);
52
    }
53
}
54