| 1 |
<?php |
| 2 |
|
| 3 |
namespace TablePress\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_NONBREAKING = "\u{a0}"; |
| 51 |
public const SEPARATOR_SPACE = ' '; |
| 52 |
|
| 53 |
protected const TIME_DEFAULT = [ |
| 54 |
self::HOURS_LONG, |
| 55 |
self::MINUTES_LONG, |
| 56 |
self::SECONDS_LONG, |
| 57 |
]; |
| 58 |
|
| 59 |
/** |
| 60 |
* @var array<?string> |
| 61 |
*/ |
| 62 |
protected array $separators; |
| 63 |
|
| 64 |
/** |
| 65 |
* @var string[] |
| 66 |
*/ |
| 67 |
protected array $formatBlocks; |
| 68 |
|
| 69 |
/** |
| 70 |
* @param null|string|string[] $separators |
| 71 |
* If you want to use the same separator for all format blocks, then it can be passed as a string literal; |
| 72 |
* if you wish to use different separators, then they should be passed as an array. |
| 73 |
* If you want to use only a single format block, then pass a null as the separator argument |
| 74 |
*/ |
| 75 |
public function __construct($separators = self::SEPARATOR_COLON, string ...$formatBlocks) |
| 76 |
{ |
| 77 |
$separators ??= self::SEPARATOR_COLON; |
| 78 |
$formatBlocks = (count($formatBlocks) === 0) ? self::TIME_DEFAULT : $formatBlocks; |
| 79 |
|
| 80 |
$this->separators = $this->padSeparatorArray( |
| 81 |
is_array($separators) ? $separators : [$separators], |
| 82 |
count($formatBlocks) - 1 |
| 83 |
); |
| 84 |
$this->formatBlocks = array_map([$this, 'mapFormatBlocks'], $formatBlocks); |
| 85 |
} |
| 86 |
|
| 87 |
private function mapFormatBlocks(string $value): string |
| 88 |
{ |
| 89 |
// Any date masking codes are returned as lower case values |
| 90 |
// except for AM/PM, which is set to uppercase |
| 91 |
if (in_array(mb_strtolower($value), self::TIME_BLOCKS, true)) { |
| 92 |
return mb_strtolower($value); |
| 93 |
} elseif (mb_strtoupper($value) === self::MORNING_AFTERNOON) { |
| 94 |
return mb_strtoupper($value); |
| 95 |
} |
| 96 |
|
| 97 |
// Wrap any string literals in quotes, so that they're clearly defined as string literals |
| 98 |
return $this->wrapLiteral($value); |
| 99 |
} |
| 100 |
|
| 101 |
public function format(): string |
| 102 |
{ |
| 103 |
return implode('', array_map([$this, 'intersperse'], $this->formatBlocks, $this->separators)); |
| 104 |
} |
| 105 |
} |
| 106 |
|