1 | <?php |
||
2 | |||
3 | |||
4 | namespace Pheanstalk\Structure; |
||
5 | |||
6 | class TimeSchedule |
||
7 | { |
||
8 | const SUM_SUBUNIT_IN_UNIT = 1770; |
||
9 | const SUM_HOURS = 276; |
||
10 | const SUM_DAYS = 465; |
||
11 | const SUM_MONTHS = 66; |
||
12 | const SUM_WEEKDAYS = 21; |
||
13 | |||
14 | /** @var array[int] $seconds */ |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
15 | private $seconds; |
||
16 | |||
17 | /** @var array[int] $minutes */ |
||
0 ignored issues
–
show
|
|||
18 | private $minutes; |
||
19 | |||
20 | /** @var array[int] $hours */ |
||
0 ignored issues
–
show
|
|||
21 | private $hours; |
||
22 | |||
23 | /** @var array[int] $hours */ |
||
0 ignored issues
–
show
|
|||
24 | private $days; |
||
25 | |||
26 | /** @var array[int] $months */ |
||
0 ignored issues
–
show
|
|||
27 | private $months; |
||
28 | |||
29 | /** @var array[int] $weekdays */ |
||
0 ignored issues
–
show
|
|||
30 | private $weekdays; |
||
31 | |||
32 | /** |
||
33 | * TimeSchedule constructor. |
||
34 | * |
||
35 | * @param array[int] $seconds |
||
0 ignored issues
–
show
|
|||
36 | * @param array[int] $minutes |
||
37 | * @param array[int] $hours |
||
38 | * @param array[int] $months |
||
39 | * @param array[int] $weekdays |
||
40 | */ |
||
41 | 25 | public function __construct(array $seconds = [], array $minutes = [], array $hours = [], array $days = [], array $months = [], array $weekdays = []) |
|
42 | { |
||
43 | $this |
||
44 | 25 | ->setSeconds($seconds) |
|
45 | 25 | ->setMinutes($minutes) |
|
46 | 25 | ->setHours($hours) |
|
47 | 25 | ->setDays($days) |
|
48 | 25 | ->setMonths($months) |
|
49 | 25 | ->setWeekdays($weekdays) |
|
50 | ; |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @return array[int] |
||
0 ignored issues
–
show
|
|||
55 | */ |
||
56 | 2 | public function getSeconds(): array |
|
57 | { |
||
58 | 2 | return $this->seconds; |
|
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param array[int] $seconds |
||
0 ignored issues
–
show
|
|||
63 | * |
||
64 | * @return TimeSchedule |
||
65 | */ |
||
66 | 25 | public function setSeconds(array $seconds): TimeSchedule |
|
67 | { |
||
68 | 25 | if (count($seconds) !== count(array_filter($seconds, 'is_numeric')) || array_sum($seconds) > self::SUM_SUBUNIT_IN_UNIT) { |
|
69 | 3 | throw new \Exception('Wrong units of time'); |
|
70 | } |
||
71 | 25 | $this->seconds = $seconds; |
|
72 | 25 | return $this; |
|
73 | } |
||
74 | |||
75 | /** |
||
76 | * @return array[int] |
||
0 ignored issues
–
show
|
|||
77 | */ |
||
78 | 2 | public function getMinutes(): array |
|
79 | { |
||
80 | 2 | return $this->minutes; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param array[int] $minutes |
||
0 ignored issues
–
show
|
|||
85 | * |
||
86 | * @return TimeSchedule |
||
87 | */ |
||
88 | 25 | public function setMinutes(array $minutes): TimeSchedule |
|
89 | { |
||
90 | 25 | if (count($minutes) !== count(array_filter($minutes, 'is_numeric')) || array_sum($minutes) > self::SUM_SUBUNIT_IN_UNIT) { |
|
91 | 1 | throw new \Exception('Wrong units of time'); |
|
92 | } |
||
93 | 25 | $this->minutes = $minutes; |
|
94 | 25 | return $this; |
|
95 | } |
||
96 | |||
97 | /** |
||
98 | * @return array[int] |
||
0 ignored issues
–
show
|
|||
99 | */ |
||
100 | 2 | public function getHours(): array |
|
101 | { |
||
102 | 2 | return $this->hours; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @param array[int] $hours |
||
0 ignored issues
–
show
|
|||
107 | * |
||
108 | * @return TimeSchedule |
||
109 | */ |
||
110 | 25 | public function setHours(array $hours): TimeSchedule |
|
111 | { |
||
112 | 25 | if (count($hours) !== count(array_filter($hours, 'is_numeric')) || array_sum($hours) > self::SUM_HOURS) { |
|
113 | 2 | throw new \Exception('Wrong units of time'); |
|
114 | } |
||
115 | 25 | $this->hours = $hours; |
|
116 | 25 | return $this; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * @return array |
||
121 | */ |
||
122 | 2 | public function getDays(): array |
|
123 | { |
||
124 | 2 | return $this->days; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param array $days |
||
129 | * |
||
130 | * @return TimeSchedule |
||
131 | */ |
||
132 | 25 | public function setDays(array $days): TimeSchedule |
|
133 | { |
||
134 | 25 | if (count($days) !== count(array_filter($days, 'is_numeric')) || array_sum($days) > self::SUM_DAYS) { |
|
135 | 2 | throw new \Exception('Wrong units of time'); |
|
136 | } |
||
137 | 25 | $this->days = $days; |
|
138 | 25 | return $this; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * @return array[int] |
||
0 ignored issues
–
show
|
|||
143 | */ |
||
144 | 2 | public function getMonths(): array |
|
145 | { |
||
146 | 2 | return $this->months; |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param array[int] $months |
||
0 ignored issues
–
show
|
|||
151 | * |
||
152 | * @return TimeSchedule |
||
153 | */ |
||
154 | 25 | public function setMonths(array $months): TimeSchedule |
|
155 | { |
||
156 | 25 | if (count($months) !== count(array_filter($months, 'is_numeric')) || array_sum($months) > self::SUM_MONTHS) { |
|
157 | 2 | throw new \Exception('Wrong units of time'); |
|
158 | } |
||
159 | 25 | $this->months = $months; |
|
160 | 25 | return $this; |
|
161 | } |
||
162 | |||
163 | /** |
||
164 | * @return array[int] |
||
0 ignored issues
–
show
|
|||
165 | */ |
||
166 | 1 | public function getWeekdays(): array |
|
167 | { |
||
168 | 1 | return $this->weekdays; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * @param array[int] $weekdays |
||
0 ignored issues
–
show
|
|||
173 | * |
||
174 | * @return TimeSchedule |
||
175 | */ |
||
176 | 25 | public function setWeekdays(array $weekdays): TimeSchedule |
|
177 | { |
||
178 | 25 | $this->weekdays = $weekdays; |
|
179 | 25 | return $this; |
|
180 | } |
||
181 | |||
182 | 1 | public function __toString() |
|
183 | { |
||
184 | 1 | return implode(",", $this->getSeconds()).";". |
|
185 | 1 | implode(",", $this->getMinutes()).";". |
|
186 | 1 | implode(",", $this->getHours()).";". |
|
187 | 1 | implode(",", $this->getDays()).";". |
|
188 | 1 | implode(",", $this->getMonths()).";". |
|
189 | 1 | implode(",", $this->getWeekdays()) |
|
190 | ; |
||
191 | } |
||
192 | |||
193 | 1 | public function __fromString($time) |
|
194 | { |
||
195 | 1 | $times = explode(';', $time); |
|
196 | 1 | $arrayOfTimes = []; |
|
197 | 1 | foreach ($times as $time) { |
|
0 ignored issues
–
show
|
|||
198 | 1 | $arrayOfTimes[] = (empty($time)) ? [] : explode(',', $time); |
|
199 | } |
||
200 | 1 | $this->__construct($arrayOfTimes[0], $arrayOfTimes[1], $arrayOfTimes[2], $arrayOfTimes[3], $arrayOfTimes[4], $arrayOfTimes[5]); |
|
201 | 1 | return $this; |
|
202 | } |
||
203 | } |
||
204 |