Completed
Push — master ( 659fdf...424bb4 )
by f
02:07
created

TimeSpeller   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 84.21%

Importance

Changes 0
Metric Value
dl 0
loc 107
ccs 32
cts 38
cp 0.8421
rs 10
c 0
b 0
f 0
wmc 16
lcom 0
cbo 0

3 Methods

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