1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Popy\Calendar\Formatter\SymbolFormatter; |
4
|
|
|
|
5
|
|
|
use Popy\Calendar\FormatterInterface; |
6
|
|
|
use Popy\Calendar\Parser\FormatToken; |
7
|
|
|
use Popy\Calendar\Formatter\LocalisationInterface; |
8
|
|
|
use Popy\Calendar\Formatter\SymbolFormatterInterface; |
9
|
|
|
use Popy\Calendar\ValueObject\DateRepresentationInterface; |
10
|
|
|
use Popy\Calendar\ValueObject\DateFragmentedRepresentationInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Standard format, handling DateFragmentedRepresentationInterface. |
14
|
|
|
* |
15
|
|
|
* Weeks and day names assume a gregorian calendar structure. |
16
|
|
|
*/ |
17
|
|
|
class StandardDateFragmented implements SymbolFormatterInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Locale (used for day & month names) |
21
|
|
|
* |
22
|
|
|
* @var LocalisationInterface |
23
|
|
|
*/ |
24
|
|
|
protected $locale; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Class constructor. |
28
|
|
|
* |
29
|
|
|
* @param LocalisationInterface $locale |
30
|
|
|
*/ |
31
|
|
|
public function __construct(LocalisationInterface $locale) |
32
|
|
|
{ |
33
|
|
|
$this->locale = $locale; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @inheritDoc |
38
|
|
|
*/ |
39
|
|
|
public function formatSymbol(DateRepresentationInterface $input, FormatToken $token, FormatterInterface $formatter) |
40
|
|
|
{ |
41
|
|
|
if (!$input instanceof DateFragmentedRepresentationInterface) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($token->is('F')) { |
46
|
|
|
// F A full textual representation of a month |
47
|
|
|
return (string)$this->locale->getMonthName($input->getDateParts()->get(0)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($token->is('M')) { |
51
|
|
|
// M A short textual representation of a month, three letters |
52
|
|
|
return (string)$this->locale->getMonthShortName($input->getDateParts()->get(0)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($token->is('m')) { |
56
|
|
|
// m Numeric representation of a month, with leading zeros |
57
|
|
|
return sprintf('%02d', $input->getDateParts()->get(0) + 1); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ($token->is('n')) { |
61
|
|
|
// n Numeric representation of a month, without leading zeros |
62
|
|
|
return $input->getDateParts()->get(0) + 1; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($token->is('t')) { |
66
|
|
|
// t Number of days in the given month |
67
|
|
|
return (int)$input->getDateParts()->getSize(0); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($token->is('d')) { |
71
|
|
|
// d Day of the month, 2 digits with leading zeros |
72
|
|
|
return sprintf('%02d', $input->getDateParts()->get(1) + 1); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if ($token->is('j')) { |
76
|
|
|
// j Day of the month without leading zeros |
77
|
|
|
return $input->getDateParts()->get(1) + 1; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if ($token->is('S')) { |
81
|
|
|
// S English ordinal suffix for the day of the month, 2 characters |
82
|
|
|
return (string)$this->locale->getNumberOrdinalSuffix($input->getDateParts()->get(1)); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($token->is('l')) { |
86
|
|
|
// l (lowercase 'L') A full textual representation of the day of the week |
87
|
|
|
return (string)$this->locale->getDayName($input->getDateParts()->getTransversal(2)); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ($token->is('D')) { |
91
|
|
|
// D A textual representation of a day, three letters |
92
|
|
|
return (string)$this->locale->getDayShortName($input->getDateParts()->getTransversal(2)); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if ($token->is('w')) { |
96
|
|
|
// w Numeric representation of the day of the week 0 (for Sunday) through 6 (for Saturday) |
97
|
|
|
return (1 + $input->getDateParts()->getTransversal(2)) % 7; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($token->is('N')) { |
101
|
|
|
// N ISO-8601 numeric representation of the day of the week (added in PHP 5.1.0) 1 (for Monday) through 7 (for Sunday) |
102
|
|
|
return $input->getDateParts()->getTransversal(2) + 1; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
if ($token->is('W')) { |
106
|
|
|
// W ISO-8601 week number of year, weeks starting on Monday |
107
|
|
|
return sprintf( |
108
|
|
|
'%02d', |
109
|
|
|
$input->getDateParts()->getTransversal(1) + 1 |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($token->is('o')) { |
114
|
|
|
// Y A full numeric representation of a year, 4 digits |
115
|
|
|
// o ISO-8601 week-numbering year. This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead. |
116
|
|
|
return sprintf('%04d', $input->getDateParts()->getTransversal(0)); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|