Completed
Push — master ( fea31f...523a89 )
by Sebastian
02:58
created

Date::getSortKeyDate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 12
nc 3
nop 2
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);
0 ignored issues
show
Coding Style introduced by
As per coding style, self should be used for accessing local static members.

This check looks for accesses to local static members using the fully qualified name instead of self::.

<?php

class Certificate {
    const TRIPLEDES_CBC = 'ASDFGHJKL';

    private $key;

    public function __construct()
    {
        $this->key = Certificate::TRIPLEDES_CBC;
    }
}

While this is perfectly valid, the fully qualified name of Certificate::TRIPLEDES_CBC could just as well be replaced by self::TRIPLEDES_CBC. Referencing local members with self:: 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.

Loading history...
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
}