1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Parser\ResultMapper; |
4
|
|
|
|
5
|
|
|
use Popy\Calendar\ValueObject\DateParts; |
6
|
|
|
use Popy\Calendar\Parser\DateLexerResult; |
7
|
|
|
use Popy\Calendar\Parser\ResultMapperInterface; |
8
|
|
|
use Popy\Calendar\ValueObject\DateRepresentationInterface; |
9
|
|
|
use Popy\Calendar\ValueObject\DateFragmentedRepresentationInterface; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Maps standard format month/day/week symbols to |
13
|
|
|
* DateFragmentedRepresentationInterface fields. |
14
|
|
|
*/ |
15
|
|
|
class StandardDateFragmented implements ResultMapperInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @inheritDoc |
19
|
|
|
*/ |
20
|
|
|
public function map(DateLexerResult $parts, DateRepresentationInterface $date) |
21
|
|
|
{ |
22
|
|
|
if (!$date instanceof DateFragmentedRepresentationInterface) { |
23
|
|
|
return; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$dateParts = $date |
27
|
|
|
->getDateParts() |
28
|
|
|
->withFragments([ |
29
|
|
|
$this->determineMonth($parts), |
30
|
|
|
$this->determineDay($parts), |
31
|
|
|
]) |
32
|
|
|
->withTransversals([ |
33
|
|
|
$parts->get('o') === null ? null : (int)$parts->get('o'), |
34
|
|
|
$this->determineWeekIndex($parts), |
35
|
|
|
$this->determineDayOfWeek($parts), |
36
|
|
|
]) |
37
|
|
|
; |
38
|
|
|
|
39
|
|
|
return $date->withDateParts($dateParts); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Determine month (0 indexed). |
44
|
|
|
* |
45
|
|
|
* @param DateLexerResult $parts |
46
|
|
|
* |
47
|
|
|
* @return integer |
48
|
|
|
*/ |
49
|
|
|
protected function determineMonth(DateLexerResult $parts) |
50
|
|
|
{ |
51
|
|
|
// m Numeric representation of a month, with leading zeros 01 through 12 |
52
|
|
|
// n Numeric representation of a month, without leading zeros |
53
|
|
|
if (null !== $m = $parts->getFirst('m', 'n')) { |
54
|
|
|
return (int)$m - 1; |
55
|
|
|
} |
56
|
|
|
// F A full textual representation of a month, such as January or March |
57
|
|
|
// M A short textual representation of a month, three letters |
58
|
|
|
if (null !== $m = $parts->getFirst('F', 'M')) { |
59
|
|
|
return (int)$m; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Determine day (0 indexed). |
65
|
|
|
* |
66
|
|
|
* @param DateLexerResult $parts |
67
|
|
|
* |
68
|
|
|
* @return integer|null |
69
|
|
|
*/ |
70
|
|
|
protected function determineDay(DateLexerResult $parts) |
71
|
|
|
{ |
72
|
|
|
// d Day of the month, 2 digits with leading zeros 01 to 31 |
73
|
|
|
// j Day of the month without leading zeros 1 to 31 |
74
|
|
|
if (null !== $d = $parts->getFirst('j', 'd')) { |
75
|
|
|
return (int)$d - 1; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Determine day of week (0 indexed, starts by monday) |
81
|
|
|
* |
82
|
|
|
* @param DateLexerResult $parts |
83
|
|
|
* |
84
|
|
|
* @return integer|null |
85
|
|
|
*/ |
86
|
|
|
protected function determineDayOfWeek(DateLexerResult $parts) |
87
|
|
|
{ |
88
|
|
|
// D A textual representation of a day, three letters |
89
|
|
|
// l (lowercase 'L') A full textual representation of the day of the week |
90
|
|
|
if (null !== $w = $parts->getFirst('D', 'l')) { |
91
|
|
|
return (int)$w; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday) |
95
|
|
|
if (null !== $w = $parts->get('N')) { |
96
|
|
|
return (int)$w - 1; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) |
100
|
|
|
if (null !== $w = $parts->get('w')) { |
101
|
|
|
return ((int)$w + 6) % 7; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Determine ISO week index from iso week number. |
107
|
|
|
* |
108
|
|
|
* @param DateLexerResult $parts |
109
|
|
|
* |
110
|
|
|
* @return integer|null |
111
|
|
|
*/ |
112
|
|
|
protected function determineWeekIndex(DateLexerResult $parts) |
113
|
|
|
{ |
114
|
|
|
if (null === $w = $parts->get('W')) { |
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return intval($w) - 1; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|