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