Passed
Push — master ( 688e42...8bf068 )
by Paul
08:13
created

Date::relative()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 27
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 27
ccs 0
cts 27
cp 0
rs 8.9617
cc 6
nc 6
nop 1
crap 42
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use DateTime;
6
use GeminiLabs\SiteReviews\Helpers\Str;
7
8
class Date
9
{
10
    /**
11
     * [60, 1],
12
     * [60 * 100, 60],
13
     * [3600 * 70, 3600],
14
     * [3600 * 24 * 10, 3600 * 24],
15
     * [3600 * 24 * 30, 3600 * 24 * 7],
16
     * [3600 * 24 * 30 * 30, 3600 * 24 * 30],
17
     * [INF, 3600 * 24 * 265],.
18
     */
19
    protected static $TIME_PERIODS = [
20
        [60, 1],
21
        [6000, 60],
22
        [252000, 3600],
23
        [864000, 86400],
24
        [2592000, 604800],
25
        [77760000, 2592000],
26
        [INF, 22896000],
27
    ];
28
29
    /**
30
     * @param mixed $date
31
     * @param string $format
32
     * @return bool
33
     */
34
    public function isDate($date, $format = 'Y-m-d H:i:s')
35
    {
36
        $datetime = DateTime::createFromFormat($format, $date);
37
        return $datetime && $date == $datetime->format($format);
38
    }
39
40
    /**
41
     * @param mixed $date
42
     * @return bool
43
     */
44
    public function isTimestamp($date)
45
    {
46
        return ctype_digit($date)
47
            ? true
48
            : false;
49
    }
50
51
    /**
52
     * @param mixed $date
53
     * @param string $format
54
     * @return bool
55
     */
56
    public function isValid($date, $format = 'Y-m-d H:i:s')
57
    {
58
        return $this->isDate($date, $format) || $this->isTimestamp($date);
59
    }
60
61
    /**
62
     * @param mixed $date
63
     * @param string $fallback
64
     * @return string
65
     */
66
    public function localized($date, $fallback = '')
67
    {
68
        return $this->isValid($date)
69
            ? date_i18n('Y-m-d H:i', $date)
70
            : $fallback;
71
    }
72
73
    /**
74
     * @param mixed $date
75
     * @return string|void
76
     */
77
    public function relative($date)
78
    {
79
        if (!$this->isDate($date)) {
80
            return '';
81
        }
82
        $diff = time() - strtotime($date);
83
        if ($diff < 0) {
84
            return __('A moment ago', 'site-reviews'); // Display something vague if the date is in the future
85
        }
86
        foreach (static::$TIME_PERIODS as $i => $timePeriod) {
87
            if ($diff > $timePeriod[0]) {
88
                continue;
89
            }
90
            $unit = intval(floor($diff / $timePeriod[1]));
91
            $relativeDates = [
92
                _n('%s second ago', '%s seconds ago', $unit, 'site-reviews'),
93
                _n('%s minute ago', '%s minutes ago', $unit, 'site-reviews'),
94
                _n('an hour ago', '%s hours ago', $unit, 'site-reviews'),
95
                _n('yesterday', '%s days ago', $unit, 'site-reviews'),
96
                _n('a week ago', '%s weeks ago', $unit, 'site-reviews'),
97
                _n('%s month ago', '%s months ago', $unit, 'site-reviews'),
98
                _n('%s year ago', '%s years ago', $unit, 'site-reviews'),
99
            ];
100
            $relativeDate = $relativeDates[$i];
101
            return Str::contains('%s', $relativeDate)
102
                ? sprintf($relativeDate, $unit)
103
                : $relativeDate;
104
        }
105
    }
106
}
107