|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PPP\Wikidata\ValueFormatters; |
|
4
|
|
|
|
|
5
|
|
|
use DataValues\TimeValue; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use ValueFormatters\ValueFormatterBase; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @licence AGPLv3+ |
|
11
|
|
|
* @author Thomas Pellissier Tanon |
|
12
|
|
|
* @todo Move to data-value/Time |
|
13
|
|
|
*/ |
|
14
|
|
|
class IsoTimeFormatter extends ValueFormatterBase { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @see ValueFormatter::format |
|
18
|
|
|
*/ |
|
19
|
9 |
|
public function format($value) { |
|
20
|
9 |
|
if(!($value instanceof TimeValue)) { |
|
21
|
|
|
throw new InvalidArgumentException('DataValue is not a TimeValue.'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
9 |
|
return $this->simplifyIsoTime($value); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
9 |
|
private function simplifyIsoTime(TimeValue $value) { |
|
28
|
9 |
|
$parts = $this->explodeISOTime($value->getTime()); |
|
29
|
9 |
|
$precision = $value->getPrecision(); |
|
30
|
|
|
|
|
31
|
9 |
|
$iso = ''; |
|
32
|
9 |
|
if($parts['year'] < 0) { |
|
33
|
1 |
|
$iso .= '-' . $this->toStringOfSize(-1 * $parts['year'], 4); |
|
34
|
1 |
|
} else { |
|
35
|
8 |
|
$iso .= $this->toStringOfSize($parts['year'], 4); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
//Month |
|
39
|
9 |
|
if($precision < TimeValue::PRECISION_MONTH) { |
|
40
|
1 |
|
return $iso; |
|
41
|
|
|
} |
|
42
|
8 |
|
$iso .= '-' . $this->toStringOfSize($parts['month'], 2); |
|
43
|
|
|
|
|
44
|
|
|
//Day |
|
45
|
8 |
|
if($precision < TimeValue::PRECISION_DAY) { |
|
46
|
1 |
|
return $iso; |
|
47
|
|
|
} |
|
48
|
7 |
|
$iso .= '-' . $this->toStringOfSize($parts['day'], 2); |
|
49
|
|
|
|
|
50
|
|
|
//Hours |
|
51
|
7 |
|
if($precision < TimeValue::PRECISION_HOUR) { |
|
52
|
3 |
|
return $iso; |
|
53
|
|
|
} |
|
54
|
4 |
|
$iso .= 'T' . $this->toStringOfSize($parts['hour'], 2); |
|
55
|
|
|
|
|
56
|
|
|
//Minutes |
|
57
|
4 |
|
if($precision < TimeValue::PRECISION_MINUTE) { |
|
58
|
1 |
|
return $iso . $this->timezoneToIso($value->getTimezone()); |
|
59
|
|
|
} |
|
60
|
3 |
|
$iso .= ':' . $this->toStringOfSize($parts['minute'], 2); |
|
61
|
|
|
|
|
62
|
|
|
//Seconds |
|
63
|
3 |
|
if($precision < TimeValue::PRECISION_SECOND) { |
|
64
|
1 |
|
return $iso . $this->timezoneToIso($value->getTimezone()); |
|
65
|
|
|
} |
|
66
|
2 |
|
$iso .= ':' . $this->toStringOfSize($parts['second'], 2); |
|
67
|
|
|
|
|
68
|
2 |
|
return $iso . $this->timezoneToIso($value->getTimezone()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
9 |
|
private function toStringOfSize($number, $size) { |
|
72
|
9 |
|
return str_pad((int) $number, $size, '0', STR_PAD_LEFT); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
4 |
|
private function timezoneToIso($timezone) { |
|
76
|
4 |
|
if($timezone === 0) { |
|
77
|
|
|
return 'Z'; |
|
78
|
4 |
|
} elseif($timezone > 0) { |
|
79
|
3 |
|
return '+' . $this->toStringOfSize($timezone / 60, 2) . ':' . $this->toStringOfSize($timezone % 60, 2); |
|
80
|
|
|
} else { |
|
81
|
1 |
|
return '-' . $this->toStringOfSize((-1 * $timezone) / 60, 2) . ':' . $this->toStringOfSize((-1 * $timezone) % 60, 2); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
9 |
|
private function explodeISOTime($time) { |
|
86
|
9 |
|
if(!preg_match('/([+-]\d+)-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/', $time, $m)) { |
|
87
|
|
|
throw new InvalidArgumentException('$time is not a valid time'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return array( |
|
91
|
9 |
|
'year' => intval($m[1]), |
|
92
|
9 |
|
'month' => intval($m[2]), |
|
93
|
9 |
|
'day' => intval($m[3]), |
|
94
|
9 |
|
'hour' => intval($m[4]), |
|
95
|
9 |
|
'minute' => intval($m[5]), |
|
96
|
9 |
|
'second' => intval($m[6]) |
|
97
|
9 |
|
); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|