Passed
Push — master ( 66b95b...1192d3 )
by Mark
21:21 queued 10:15
created

Date   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 38
c 1
b 0
f 0
dl 0
loc 109
ccs 12
cts 12
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A mapFormatBlocks() 0 9 2
A format() 0 3 1
A __construct() 0 7 2
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard;
4
5
class Date extends DateTimeWizard
6
{
7
    /**
8
     * Year (4 digits), e.g. 2023.
9
     */
10
    public const YEAR_FULL = 'yyyy';
11
12
    /**
13
     * Year (last 2 digits), e.g. 23.
14
     */
15
    public const YEAR_SHORT = 'yy';
16
17
    public const MONTH_FIRST_LETTER = 'mmmmm';
18
    /**
19
     * Month name, long form, e.g. January.
20
     */
21
    public const MONTH_NAME_FULL = 'mmmm';
22
    /**
23
     * Month name, short form, e.g. Jan.
24
     */
25
    public const MONTH_NAME_SHORT = 'mmm';
26
    /**
27
     * Month number with a leading zero if required, e.g. 01.
28
     */
29
    public const MONTH_NUMBER_LONG = 'mm';
30
31
    /**
32
     * Month number without a leading zero, e.g. 1.
33
     */
34
    public const MONTH_NUMBER_SHORT = 'm';
35
36
    /**
37
     * Day of the week, full form, e.g. Tuesday.
38
     */
39
    public const WEEKDAY_NAME_LONG = 'dddd';
40
41
    /**
42
     * Day of the week, short form, e.g. Tue.
43
     */
44
    public const WEEKDAY_NAME_SHORT = 'ddd';
45
46
    /**
47
     * Day number with a leading zero, e.g. 03.
48
     */
49
    public const DAY_NUMBER_LONG = 'dd';
50
51
    /**
52
     * Day number without a leading zero, e.g. 3.
53
     */
54
    public const DAY_NUMBER_SHORT = 'd';
55
56
    protected const DATE_BLOCKS = [
57
        self::YEAR_FULL,
58
        self::YEAR_SHORT,
59
        self::MONTH_FIRST_LETTER,
60
        self::MONTH_NAME_FULL,
61
        self::MONTH_NAME_SHORT,
62
        self::MONTH_NUMBER_LONG,
63
        self::MONTH_NUMBER_SHORT,
64
        self::WEEKDAY_NAME_LONG,
65
        self::WEEKDAY_NAME_SHORT,
66
        self::DAY_NUMBER_LONG,
67
        self::DAY_NUMBER_SHORT,
68
    ];
69
70
    public const SEPARATOR_DASH = '-';
71
    public const SEPARATOR_DOT = '.';
72
    public const SEPARATOR_SLASH = '/';
73
    public const SEPARATOR_SPACE = ' ';
74
75
    /**
76
     * @var string[]
77
     */
78
    protected array $separators;
79
80
    /**
81
     * @var string[]
82
     */
83
    protected array $formatBlocks;
84
85
    /**
86
     * @param null|string|string[] $separators
87
     *        If you want to use the same separator for all format blocks, then it can be passed as a string literal;
88
     *           if you wish to use different separators, then they should be passed as an array.
89
     *        If you want to use only a single format block, then pass a null as the separator argument
90
     */
91 8
    public function __construct($separators, string ...$formatBlocks)
92
    {
93 8
        $this->separators = $this->padSeparatorArray(
94 8
            is_array($separators) ? $separators : [$separators],
95 8
            count($formatBlocks) - 1
96 8
        );
97 8
        $this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
98
    }
99
100 8
    private function mapFormatBlocks(string $value): string
101
    {
102
        // Any date masking codes are returned as lower case values
103 8
        if (in_array(mb_strtolower($value), self::DATE_BLOCKS, true)) {
104 8
            return mb_strtolower($value);
105
        }
106
107
        // Wrap any string literals in quotes, so that they're clearly defined as string literals
108 2
        return $this->wrapLiteral($value);
109
    }
110
111 10
    public function format(): string
112
    {
113 10
        return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
114
    }
115
}
116