Passed
Push — master ( 6f90eb...36eb00 )
by Magnar Ovedal
03:27
created

DefaultFormatter::applyCurrent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\DateFormatter;
6
7
use DateTimeInterface;
8
use Traversable;
9
10
final class DefaultFormatter extends ChainableFormatter
11
{
12
    private const DATE_FORMATS = [
13
        // Year
14
        ['Y'], // 2018
15
16
        // Year month
17
        ['y', 'n'], // 18 8
18
        ['y', 'm'], // 18 08
19
        ['y', 'M'], // 18 Aug
20
        ['y', 'F'], // 18 August
21
22
        // Month year
23
        ['n', 'y'], // 8 18
24
        ['M', 'y'], // Aug 18
25
        ['F', 'y'], // August 18
26
27
        // Day month
28
        ['j', 'n'], // 4 8
29
        ['j', 'm'], // 4 08
30
        ['j', 'M'], // 4 Aug
31
        ['j', 'F'], // 4 August
32
33
        // Month day
34
        ['n', 'j'], // 8 4
35
        ['n', 'd'], // 8 04
36
        ['M', 'j'], // Aug 4
37
        ['M', 'd'], // Aug 04
38
        ['F', 'j'], // August 4
39
        ['F', 'd'], // August 04
40
    ];
41
42
    private const DATE_SEPARATORS = [
43
        '',
44
        '-',
45
        ' ',
46
        '/',
47
        '.',
48
        ',',
49
        '. ',
50
        ', ',
51
    ];
52
53
    /**
54
     * @var string[]
55
     */
56
    private $formats = [];
57
58 2
    public function __construct()
59
    {
60 2
        foreach (self::DATE_FORMATS as $format) {
61 2
            if (count($format) === 1) {
62 2
                $this->formats[] = reset($format);
63
            } else {
64 2
                foreach (self::DATE_SEPARATORS as $separator) {
65 2
                    $this->formats[] = implode($separator, $format);
66
                }
67
            }
68
        }
69 2
    }
70
71
    /**
72
     * @param iterable<DateTimeInterface> $dates Dates to format.
73
     * @return Traversable<string> The formatted dates.
74
     */
75 2
    protected function applyCurrent(iterable $dates): Traversable
76
    {
77 2
        foreach ($dates as $date) {
78 2
            foreach ($this->formats as $format) {
79 2
                yield $date->format($format);
80
            }
81
        }
82 2
    }
83
}
84