Passed
Push — master ( bd2115...5437ab )
by Xavier
01:30
created

DateHelper::dateFromHumanReadable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace PubPeerFoundation\PublicationDataExtractor\Support;
4
5
use Carbon\Carbon;
6
7
class DateHelper
8
{
9
    /**
10
     * @param  array        $parts
11
     * @return mixed|string
12
     */
13
    public static function dateFromDateParts(array $parts)
14
    {
15
        if (empty($parts)) {
16
            return '';
17
        }
18
19
        list($year, $month, $day) = array_merge($parts, [1, 1, 1]);
20
21
        return static::formatForParts(
22
            Carbon::createFromDate($year, $month, $day),
23
            count($parts)
24
        );
25
    }
26
27
    /**
28
     * @param $formattedDate
29
     * @return string
30
     */
31
    public static function dateFromParseableFormat($formattedDate)
32
    {
33
        return Carbon::parse($formattedDate)->format('Y-m-d');
34
    }
35
36
    /**
37
     * @param $pubDateObject
38
     * @return mixed|string
39
     */
40
    public static function dateFromPubDate($pubDateObject)
41
    {
42
        if (! isset($pubDateObject->Year) || empty($pubDateObject->Year)) {
43
            return '';
44
        }
45
46
        $month = static::getPubDateMonth($pubDateObject);
47
48
        return static::formatForParts(Carbon::create(
49
            (int) $pubDateObject->Year,
50
            (int) $month,
51
            isset($pubDateObject->Day) ? (int) $pubDateObject->Day : 1
52
        ), static::countObjectParts($pubDateObject));
53
    }
54
55
    public static function dateFromHumanReadable($dateString)
56
    {
57
        $format = 3 === count(explode(' ', $dateString)) ? 'Y M d' : 'Y M';
58
59
        return Carbon::createFromFormat($format, $dateString);
60
    }
61
62
    /**
63
     * @param $pubDateObject
64
     * @return int|string
65
     */
66
    protected static function getPubDateMonth($pubDateObject)
67
    {
68
        if (! isset($pubDateObject->Month)) {
69
            return 1;
70
        }
71
72
        return ! is_numeric((string) $pubDateObject->Month)
73
            ? Carbon::createFromFormat('M', (string) $pubDateObject->Month)->format('n')
74
            : $pubDateObject->Month;
75
    }
76
77
    /**
78
     * @param $date
79
     * @param $count
80
     * @return mixed
81
     */
82
    protected static function formatForParts($date, $count)
83
    {
84
        $partsToFormat = [
85
            1 => 'Y',
86
            2 => 'Y-m',
87
            3 => 'Y-m-d',
88
        ];
89
90
        return $date->format($partsToFormat[$count]);
91
    }
92
93
    /**
94
     * @param $pubDateObject
95
     * @return mixed
96
     */
97
    protected static function countObjectParts($pubDateObject)
98
    {
99
        return array_reduce(['Year', 'Month', 'Day'], function ($carry, $datePart) use ($pubDateObject) {
100
            return isset($pubDateObject->$datePart) ? $carry + 1 : $carry;
101
        }, 0);
102
    }
103
}
104