|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Andegna\Ethiopian; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Trait DateFormatter. |
|
9
|
|
|
* |
|
10
|
|
|
* This trait deals with the date time formatting issue |
|
11
|
|
|
*/ |
|
12
|
|
|
trait DateFormatter |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* I got the Magic :) call. |
|
16
|
|
|
* |
|
17
|
|
|
* @param $name |
|
18
|
|
|
* @param $arguments |
|
19
|
|
|
* |
|
20
|
|
|
* @return int|string |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __call($name, $arguments) |
|
23
|
|
|
{ |
|
24
|
|
|
$field = $this->extractCalledField($name); |
|
25
|
|
|
|
|
26
|
|
|
if (property_exists($this, $field)) { |
|
27
|
|
|
return $this->{$field}; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (array_key_exists($field, $formats = [ |
|
31
|
|
|
'hour' => 'G', |
|
32
|
|
|
'minute' => 'i', |
|
33
|
|
|
'second' => 's', |
|
34
|
|
|
'micro' => 'u', |
|
35
|
|
|
'dayOfWeek' => 'N', |
|
36
|
|
|
'timestamp' => 'U', ])) { |
|
37
|
|
|
return (int) $this->format($formats[$field]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
return $this->format($field); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Extract the name of the getter function. |
|
45
|
|
|
* |
|
46
|
|
|
* eg: given 'getDayOfWeek' return 'dayOfWeek' |
|
47
|
|
|
* given 'isFinished' return 'finished' |
|
48
|
|
|
* |
|
49
|
|
|
* @param $name |
|
50
|
|
|
* |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function extractCalledField($name) |
|
54
|
|
|
{ |
|
55
|
|
|
if (strpos($name, 'get') === 0) { |
|
56
|
|
|
return lcfirst(substr($name, 3)); |
|
57
|
|
|
} elseif (strpos($name, 'is') === 0) { |
|
58
|
|
|
return lcfirst(substr($name, 2)); |
|
59
|
|
|
} else { |
|
60
|
|
|
throw new InvalidArgumentException(sprintf("Unknown getter '%s'", $name)); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Accepts the same format as the date() function. |
|
66
|
|
|
* |
|
67
|
|
|
* @see date(), \DateTime |
|
68
|
|
|
* |
|
69
|
|
|
* @param $format |
|
70
|
|
|
* |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function format($format) |
|
74
|
|
|
{ |
|
75
|
|
|
$result = ''; |
|
76
|
|
|
/** @var int $length */ |
|
77
|
|
|
$length = mb_strlen($format); |
|
78
|
|
|
|
|
79
|
|
|
// iterate for each character |
|
80
|
|
|
for ($index = 0; $index < $length; $index++) { |
|
81
|
|
|
$result .= $this->getValueOfFormatCharacter($format[$index]); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $result; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return the value of the format character. |
|
89
|
|
|
* |
|
90
|
|
|
* @param string $name of the field |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function getValueOfFormatCharacter($name) |
|
95
|
|
|
{ |
|
96
|
|
|
switch ($name) { |
|
97
|
|
|
// (01-30) Day of the month, 2 digits with leading zeros |
|
98
|
|
|
case 'd': |
|
99
|
|
|
$d = $this->getValueOfFormatCharacter('j'); |
|
100
|
|
|
|
|
101
|
|
|
if (strlen($d) == 1) { |
|
102
|
|
|
return "0$d"; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
return $d; |
|
106
|
|
|
|
|
107
|
|
|
// (ሰኞ-እሑ) A textual representation of a day, two letters |
|
108
|
|
|
case 'D': |
|
109
|
|
|
$week = $this->getValueOfFormatCharacter('l'); |
|
110
|
|
|
|
|
111
|
|
|
return mb_substr($week, 0, 2, 'UTF-8'); |
|
112
|
|
|
|
|
113
|
|
|
// (1-30) Day of the month without leading zeros |
|
114
|
|
|
case 'j': |
|
115
|
|
|
return "{$this->getDay()}"; |
|
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
// (ሰኞ-እሑድ) A full textual representation of the day of the week |
|
118
|
|
|
case 'l': // l (lowercase 'L') |
|
|
|
|
|
|
119
|
|
|
return DateConstant::$WEEK_NAME_AM[$this->getDayOfWeek()]; |
|
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
// This should be the "English" ordinal suffix for the day of the month |
|
122
|
|
|
// 2 characters so am gonna just return the letter 'ኛ' |
|
123
|
|
|
case 'S': |
|
124
|
|
|
return 'ኛ'; |
|
125
|
|
|
|
|
126
|
|
|
// (0-365) The day of the year (starting from 0) |
|
127
|
|
|
case 'z': |
|
128
|
|
|
return "{$this->getDayOfYear()}"; |
|
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
// ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0) |
|
131
|
|
|
case 'W': |
|
132
|
|
|
return "{$this->getIsoWeekNumber()}"; |
|
|
|
|
|
|
133
|
|
|
|
|
134
|
|
|
// (መስከረም-ጳጉሜን) A full textual representation of a month |
|
135
|
|
|
case 'F': |
|
136
|
|
|
return DateConstant::$MONTHS_NAME[$this->getMonth()]; |
|
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
// (01-13) Numeric representation of a month, with leading zeros |
|
139
|
|
|
case 'm': |
|
140
|
|
|
$n = $this->getValueOfFormatCharacter('n'); |
|
141
|
|
|
|
|
142
|
|
|
return (strlen($n) == 1) ? "0$n" : "$n"; |
|
143
|
|
|
|
|
144
|
|
|
// (መስ - ጳጉ) A short textual representation of a month, two letters |
|
145
|
|
|
case 'M': |
|
146
|
|
|
$F = $this->getValueOfFormatCharacter('F'); |
|
147
|
|
|
|
|
148
|
|
|
return mb_substr($F, 0, 2, 'UTF-8'); |
|
149
|
|
|
|
|
150
|
|
|
// (1-13) Numeric representation of a month, without leading zeros |
|
151
|
|
|
case 'n': |
|
152
|
|
|
return "{$this->getMonth()}"; |
|
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
// (5,6 or 30) Number of days in the given month |
|
155
|
|
|
case 't': |
|
156
|
|
|
return "{$this->getDaysInMonth()}"; |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
// (1 or 0) Whether it's a leap year |
|
159
|
|
|
case 'L': |
|
160
|
|
|
return $this->isLeapYear() ? '1' : '0'; |
|
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
// ISO-8601 year number. This has the same value as Y, except that if |
|
163
|
|
|
// the ISO week number (W) belongs to the previous or next year, |
|
164
|
|
|
// that year is used instead. (added in PHP 5.1.0) |
|
165
|
|
|
case 'o': |
|
166
|
|
|
// TODO: Research if the ISO 8601 week number apply for our calender |
|
167
|
|
|
return "{$this->getIsoYearNumber()}"; |
|
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
// (1000-9999) A full numeric representation of a year, 4 digits mostly |
|
170
|
|
|
case 'Y': |
|
171
|
|
|
return "{$this->getYear()}"; |
|
|
|
|
|
|
172
|
|
|
|
|
173
|
|
|
// (00-99) A two digit representation of a year |
|
174
|
|
|
case 'y': |
|
175
|
|
|
$Y = $this->getValueOfFormatCharacter('Y'); |
|
176
|
|
|
|
|
177
|
|
|
return mb_substr($Y, strlen($Y) - 2, 2); |
|
178
|
|
|
|
|
179
|
|
|
// (እኩለ፡ሌሊት-ምሽት) |
|
180
|
|
|
// It suppose to format 'Ante meridiem' and 'Post meridiem' |
|
181
|
|
|
// But we Ethiopians classify the day in <b>ten</b> parts |
|
182
|
|
|
// and we don't have Uppercase and Lowercase letters |
|
183
|
|
|
case 'a': // Lowercase |
|
184
|
|
|
case 'A': // Uppercase |
|
185
|
|
|
return $this->getTimeOfDay(); |
|
186
|
|
|
|
|
187
|
|
|
// (Y-m-d\TH:i:sO) as of The ISO 8601 standard |
|
188
|
|
|
case 'c': |
|
189
|
|
|
return $this->format(DATE_ISO8601); |
|
190
|
|
|
|
|
191
|
|
|
// (D, d M Y H:i:s O) as of The RFC 2822 standard |
|
192
|
|
|
case 'r': |
|
193
|
|
|
return $this->format(DATE_RFC2822); |
|
194
|
|
|
|
|
195
|
|
|
// if the format character is not defined here |
|
196
|
|
|
// implies it doesn't depend on calender like 'hour' |
|
197
|
|
|
// or it is not a format character like '-' |
|
198
|
|
|
default: |
|
199
|
|
|
return $this->dateTime->format($name); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Return the amharic equivalent of AM & PM. |
|
205
|
|
|
* |
|
206
|
|
|
* @link http://web.archive.org/web/20140331152859/http://ethiopic.org/Calendars/ |
|
207
|
|
|
* |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
|
|
public function getTimeOfDay() |
|
211
|
|
|
{ |
|
212
|
|
|
switch ($this->getHour()) { |
|
|
|
|
|
|
213
|
|
|
case 23: |
|
214
|
|
|
case 0: |
|
215
|
|
|
return 'እኩለ፡ሌሊት'; |
|
216
|
|
|
case 1: |
|
217
|
|
|
case 2: |
|
218
|
|
|
case 3: |
|
219
|
|
|
return 'ውደቀት'; |
|
220
|
|
|
case 4: |
|
221
|
|
|
case 5: |
|
222
|
|
|
return 'ንጋት'; |
|
223
|
|
|
case 6: |
|
224
|
|
|
case 7: |
|
225
|
|
|
case 8: |
|
226
|
|
|
return 'ጡዋት'; |
|
227
|
|
|
case 9: |
|
228
|
|
|
case 10: |
|
229
|
|
|
case 11: |
|
230
|
|
|
return 'ረፋድ'; |
|
231
|
|
|
case 12: |
|
232
|
|
|
return 'እኩለ፡ቀን'; |
|
233
|
|
|
case 13: |
|
234
|
|
|
case 14: |
|
235
|
|
|
case 15: |
|
236
|
|
|
return 'ከሰዓት፡በኋላ'; |
|
237
|
|
|
case 16: |
|
238
|
|
|
case 17: |
|
239
|
|
|
return 'ወደማታ'; |
|
240
|
|
|
case 18: |
|
241
|
|
|
case 19: |
|
242
|
|
|
return 'ሲደነግዝ'; |
|
243
|
|
|
case 20: |
|
244
|
|
|
case 21: |
|
245
|
|
|
case 22: |
|
246
|
|
|
return 'ምሽት'; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
// We shouldn't get to this :( |
|
250
|
|
|
// Something must been wrong |
|
251
|
|
|
|
|
252
|
|
|
// Lets just keep quite about it :) |
|
253
|
|
|
return ''; |
|
254
|
|
|
} |
|
255
|
|
|
} |
|
256
|
|
|
|
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: