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 | |||
12 | use DateTime; |
||
13 | use Exception; |
||
14 | use Seboettg\CiteProc\Exception\CiteProcException; |
||
15 | use Seboettg\CiteProc\Style\Sort\Key; |
||
16 | |||
17 | /** |
||
18 | * Class Date |
||
19 | * |
||
20 | * Just a helper class for date issues |
||
21 | * |
||
22 | * @package Seboettg\CiteProc\Util |
||
23 | * |
||
24 | * @author Sebastian Böttger <[email protected]> |
||
25 | */ |
||
26 | class DateHelper |
||
27 | { |
||
28 | |||
29 | /** |
||
30 | * dates: Date variables called via the variable attribute are returned in the YYYYMMDD format, with zeros |
||
31 | * substituted for any missing date-parts (e.g. 20001200 for December 2000). As a result, less specific dates |
||
32 | * precede more specific dates in ascending sorts, e.g. “2000, May 2000, May 1st 2000”. Negative years are sorted |
||
33 | * inversely, e.g. “100BC, 50BC, 50AD, 100AD”. Seasons are ignored for sorting, as the chronological order of the |
||
34 | * seasons differs between the northern and southern hemispheres. |
||
35 | * |
||
36 | * @param array $dateParts |
||
37 | * @return string |
||
38 | */ |
||
39 | public static function serializeDate($dateParts) |
||
40 | { |
||
41 | $year = isset($dateParts[0]) ? $dateParts[0] : "0000"; |
||
42 | $month = isset($dateParts[1]) ? $dateParts[1] : "00"; |
||
43 | $day = isset($dateParts[2]) ? $dateParts[2] : "00"; |
||
44 | |||
45 | return sprintf("%04d%02d%02d", $year, $month, $day); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @param $date |
||
50 | * @return array |
||
51 | * @throws CiteProcException |
||
52 | */ |
||
53 | public static function parseDateParts($date) |
||
54 | { |
||
55 | if (!isset($date->{'raw'})) { |
||
56 | return []; |
||
57 | } |
||
58 | try { |
||
59 | $dateTime = new DateTime($date->{'raw'}); |
||
60 | $arr = [[$dateTime->format("Y"), $dateTime->format("m"), $dateTime->format("d")]]; |
||
61 | } catch (Exception $e) { |
||
62 | throw new CiteProcException("Could not parse date \"".$date->{'raw'}."\".", 0, $e); |
||
63 | } |
||
64 | |||
65 | return $arr; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * creates sort key for variables containing date and date ranges |
||
70 | * @param array $dataItem |
||
71 | * @param Key $key |
||
72 | * @return string |
||
73 | * @throws CiteProcException |
||
74 | */ |
||
75 | public static function getSortKeyDate($dataItem, $key) |
||
76 | { |
||
77 | $variable = $variable = $key->getVariable(); |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
78 | $part = $key->getRangePart(); |
||
79 | if (count($dataItem->{$variable}->{'date-parts'}) > 1) { |
||
80 | //Date range |
||
81 | $datePart = $dataItem->{$variable}->{'date-parts'}[$part]; |
||
82 | $sortKey = self::serializeDate($datePart); |
||
83 | if ($key->getSort() === "descending" && $part === 0 || |
||
0 ignored issues
–
show
|
|||
84 | $key->getSort() === "ascending" && $part === 1) { |
||
85 | $sortKey .= "-"; |
||
86 | } |
||
87 | } else { |
||
88 | if (!isset($dataItem->{$variable}->{'date-parts'})) { |
||
89 | $dateParts = self::parseDateParts($dataItem->{$variable}); |
||
90 | } else { |
||
91 | $dateParts = $dataItem->{$variable}->{'date-parts'}[0]; |
||
92 | } |
||
93 | $sortKey = self::serializeDate($dateParts); |
||
94 | } |
||
95 | return $sortKey; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param array $items |
||
100 | * @param string $variable name of the date variable |
||
101 | * @param string $match ("all"|"any") default "all" |
||
102 | * @return bool |
||
103 | */ |
||
104 | public static function hasDateRanges($items, $variable, $match = "all") |
||
105 | { |
||
106 | $ret = true; |
||
107 | foreach ($items as $item) { |
||
108 | $dateParts = $item->{$variable}->{"date-parts"}; |
||
109 | if ($match === "all" && count($dateParts) !== 2) { |
||
110 | return false; |
||
111 | } elseif ($match === "any" && count($dateParts) === 2) { |
||
112 | return true; |
||
113 | } else { |
||
114 | $ret = ($match === "all") ? $ret&true : $ret|true; |
||
0 ignored issues
–
show
|
|||
115 | } |
||
116 | } |
||
117 | return (bool) $ret; |
||
118 | } |
||
119 | } |
||
120 |