1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW; |
4
|
|
|
|
5
|
|
|
use DateTimeZone; |
6
|
|
|
use SMWDITime as DITime; |
7
|
|
|
use Language; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @license GNU GPL v2+ |
11
|
|
|
* @since 2.4 |
12
|
|
|
* |
13
|
|
|
* @author mwjames |
14
|
|
|
*/ |
15
|
|
|
class IntlTimeFormatter { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var DITime |
19
|
|
|
*/ |
20
|
|
|
private $dataItem; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Language |
24
|
|
|
*/ |
25
|
|
|
private $language; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @since 2.4 |
29
|
|
|
* |
30
|
|
|
* @param DITime $dataItem |
31
|
|
|
* @param Language|null $language |
32
|
|
|
*/ |
33
|
|
|
public function __construct( DITime $dataItem, Language $language = null ) { |
34
|
|
|
$this->dataItem = $dataItem; |
35
|
|
|
$this->language = $language; |
36
|
|
|
|
37
|
|
|
if ( $this->language === null ) { |
38
|
|
|
$this->language = Localizer::getInstance()->getContentLanguage(); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Permitted formatting options are specified by http://php.net/manual/en/function.date.php |
44
|
|
|
* |
45
|
|
|
* @since 2.4 |
46
|
|
|
* |
47
|
|
|
* @param string $format |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function format( $format ) { |
52
|
|
|
|
53
|
|
|
$dateTime = $this->dataItem->asDateTime(); |
54
|
|
|
|
55
|
|
|
if ( !$dateTime ) { |
56
|
|
|
return false; |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$output = $this->getFormattedOutputWithTextualRepresentationReplacement( |
60
|
|
|
$dateTime, |
61
|
|
|
$format |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
if ( $this->dataItem->getCalendarModel() !== DITime::CM_GREGORIAN && $this->containsDateFormatRule( $format ) ) { |
65
|
|
|
$output .= ' ' . $this->dataItem->getCalendarModelLiteral(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $output; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function containsDateFormatRule( $format ) { |
72
|
|
|
|
73
|
|
|
foreach ( str_split( $format ) as $value ) { |
74
|
|
|
if ( in_array( $value, array( 'd', 'D', 'j', 'l', 'N', 'w', 'W', 'F', 'M', 'm', 'n', 't', 'L', 'o', 'Y', 'y', "c", 'r' ) ) ) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* DateTime generally outputs English textual representation |
84
|
|
|
* |
85
|
|
|
* - D A textual representation of a day, three letters |
86
|
|
|
* - l (lowercase 'L'), A full textual representation of the day of the week |
87
|
|
|
* - F A full textual representation of a month, such as January or March |
88
|
|
|
* - M A short textual representation of a month, three letters |
89
|
|
|
* - a Lowercase Ante meridiem and Post meridiem am or pm |
90
|
|
|
* - A Uppercase Ante meridiem and Post meridiem |
91
|
|
|
*/ |
92
|
|
|
private function getFormattedOutputWithTextualRepresentationReplacement( $dateTime, $format ) { |
93
|
|
|
|
94
|
|
|
$output = $dateTime->format( $format ); |
95
|
|
|
|
96
|
|
|
$monthNumber = $dateTime->format( 'n' ); |
97
|
|
|
$dayNumber = $dateTime->format( 'N' ); |
98
|
|
|
|
99
|
|
|
if ( strpos( $format, 'F' ) !== false ) { |
100
|
|
|
$output = str_replace( |
101
|
|
|
$dateTime->format( 'F' ), |
102
|
|
|
$this->language->getMonthName( $monthNumber ), |
103
|
|
|
$output |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ( strpos( $format, 'M' ) !== false ) { |
108
|
|
|
$output = str_replace( |
109
|
|
|
$dateTime->format( 'M' ), |
110
|
|
|
$this->language->getMonthAbbreviation( $monthNumber ), |
111
|
|
|
$output |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
if ( strpos( $format, 'l' ) !== false ) { |
116
|
|
|
$output = str_replace( |
117
|
|
|
$dateTime->format( 'l' ), |
118
|
|
|
$this->language->getWeekdayName( $dayNumber ), |
119
|
|
|
$output |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ( strpos( $format, 'D' ) !== false ) { |
124
|
|
|
$output = str_replace( |
125
|
|
|
$dateTime->format( 'D' ), |
126
|
|
|
$this->language->getWeekdayAbbreviation( $dayNumber ), |
127
|
|
|
$output |
128
|
|
|
); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $output; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.