1441 |
ariadna |
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_NONBREAKING = "\u{a0}";
|
|
|
74 |
public const SEPARATOR_SPACE = ' ';
|
|
|
75 |
|
|
|
76 |
protected const DATE_DEFAULT = [
|
|
|
77 |
self::YEAR_FULL,
|
|
|
78 |
self::MONTH_NUMBER_LONG,
|
|
|
79 |
self::DAY_NUMBER_LONG,
|
|
|
80 |
];
|
|
|
81 |
|
|
|
82 |
/**
|
|
|
83 |
* @var string[]
|
|
|
84 |
*/
|
|
|
85 |
protected array $separators;
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* @var string[]
|
|
|
89 |
*/
|
|
|
90 |
protected array $formatBlocks;
|
|
|
91 |
|
|
|
92 |
/**
|
|
|
93 |
* @param null|string|string[] $separators
|
|
|
94 |
* If you want to use the same separator for all format blocks, then it can be passed as a string literal;
|
|
|
95 |
* if you wish to use different separators, then they should be passed as an array.
|
|
|
96 |
* If you want to use only a single format block, then pass a null as the separator argument
|
|
|
97 |
*/
|
|
|
98 |
public function __construct($separators = self::SEPARATOR_DASH, string ...$formatBlocks)
|
|
|
99 |
{
|
|
|
100 |
$separators ??= self::SEPARATOR_DASH;
|
|
|
101 |
$formatBlocks = (count($formatBlocks) === 0) ? self::DATE_DEFAULT : $formatBlocks;
|
|
|
102 |
|
|
|
103 |
$this->separators = $this->padSeparatorArray(
|
|
|
104 |
is_array($separators) ? $separators : [$separators],
|
|
|
105 |
count($formatBlocks) - 1
|
|
|
106 |
);
|
|
|
107 |
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks);
|
|
|
108 |
}
|
|
|
109 |
|
|
|
110 |
private function mapFormatBlocks(string $value): string
|
|
|
111 |
{
|
|
|
112 |
// Any date masking codes are returned as lower case values
|
|
|
113 |
if (in_array(mb_strtolower($value), self::DATE_BLOCKS, true)) {
|
|
|
114 |
return mb_strtolower($value);
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
// Wrap any string literals in quotes, so that they're clearly defined as string literals
|
|
|
118 |
return $this->wrapLiteral($value);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public function format(): string
|
|
|
122 |
{
|
|
|
123 |
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators));
|
|
|
124 |
}
|
|
|
125 |
}
|