Completed
Push — master ( 5e2eb6...86efd8 )
by Magnar Ovedal
34:31 queued 29:45
created

MixedCaseMap::__construct()   A

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 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
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\CodeMap;
6
7
use Stadly\PasswordPolice\CharTree;
8
use Stadly\PasswordPolice\CodeMap;
9
use Stadly\PasswordPolice\Formatter\LengthFilter;
10
use Stadly\PasswordPolice\Formatter\Truncator;
11
12
final class MixedCaseMap implements CodeMap
13
{
14
    /**
15
     * @var Truncator Formatter for extracting the first character.
16
     */
17
    private $charExtractor;
18
19 11
    public function __construct()
20
    {
21 11
        $this->charExtractor = new Truncator(1);
22 11
        $this->charExtractor->setNext(new LengthFilter(1, 1));
23 11
    }
24
25
    /**
26
     * {@inheritDoc}
27
     */
28 3
    public function getMap(CharTree $charTree): array
29
    {
30 3
        $codeMap = [];
31 3
        foreach ($this->charExtractor->apply($charTree) as $char) {
32 1
            $lowerCase = mb_strtolower($char);
33 1
            $upperCase = mb_strtoupper($char);
34
35 1
            $codeMap[$char] = [$lowerCase];
36
37 1
            if ($lowerCase !== $upperCase) {
38 1
                $codeMap[$char][] = $upperCase;
39
            }
40
        }
41 3
        return $codeMap;
42
    }
43
44
    /**
45
     * {@inheritDoc}
46
     */
47 1
    public function getLengths(): array
48
    {
49 1
        return [1];
50
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 7
    public function code(string $string): array
56
    {
57 7
        $lowerCase = mb_strtolower($string);
58 7
        $upperCase = mb_strtoupper($string);
59
60 7
        $codeMap = [$lowerCase];
61 7
        if ($lowerCase !== $upperCase) {
62 5
            $codeMap[] = $upperCase;
63
        }
64
65 7
        return $codeMap;
66
    }
67
}
68