1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ValueFormatters; |
4
|
|
|
|
5
|
|
|
use DataValues\TimeValue; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Basic plain text formatter for TimeValue objects that either delegates formatting to an other |
10
|
|
|
* formatter given via OPT_TIME_ISO_FORMATTER or outputs the timestamp as it is. |
11
|
|
|
* |
12
|
|
|
* @since 0.1 |
13
|
|
|
* |
14
|
|
|
* @license GPL-2.0+ |
15
|
|
|
* @author H. Snater < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class TimeFormatter extends ValueFormatterBase { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @deprecated since 0.7.1, use TimeValue::CALENDAR_GREGORIAN instead |
21
|
|
|
*/ |
22
|
|
|
const CALENDAR_GREGORIAN = TimeValue::CALENDAR_GREGORIAN; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @deprecated since 0.7.1, use TimeValue::CALENDAR_JULIAN instead |
26
|
|
|
*/ |
27
|
|
|
const CALENDAR_JULIAN = TimeValue::CALENDAR_JULIAN; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Option to localize calendar models. Must contain an array mapping known calendar model URIs |
31
|
|
|
* to localized calendar model names. |
32
|
|
|
*/ |
33
|
|
|
const OPT_CALENDARNAMES = 'calendars'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Option for a custom timestamp formatter. Must contain an instance of a ValueFormatter |
37
|
|
|
* subclass, capable of formatting TimeValue objects. The output of the custom formatter is |
38
|
|
|
* threaded as plain text and passed through. |
39
|
|
|
*/ |
40
|
|
|
const OPT_TIME_ISO_FORMATTER = 'time iso formatter'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @see ValueFormatterBase::__construct |
44
|
|
|
* |
45
|
|
|
* @param FormatterOptions|null $options |
46
|
|
|
*/ |
47
|
8 |
|
public function __construct( FormatterOptions $options = null ) { |
48
|
8 |
|
parent::__construct( $options ); |
49
|
|
|
|
50
|
8 |
|
$this->defaultOption( self::OPT_CALENDARNAMES, array() ); |
51
|
8 |
|
$this->defaultOption( self::OPT_TIME_ISO_FORMATTER, null ); |
52
|
8 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @see ValueFormatter::format |
56
|
|
|
* |
57
|
|
|
* @param TimeValue $value |
58
|
|
|
* |
59
|
|
|
* @throws InvalidArgumentException |
60
|
|
|
* @return string Plain text |
61
|
|
|
*/ |
62
|
8 |
|
public function format( $value ) { |
63
|
8 |
|
if ( !( $value instanceof TimeValue ) ) { |
64
|
|
|
throw new InvalidArgumentException( 'Data value type mismatch. Expected a TimeValue.' ); |
65
|
|
|
} |
66
|
|
|
|
67
|
8 |
|
$formatted = $value->getTime(); |
68
|
|
|
|
69
|
8 |
|
$isoFormatter = $this->getOption( self::OPT_TIME_ISO_FORMATTER ); |
70
|
8 |
|
if ( $isoFormatter instanceof ValueFormatter ) { |
71
|
|
|
$formatted = $isoFormatter->format( $value ); |
72
|
|
|
} |
73
|
|
|
|
74
|
8 |
|
return $formatted; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |
78
|
|
|
|