Completed
Push — master ( d8f384...facc21 )
by f
01:38
created

TimeSpeller::spellDifference()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5.3906

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 3
dl 0
loc 12
ccs 6
cts 8
cp 0.75
crap 5.3906
rs 9.5555
c 0
b 0
f 0
1
<?php
2
namespace morphos;
3
4
use DateInterval;
5
use DateTime;
6
use InvalidArgumentException;
7
8
abstract class TimeSpeller
9
{
10
    const YEAR = 'year';
11
    const MONTH = 'month';
12
    const DAY = 'day';
13
    const HOUR = 'hour';
14
    const MINUTE = 'minute';
15
    const SECOND = 'second';
16
17
    const AGO = 'ago';
18
    const IN = 'in';
19
20
    const AND_WORD = 'and';
21
22
    const JUST_NOW = 'just now';
23
24
    const DIRECTION = 1;
25
    const SEPARATE = 2;
26
27
    /**
28
     * @abstract
29
     * @param $count
30
     * @param $unit
31
     * @return string
32
     */
33
    public static function spellUnit($count, $unit) {}
34
35
    /**
36
     * @param DateInterval $interval
37
     * @param int $options
38
     * @param int $limit
39
     * @return string
40
     */
41 22
    public static function spellInterval(DateInterval $interval, $options = 0, $limit = 0)
42
    {
43 22
        $parts = [];
44 22
        $k = 0;
45
        foreach ([
46 22
            'y' => static::YEAR,
47 22
            'm' => static::MONTH,
48 22
            'd' => static::DAY,
49 22
            'h' => static::HOUR,
50 22
            'i' => static::MINUTE,
51 22
            's' => static::SECOND
52
        ] as $interval_field => $unit) {
53 22
            if ($interval->{$interval_field} > 0) {
54 22
                if($limit > 0 && $k >= $limit) {
55 4
                    break;
56
                }
57 22
                $parts[] = static::spellUnit($interval->{$interval_field}, $unit);
58 22
                $k++;
59
            }
60
        }
61
62 22
        if (empty($parts)) {
63
            return static::JUST_NOW;
64
        }
65
66 22
        if ($options & static::SEPARATE && count($parts) > 1) {
67 6
            $last_part = array_pop($parts);
68 6
            $spelled = implode(', ', $parts).' '.static::AND_WORD.' '.$last_part;
69
        } else {
70 16
            $spelled = implode(' ', $parts);
71
        }
72
73 22
        if ($options & static::DIRECTION) {
74 4
            if ($interval->invert) {
75
                $spelled = static::IN.' '.$spelled;
76
            } else {
77 4
                $spelled .= ' '.static::AGO;
78
            }
79
        }
80
81 22
        return $spelled;
82
    }
83
84
    /**
85
     * @param int|string|DateTime $dateTime Unix timestamp
86
     *                                      or datetime in strtotime() format
87
     *                                      or DateTime instance
88
     *
89
     * @param int                 $options
90
     * @param int                 $limit
91
     *
92
     * @return string
93
     * @throws \Exception
94
     */
95 6
    public static function spellDifference($dateTime, $options = 0, $limit = 0)
96
    {
97 6
        if (is_numeric($dateTime) || is_string($dateTime)) {
98 6
            $dateTime = new DateTime(is_numeric($dateTime)
99 4
                ? '@' . $dateTime
100 6
                : $dateTime);
101
        } else if(!($dateTime instanceof DateTime)) {
102
            throw new InvalidArgumentException('dateTime argument should be unix timestamp (int) or date time (string) or DateTime instance');
103
        }
104
105 6
        return static::spellInterval($dateTime->diff(new DateTime()), $options, $limit);
106
    }
107
}
108