DateTime::asDate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Benrowe\Formatter\Providers;
4
5
use Benrowe\Formatter\AbstractFormatterProvider;
6
use Carbon\Carbon;
7
8
/**
9
 * DateTime Formatter
10
 *
11
 * Contains formatter for datetime values
12
 *
13
 * @package Benrowe\Formatter
14
 */
15
class DateTime extends AbstractFormatterProvider
16
{
17
    protected $carbon;
18
19 12
    public function __construct(Carbon $carbon)
20
    {
21 12
        $this->carbon = $carbon;
22 12
    }
23
24
    /**
25
     * Format the datetime as a date
26
     *
27
     * @param  mixed $value
28
     * @param  string $format
29
     * @return string
30
     */
31 3
    public function asDate($value, $format = 'Y-m-d')
32
    {
33 3
        return $this->output($value, $format);
34
    }
35
36
    /**
37
     * Format the datetime as a date
38
     *
39
     * @param  mixed $value
40
     * @param  string $format
41
     * @return string
42
     */
43 1
    public function asTime($value, $format = 'H:i:s')
44
    {
45 1
        return $this->output($value, $format);
46
    }
47
48
    /**
49
     * Format the datetime as a date
50
     *
51
     * @param  mixed $value
52
     * @param  string $format
53
     * @return string
54
     */
55 1
    public function asDateTime($value, $format = 'Y-m-d H:i:s')
56
    {
57 1
        return $this->output($value, $format);
58
    }
59
60
    private $durationUnits = [
61
        'Day'    => 86400,
62
        'Hour'   => 3600,
63
        'Minute' => 60,
64
        'Second' => 1
65
    ];
66
67
    /**
68
     * Take in a number of seconds and display that as a human readable amount
69
     * of time
70
     *
71
     * From: https://jonlabelle.com/snippets/view/php/convert-seconds-to-human-readable
72
     *
73
     * @param  int $seconds
74
     * @return string
75
     * @todo allow units to be overridden (non-english..)
76
     */
77 6
    public function asDurationHuman($seconds, $delimit = ', ')
78
    {
79 6
        if ($seconds === null) {
80 1
            return $this->nullValue;
81
        }
82
83 5
        $parts = [];
84
85 5
        foreach ($this->durationUnits as $label => $duration) {
86 5
            $div = floor($seconds / $duration);
87 5
            if ($div == 0) {
88 5
                continue;
89
            }
90
91 5
            $part = $div . ' ' . $label;
92 5
            if ($div != 1) {
93 4
                $part .= 's';
94 4
            }
95 5
            $parts[] = $part;
96
97 5
            $seconds %= $duration;
98 5
        }
99
100 5
        $last = array_pop($parts);
101
102 5
        if (empty($parts)) {
103 3
            return $last;
104
        }
105
106 2
        return join($delimit, $parts) . ' and ' . $last;
107
    }
108
109
    /**
110
     * Generate the output from the value
111
     *
112
     * @param  mixed $value the datetime value
113
     * @param  string $format format string
114
     * @return string
115
     */
116 5
    private function output($value, $format)
117
    {
118 5
        return $this
119 5
            ->normaliseValue($value)
120 5
            ->format($format);
121
    }
122
123
    /**
124
     * Convert the datetime input into a instance of Carbon
125
     *
126
     * @param  mixed $value the datetime value
127
     * @return Carbon
128
     */
129 5
    private function normaliseValue($value)
130
    {
131 5
        $carbon = $this->getCarbon();
132
        // sniff the value type
133 5
        if (is_int($value)) {
134 3
            return $carbon->createFromTimestamp($value);
135
        }
136 2
        return $carbon->parse($value);
137
    }
138
139
    /**
140
     * Get an instance of the carbon datetime handler
141
     *
142
     * @return Carbon
143
     * @throws FormatterException if carbon isn't available
144
     */
145 5
    private function getCarbon()
146
    {
147 5
        return $this->carbon;
148
    }
149
}
150