|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silviooosilva\CacheerPhp\Support; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class TimeBuilder |
|
9
|
|
|
* @author Sílvio Silva <https://github.com/silviooosilva> |
|
10
|
|
|
* @package Silviooosilva\CacheerPhp |
|
11
|
|
|
*/ |
|
12
|
|
|
class TimeBuilder |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** @param Closure $callback */ |
|
16
|
|
|
private Closure $callback; |
|
17
|
|
|
|
|
18
|
|
|
/** @var mixed */ |
|
19
|
|
|
private $builder = null; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* TimeBuilder constructor. |
|
23
|
|
|
* @param Closure $callback |
|
24
|
|
|
* @param mixed $builder |
|
25
|
|
|
* |
|
26
|
|
|
* @return void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(Closure $callback, $builder) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->callback = $callback; |
|
31
|
|
|
$this->builder = $builder; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Sets the time in seconds. |
|
36
|
|
|
* |
|
37
|
|
|
* @param int $seconds |
|
38
|
|
|
* @return mixed |
|
39
|
|
|
*/ |
|
40
|
|
|
public function second(int $seconds) |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->setTime($seconds, "seconds"); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Sets the time in minutes. |
|
47
|
|
|
* |
|
48
|
|
|
* @param int $minutes |
|
49
|
|
|
* @return mixed |
|
50
|
|
|
*/ |
|
51
|
|
|
public function minute(int $minutes) |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->setTime($minutes, "minutes"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Sets the time in hours. |
|
58
|
|
|
* |
|
59
|
|
|
* @param int $hours |
|
60
|
|
|
* @return mixed |
|
61
|
|
|
*/ |
|
62
|
|
|
public function hour(int $hours) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->setTime($hours, "hours"); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Sets the time in days. |
|
69
|
|
|
* |
|
70
|
|
|
* @param int $days |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
|
|
public function day(int $days) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->setTime($days, "days"); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Sets the time in weeks. |
|
80
|
|
|
* |
|
81
|
|
|
* @param int $weeks |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
public function week(int $weeks) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->setTime($weeks, "weeks"); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Sets the time in months. |
|
91
|
|
|
* |
|
92
|
|
|
* @param int $months |
|
93
|
|
|
* @return mixed |
|
94
|
|
|
*/ |
|
95
|
|
|
public function month(int $months) |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->setTime($months, "months"); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* This method sets the time value and unit, and invokes the callback with the formatted string. |
|
103
|
|
|
* |
|
104
|
|
|
* @param int $value |
|
105
|
|
|
* @param string $unit |
|
106
|
|
|
* @return mixed |
|
107
|
|
|
*/ |
|
108
|
|
|
private function setTime(int $value, string $unit) |
|
109
|
|
|
{ |
|
110
|
|
|
($this->callback)("{$value} {$unit}"); |
|
111
|
|
|
return $this->builder; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|