|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace Tequilarapido\Consolify\Progress; |
|
5
|
|
|
|
|
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
7
|
|
|
|
|
8
|
|
|
class SleepModeState implements Arrayable |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Is sleep mode active ? |
|
12
|
|
|
* |
|
13
|
|
|
* @var bool |
|
14
|
|
|
*/ |
|
15
|
|
|
private $on = false; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Message describing the sleep mode state |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
private $message = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* How much remaining seconds before sleep mode goes off ? |
|
26
|
|
|
* |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
private $remaining = 0; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* SleepModeState constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param $on |
|
35
|
|
|
* @param $message |
|
36
|
|
|
* @param int $remaining |
|
37
|
|
|
*/ |
|
38
|
|
|
public function __construct($on, $message, $remaining = 0) |
|
39
|
|
|
{ |
|
40
|
|
|
$this->on = $on; |
|
41
|
|
|
$this->message = $message; |
|
42
|
|
|
$this->remaining = $remaining; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Alias : Static constructor for entering sleep mode. |
|
47
|
|
|
* |
|
48
|
|
|
* @return static |
|
49
|
|
|
*/ |
|
50
|
|
|
public static function entering() |
|
51
|
|
|
{ |
|
52
|
|
|
return new static(true, 'Entering sleep mode'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Alias : Static constructor for updating remaining time |
|
57
|
|
|
* |
|
58
|
|
|
* @return static |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function remaining($remaining) |
|
61
|
|
|
{ |
|
62
|
|
|
return new static( |
|
63
|
|
|
true, |
|
64
|
|
|
'Remaining : ' . static::formatRemainingSeconds($remaining), |
|
65
|
|
|
$remaining |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Alias : Static constructor for leaving sleep mode. |
|
71
|
|
|
* |
|
72
|
|
|
* @return static |
|
73
|
|
|
*/ |
|
74
|
|
|
public static function leaving() |
|
75
|
|
|
{ |
|
76
|
|
|
return new static(false, 'leaving sleep mode'); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Return message |
|
81
|
|
|
* |
|
82
|
|
|
* @return string |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getMessage() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->message; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Return remaining |
|
91
|
|
|
* |
|
92
|
|
|
* @return int |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getRemaining() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->remaining; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Is on ? |
|
101
|
|
|
* |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function isOn() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->on; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Return array representing the sleep mode state. |
|
111
|
|
|
* |
|
112
|
|
|
* @return array |
|
113
|
|
|
*/ |
|
114
|
|
|
public function toArray() |
|
115
|
|
|
{ |
|
116
|
|
|
return [ |
|
117
|
|
|
'on' => $this->on, |
|
118
|
|
|
'message' => $this->message, |
|
119
|
|
|
'remaining' => $this->remaining |
|
120
|
|
|
]; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Format seconds. |
|
125
|
|
|
* |
|
126
|
|
|
* @param $seconds |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
private static function formatRemainingSeconds($seconds) |
|
130
|
|
|
{ |
|
131
|
|
|
return gmdate('i', $seconds) . 'm, ' . gmdate('s', $seconds) . 's'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
} |