|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* citeproc-php |
|
4
|
|
|
* |
|
5
|
|
|
* @link http://github.com/seboettg/citeproc-php for the source repository |
|
6
|
|
|
* @copyright Copyright (c) 2016 Sebastian Böttger. |
|
7
|
|
|
* @license https://opensource.org/licenses/MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Seboettg\CiteProc\Util; |
|
11
|
|
|
use Seboettg\CiteProc\Exception\CiteProcException; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Date |
|
16
|
|
|
* |
|
17
|
|
|
* Just a helper class for date issues |
|
18
|
|
|
* |
|
19
|
|
|
* @package Seboettg\CiteProc\Util |
|
20
|
|
|
* |
|
21
|
|
|
* @author Sebastian Böttger <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class Date |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* dates: Date variables called via the variable attribute are returned in the YYYYMMDD format, with zeros |
|
28
|
|
|
* substituted for any missing date-parts (e.g. 20001200 for December 2000). As a result, less specific dates |
|
29
|
|
|
* precede more specific dates in ascending sorts, e.g. “2000, May 2000, May 1st 2000”. Negative years are sorted |
|
30
|
|
|
* inversely, e.g. “100BC, 50BC, 50AD, 100AD”. Seasons are ignored for sorting, as the chronological order of the |
|
31
|
|
|
* seasons differs between the northern and southern hemispheres. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $dateParts |
|
34
|
|
|
* @return string |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function serializeDate($dateParts) |
|
37
|
|
|
{ |
|
38
|
|
|
$year = isset($dateParts[0]) ? $dateParts[0] : "0000"; |
|
39
|
|
|
$month = isset($dateParts[1]) ? $dateParts[1] : "00"; |
|
40
|
|
|
$day = isset($dateParts[2]) ? $dateParts[2] : "00"; |
|
41
|
|
|
|
|
42
|
|
|
return sprintf("%04d%02d%02d", $year, $month, $day); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public static function parseDateParts($date) |
|
46
|
|
|
{ |
|
47
|
|
|
if (!isset($date->{'raw'})) { |
|
48
|
|
|
return []; |
|
49
|
|
|
} |
|
50
|
|
|
try { |
|
51
|
|
|
$dateTime = new \DateTime($date->{'raw'}); |
|
52
|
|
|
$arr = [[$dateTime->format("Y"), $dateTime->format("m"), $dateTime->format("d")]]; |
|
53
|
|
|
} catch (\Exception $e) { |
|
54
|
|
|
throw new CiteProcException("Could not parse date \"".$date->{'raw'}."\".", 0, $e); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $arr; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* creates sort key for variables containing date and date ranges |
|
62
|
|
|
* @param $variable |
|
63
|
|
|
* @param $dataItem |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public static function getSortKeyDate($variable, $dataItem) |
|
67
|
|
|
{ |
|
68
|
|
|
if (count($dataItem->{$variable}->{'date-parts'}) > 1) { |
|
69
|
|
|
$datePartsFrom = $dataItem->{$variable}->{'date-parts'}[0]; |
|
70
|
|
|
$datePartsTo = $dataItem->{$variable}->{'date-parts'}[1]; |
|
71
|
|
|
$sortKey = self::serializeDate($datePartsFrom) . "-" . Date::serializeDate($datePartsTo); |
|
|
|
|
|
|
72
|
|
|
} else { |
|
73
|
|
|
//Date range |
|
74
|
|
|
if (!isset($dataItem->{$variable}->{'date-parts'})) { |
|
75
|
|
|
$dateParts = self::parseDateParts($dataItem->{$variable}); |
|
76
|
|
|
} else { |
|
77
|
|
|
$dateParts = $dataItem->{$variable}->{'date-parts'}[0]; |
|
78
|
|
|
} |
|
79
|
|
|
$sortKey = self::serializeDate($dateParts); |
|
80
|
|
|
} |
|
81
|
|
|
return $sortKey; |
|
82
|
|
|
} |
|
83
|
|
|
} |
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.