1 | <?php |
||
17 | class Duration |
||
18 | { |
||
19 | const SECOND = 1; |
||
20 | const MINUTE = 60; |
||
21 | const HOUR = self::MINUTE * 60; |
||
22 | const DAY = self::HOUR * 24; |
||
23 | const WEEK = self::DAY * 7; |
||
24 | const MONTH = self::DAY * 30; |
||
25 | const YEAR = self::DAY * 365; |
||
26 | |||
27 | /** |
||
28 | * 1y 2m 3d 4h 5m 6s. |
||
29 | */ |
||
30 | const FORMAT_SHORT = 0; |
||
31 | /** |
||
32 | * 1 year 2 months 3 days 4 hours 5 minutes 6 seconds. |
||
33 | */ |
||
34 | const FORMAT_LONG = 1; |
||
35 | |||
36 | private static $defaultFormats = [ |
||
37 | self::FORMAT_SHORT => [ |
||
38 | self::YEAR => 'y', |
||
39 | self::MONTH => 'mo', |
||
40 | self::WEEK => 'w', |
||
41 | self::DAY => 'd', |
||
42 | self::HOUR => 'h', |
||
43 | self::MINUTE => 'm', |
||
44 | self::SECOND => 's', |
||
45 | ], |
||
46 | self::FORMAT_LONG => [ |
||
47 | self::YEAR => ' year(s)', |
||
48 | self::MONTH => ' month(s)', |
||
49 | self::WEEK => ' week(s)', |
||
50 | self::DAY => ' day(s)', |
||
51 | self::HOUR => ' hour(s)', |
||
52 | self::MINUTE => ' minute(s)', |
||
53 | self::SECOND => ' second(s)', |
||
54 | ], |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * @var int |
||
59 | */ |
||
60 | private $format = self::FORMAT_SHORT; |
||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | private $formats; |
||
65 | /** |
||
66 | * @var int |
||
67 | */ |
||
68 | private $limit = 0; |
||
69 | |||
70 | /** |
||
71 | * @param int $format |
||
72 | */ |
||
73 | public function __construct($format = self::FORMAT_SHORT) |
||
78 | |||
79 | /** |
||
80 | * Create a new Duration. |
||
81 | * |
||
82 | * @param int $format |
||
83 | * |
||
84 | * @return \nochso\Omni\Duration |
||
85 | */ |
||
86 | public static function create($format = self::FORMAT_SHORT) |
||
90 | |||
91 | /** |
||
92 | * addFormat to the existing defaults and set it as the current format. |
||
93 | * |
||
94 | * e.g. |
||
95 | * |
||
96 | * ```php |
||
97 | * $format = Duration::FORMAT_LONG => [ |
||
98 | * Duration::YEAR => ' year(s)', |
||
99 | * Duration::MONTH => ' month(s)', |
||
100 | * Duration::WEEK => ' week(s)', |
||
101 | * Duration::DAY => ' day(s)', |
||
102 | * Duration::HOUR => ' hour(s)', |
||
103 | * Duration::MINUTE => ' minute(s)', |
||
104 | * Duration::SECOND => ' second(s)', |
||
105 | * ]; |
||
106 | * $df->addFormat('my custom period format', $format); |
||
107 | * ``` |
||
108 | * |
||
109 | * @param string $name |
||
110 | * @param string[] $periodFormats |
||
111 | * |
||
112 | * @return $this |
||
113 | */ |
||
114 | public function addFormat($name, array $periodFormats) |
||
120 | |||
121 | /** |
||
122 | * setFormat to use by its custom name or one of the default Duration constants. |
||
123 | * |
||
124 | * @param string $name One of the `Duration::FORMAT_*` constants or a name of a format added via `addFormat()` |
||
125 | * |
||
126 | * @return $this |
||
127 | */ |
||
128 | public function setFormat($name) |
||
136 | |||
137 | /** |
||
138 | * limitPeriods limits the amount of significant periods (years, months, etc.) to keep. |
||
139 | * |
||
140 | * Significant periods are periods with non-zero values. |
||
141 | * |
||
142 | * @param int $limit 0 for keeping all significant periods or any positive integer. |
||
143 | * |
||
144 | * @return $this |
||
145 | */ |
||
146 | public function limitPeriods($limit) |
||
151 | |||
152 | /** |
||
153 | * Format an amount of seconds or a `DateInterval` object. |
||
154 | * |
||
155 | * @param int|\DateInterval $duration |
||
156 | * |
||
157 | * @return string A formatted duration for human consumption. |
||
158 | */ |
||
159 | public function format($duration) |
||
163 | |||
164 | /** |
||
165 | * @param int|\DateInterval $duration |
||
166 | * @param array $steps |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | private function formatPeriods($duration, $steps) |
||
192 | |||
193 | /** |
||
194 | * @param int|\DateInterval $duration |
||
195 | * |
||
196 | * @return int |
||
197 | */ |
||
198 | private function ensureSeconds($duration) |
||
206 | } |
||
207 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.