PluginProbe ʕ •ᴥ•ʔ
WP Popular Posts / 5.0.2
WP Popular Posts v5.0.2
4.0.8 4.0.9 4.1.0 4.1.1 4.1.2 4.2.0 4.2.1 4.2.2 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.2.2 5.2.3 5.2.4 5.3.0 5.3.1 5.3.2 5.3.3 5.3.4 5.3.5 5.3.6 5.4.0 5.4.1 5.4.2 5.5.0 5.5.1 6.0.0 6.0.1 6.0.2 6.0.3 6.0.4 6.0.5 6.1.0 6.1.1 6.1.2 6.1.3 6.1.4 6.2.0 6.2.1 6.3.0 6.3.1 6.3.2 6.3.3 6.3.4 6.4.0 6.4.1 6.4.2 7.0.0 7.0.1 7.1.0 7.2.0 7.3.0 7.3.1 7.3.2 7.3.3 7.3.4 7.3.5 7.3.6 7.3.7 7.3.8 7.4.0 trunk 2.3.7 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 3.2.3 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.2 4.0.3 4.0.5 4.0.6
wordpress-popular-posts / src / Moment / MomentFromVo.php
wordpress-popular-posts / src / Moment Last commit date
CustomFormats 6 years ago Locales 6 years ago FormatsInterface.php 6 years ago LICENSE.md 6 years ago Moment.php 6 years ago MomentException.php 6 years ago MomentFromVo.php 6 years ago MomentHelper.php 6 years ago MomentLocale.php 6 years ago MomentPeriodVo.php 6 years ago
MomentFromVo.php
322 lines
1 <?php
2
3 namespace WordPressPopularPosts\Moment;
4
5 /**
6 * MomentFromVo
7 * @package Moment
8 * @author Tino Ehrich (tino@bigpun.me)
9 */
10 class MomentFromVo
11 {
12 /**
13 * @var Moment
14 */
15 protected $moment;
16
17 /**
18 * @var string
19 */
20 protected $direction;
21
22 /**
23 * @var int
24 */
25 protected $seconds;
26
27 /**
28 * @var float
29 */
30 protected $minutes;
31
32 /**
33 * @var float
34 */
35 protected $hours;
36
37 /**
38 * @var float
39 */
40 protected $days;
41
42 /**
43 * @var float
44 */
45 protected $weeks;
46
47 /**
48 * @var string
49 */
50 protected $relative;
51
52 /**
53 * @param Moment $moment
54 */
55 public function __construct(Moment $moment)
56 {
57 $this->moment = $moment;
58 }
59
60 /**
61 * @return Moment
62 */
63 public function getMoment()
64 {
65 return $this->moment;
66 }
67
68 /**
69 * @param $value
70 *
71 * @return float
72 */
73 protected function getRoundedValue($value)
74 {
75 $value = round($value, 2);
76
77 if ($this->getDirection() === 'future')
78 {
79 $value = '-' . $value;
80 }
81
82 return (float)$value;
83 }
84
85 /**
86 * @param string $direction
87 *
88 * @return MomentFromVo
89 */
90 public function setDirection($direction)
91 {
92 $this->direction = $direction;
93
94 return $this;
95 }
96
97 /**
98 * @return string
99 */
100 public function getDirection()
101 {
102 return $this->direction === '-' ? 'future' : 'past';
103 }
104
105 /**
106 * @param float $days
107 *
108 * @return MomentFromVo
109 */
110 public function setDays($days)
111 {
112 $this->days = $days;
113
114 return $this;
115 }
116
117 /**
118 * @return float
119 */
120 public function getDays()
121 {
122 return $this->getRoundedValue($this->days);
123 }
124
125 /**
126 * @param float $hours
127 *
128 * @return MomentFromVo
129 */
130 public function setHours($hours)
131 {
132 $this->hours = $hours;
133
134 return $this;
135 }
136
137 /**
138 * @return float
139 */
140 public function getHours()
141 {
142 return $this->getRoundedValue($this->hours);
143 }
144
145 /**
146 * @param float $minutes
147 *
148 * @return MomentFromVo
149 */
150 public function setMinutes($minutes)
151 {
152 $this->minutes = $minutes;
153
154 return $this;
155 }
156
157 /**
158 * @return float
159 */
160 public function getMinutes()
161 {
162 return $this->getRoundedValue($this->minutes);
163 }
164
165 /**
166 * @param int $seconds
167 *
168 * @return MomentFromVo
169 */
170 public function setSeconds($seconds)
171 {
172 $this->seconds = $seconds;
173
174 return $this;
175 }
176
177 /**
178 * @return int
179 */
180 public function getSeconds()
181 {
182 return (int)$this->getRoundedValue($this->seconds);
183 }
184
185 /**
186 * @param mixed $weeks
187 *
188 * @return MomentFromVo
189 */
190 public function setWeeks($weeks)
191 {
192 $this->weeks = $weeks;
193
194 return $this;
195 }
196
197 /**
198 * @return float
199 */
200 public function getWeeks()
201 {
202 return $this->getRoundedValue($this->weeks);
203 }
204
205 /**
206 * @return float
207 */
208 public function getMonths()
209 {
210 return $this->getRoundedValue($this->weeks / 4);
211 }
212
213 /**
214 * @return float
215 */
216 public function getYears()
217 {
218 return $this->getRoundedValue($this->days / 365);
219 }
220
221 /**
222 * @return string
223 */
224 public function getRelative()
225 {
226 $formatArgs = array();
227
228 if ($this->valueInRange($this->getSeconds(), 0, 3))
229 {
230 $localeKeys = array('relativeTime', 's');
231 $formatArgs[] = 1;
232 }
233 elseif ($this->valueInRange($this->getSeconds(), 4, 59))
234 {
235 $localeKeys = array('relativeTime', 'ss');
236 $formatArgs[] = $this->roundAbs($this->getSeconds());
237 }
238 elseif ($this->valueInRange($this->getSeconds(), 60, 89))
239 {
240 $localeKeys = array('relativeTime', 'm');
241 $formatArgs[] = 1;
242 }
243 elseif ($this->valueInRange($this->getSeconds(), 90, (45 * 60)-1))
244 {
245 $localeKeys = array('relativeTime', 'mm');
246 $formatArgs[] = $this->roundAbs($this->getMinutes());
247 }
248 elseif ($this->valueInRange($this->getMinutes(), 45, 89))
249 {
250 $localeKeys = array('relativeTime', 'h');
251 $formatArgs[] = 1;
252 }
253 elseif ($this->valueInRange($this->getMinutes(), 90, (22 * 60)-1))
254 {
255 $localeKeys = array('relativeTime', 'hh');
256 $formatArgs[] = $this->roundAbs($this->getHours());
257 }
258 elseif ($this->valueInRange($this->getHours(), 22, 35))
259 {
260 $localeKeys = array('relativeTime', 'd');
261 $formatArgs[] = 1;
262 }
263 elseif ($this->valueInRange($this->getHours(), 36, (25 * 24)-1))
264 {
265 $localeKeys = array('relativeTime', 'dd');
266 $formatArgs[] = $this->roundAbs($this->getDays());
267 }
268 elseif ($this->valueInRange($this->getDays(), 25, 44))
269 {
270 $localeKeys = array('relativeTime', 'M');
271 $formatArgs[] = 1;
272 }
273 elseif ($this->valueInRange($this->getDays(), 45, 344))
274 {
275 $localeKeys = array('relativeTime', 'MM');
276 $formatArgs[] = $this->roundAbs($this->getMonths());
277 }
278 elseif ($this->valueInRange($this->getDays(), 345, 547))
279 {
280 $localeKeys = array('relativeTime', 'y');
281 $formatArgs[] = 1;
282 }
283 else
284 {
285 $localeKeys = array('relativeTime', 'yy');
286 $formatArgs[] = $this->roundAbs($this->getYears());
287 }
288
289 // add to context
290 $formatArgs[] = $this->getDirection();
291 $formatArgs[] = $this->getMoment();
292
293 // render value
294 $time = MomentLocale::renderLocaleString($localeKeys, $formatArgs);
295
296 // render value result by direction string
297 return MomentLocale::renderLocaleString(array('relativeTime', $this->getDirection()), array($time));
298 }
299
300 /**
301 * @param $value
302 * @param $from
303 * @param $to
304 *
305 * @return bool
306 */
307 private function valueInRange($value, $from, $to)
308 {
309 return abs($value) >= $from && abs($value) <= $to ? true : false;
310 }
311
312 /**
313 * @param $number
314 *
315 * @return float
316 */
317 private function roundAbs($number)
318 {
319 return round(abs($number));
320 }
321 }
322