DefaultFormatter::__construct()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 6
c 1
b 0
f 0
nc 6
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 6
rs 9.2222
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\DateFormatter;
6
7
use DateTimeInterface;
8
use Stadly\PasswordPolice\CharTree;
9
use Stadly\PasswordPolice\DateFormatter;
10
11
final class DefaultFormatter implements DateFormatter
12
{
13
    use Chaining;
14
15
    private const FORMATS = [
16
        // Year month day
17
        [['Y', 'y'], ['m'], ['d']], // 2018-08-04
18
        [['Y', 'y'], ['n'], ['j']], // 2018-8-4
19
20
        // Month day year
21
        [['m'], ['d'], ['Y', 'y']], // 08-04-2018
22
        [['n'], ['j'], ['Y', 'y']], // 8-4-2018
23
24
        // Day month year
25
        [['d'], ['m'], ['Y', 'y']], // 04-08-2018
26
        [['j'], ['n'], ['Y', 'y']], // 4-8-2018
27
    ];
28
29
    private const SEPARATORS = [
30
        '',
31
        ' ',
32
        '-',
33
        '/',
34
        '.',
35
    ];
36
37
    /**
38
     * @var array<string>
39
     */
40
    private $formats = [];
41
42 2
    public function __construct()
43
    {
44 2
        foreach (self::FORMATS as [$parts1, $parts2, $parts3]) {
45 2
            foreach ($parts1 as $part1) {
46 2
                foreach ($parts2 as $part2) {
47 2
                    foreach ($parts3 as $part3) {
48 2
                        foreach (self::SEPARATORS as $separator) {
49 2
                            $this->formats[] = $part1 . $separator . $part2 . $separator . $part3;
50
                        }
51
                    }
52
                }
53
            }
54
        }
55 2
    }
56
57
    /**
58
     * @param iterable<DateTimeInterface> $dates Dates to format.
59
     * @return CharTree The formatted dates.
60
     */
61 2
    protected function applyCurrent(iterable $dates): CharTree
62
    {
63 2
        $charTrees = [];
64
65 2
        foreach ($dates as $date) {
66 2
            foreach ($this->formats as $format) {
67 2
                $charTrees[] = CharTree::fromString($date->format($format));
68
            }
69
        }
70
71 2
        return CharTree::fromArray($charTrees);
72
    }
73
}
74