|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Parser\ResultMapper; |
|
4
|
|
|
|
|
5
|
|
|
use Popy\Calendar\Parser\DateLexerResult; |
|
6
|
|
|
use Popy\Calendar\Parser\ResultMapperInterface; |
|
7
|
|
|
use Popy\Calendar\ValueObject\DateRepresentationInterface; |
|
8
|
|
|
use Popy\Calendar\ValueObject\DateTimeRepresentationInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Maps standard format time symbols to DateTimeRepresentationInterface fields. |
|
12
|
|
|
*/ |
|
13
|
|
|
class StandardDateTime implements ResultMapperInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @inheritDoc |
|
17
|
|
|
*/ |
|
18
|
|
|
public function map(DateLexerResult $parts, DateRepresentationInterface $date) |
|
19
|
|
|
{ |
|
20
|
|
|
if (!$date instanceof DateTimeRepresentationInterface) { |
|
21
|
|
|
return; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// g 12-hour format of an hour without leading zeros 1 through 12 |
|
25
|
|
|
// h 12-hour format of an hour with leading zeros 01 through 12 |
|
26
|
|
|
// G 24-hour format of an hour without leading zeros 0 through 23 |
|
27
|
|
|
// H 24-hour format of an hour with leading zeros 00 through 23 |
|
28
|
|
|
// i Minutes with leading zeros 00 to 59 |
|
29
|
|
|
// s Seconds, with leading zeros 00 through 59 |
|
30
|
|
|
// v Milliseconds |
|
31
|
|
|
// μ Microseconds (the u microseconds is used for SI microseconds) |
|
32
|
|
|
$time = $date |
|
33
|
|
|
->getTime() |
|
34
|
|
|
->withFragments([ |
|
35
|
|
|
$this->intIfNotNull($parts->getFirst('g', 'G', 'h', 'H')), |
|
36
|
|
|
$this->intIfNotNull($parts->get('i')), |
|
37
|
|
|
$this->intIfNotNull($parts->get('s')), |
|
38
|
|
|
$this->intIfNotNull($parts->get('v')), |
|
39
|
|
|
$this->intIfNotNull($parts->get('μ')), |
|
40
|
|
|
]) |
|
41
|
|
|
->withHalved(0, $this->determineAmPm($parts)) |
|
42
|
|
|
; |
|
43
|
|
|
|
|
44
|
|
|
// B Swatch Internet time 000 through 999 |
|
45
|
|
|
if (null !== $b = $parts->get('B')) { |
|
46
|
|
|
$time = $time->withTransversal(0, (int)$b); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
return $date->withTime($time); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function intIfNotNull($value) |
|
53
|
|
|
{ |
|
54
|
|
|
return $value === null ? null : (int)$value; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
protected function determineAmPm(DateLexerResult $parts) |
|
58
|
|
|
{ |
|
59
|
|
|
// a Lowercase Ante meridiem and Post meridiem am or pm |
|
60
|
|
|
// A Uppercase Ante meridiem and Post meridiem AM or PM |
|
61
|
|
|
if (null !== $a = $parts->getFirst('a', 'A')) { |
|
62
|
|
|
return (bool)$a; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|