1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Style\NumberFormat\Wizard; |
4
|
|
|
|
5
|
|
|
class Time extends DateTimeWizard |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Hours without a leading zero, e.g. 9. |
9
|
|
|
*/ |
10
|
|
|
public const HOURS_SHORT = 'h'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Hours with a leading zero, e.g. 09. |
14
|
|
|
*/ |
15
|
|
|
public const HOURS_LONG = 'hh'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Minutes without a leading zero, e.g. 5. |
19
|
|
|
*/ |
20
|
|
|
public const MINUTES_SHORT = 'm'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Minutes with a leading zero, e.g. 05. |
24
|
|
|
*/ |
25
|
|
|
public const MINUTES_LONG = 'mm'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Seconds without a leading zero, e.g. 2. |
29
|
|
|
*/ |
30
|
|
|
public const SECONDS_SHORT = 's'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Seconds with a leading zero, e.g. 02. |
34
|
|
|
*/ |
35
|
|
|
public const SECONDS_LONG = 'ss'; |
36
|
|
|
|
37
|
|
|
public const MORNING_AFTERNOON = 'AM/PM'; |
38
|
|
|
|
39
|
|
|
protected const TIME_BLOCKS = [ |
40
|
|
|
self::HOURS_LONG, |
41
|
|
|
self::HOURS_SHORT, |
42
|
|
|
self::MINUTES_LONG, |
43
|
|
|
self::MINUTES_SHORT, |
44
|
|
|
self::SECONDS_LONG, |
45
|
|
|
self::SECONDS_SHORT, |
46
|
|
|
self::MORNING_AFTERNOON, |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
public const SEPARATOR_COLON = ':'; |
50
|
|
|
public const SEPARATOR_SPACE = ' '; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string[] |
54
|
|
|
*/ |
55
|
|
|
protected array $separators; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string[] |
59
|
|
|
*/ |
60
|
|
|
protected array $formatBlocks; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param null|string|string[] $separators |
64
|
|
|
* If you want to use the same separator for all format blocks, then it can be passed as a string literal; |
65
|
|
|
* if you wish to use different separators, then they should be passed as an array. |
66
|
|
|
* If you want to use only a single format block, then pass a null as the separator argument |
67
|
|
|
*/ |
68
|
4 |
|
public function __construct($separators, string ...$formatBlocks) |
69
|
|
|
{ |
70
|
4 |
|
$this->separators = $this->padSeparatorArray( |
71
|
4 |
|
is_array($separators) ? $separators : [$separators], |
72
|
4 |
|
count($formatBlocks) - 1 |
73
|
4 |
|
); |
74
|
4 |
|
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks); |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
private function mapFormatBlocks(string $value): string |
78
|
|
|
{ |
79
|
|
|
// Any date masking codes are returned as lower case values |
80
|
|
|
// except for AM/PM, which is set to uppercase |
81
|
4 |
|
if (in_array(mb_strtolower($value), self::TIME_BLOCKS, true)) { |
82
|
4 |
|
return mb_strtolower($value); |
83
|
2 |
|
} elseif (mb_strtoupper($value) === self::MORNING_AFTERNOON) { |
84
|
1 |
|
return mb_strtoupper($value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
// Wrap any string literals in quotes, so that they're clearly defined as string literals |
88
|
1 |
|
return $this->wrapLiteral($value); |
89
|
|
|
} |
90
|
|
|
|
91
|
6 |
|
public function format(): string |
92
|
|
|
{ |
93
|
6 |
|
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators)); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|