1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JPBernius\FMeat\Entities; |
4
|
|
|
|
5
|
|
|
use ArrayIterator; |
6
|
|
|
use IteratorAggregate; |
7
|
|
|
use Traversable; |
8
|
|
|
use JPBernius\FMeat\Exeptions\DayNotFoundException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Week |
12
|
|
|
* @package JPBernius\FMeat\Entities |
13
|
|
|
*/ |
14
|
|
|
class Week implements Entity, IteratorAggregate |
15
|
|
|
{ |
16
|
|
|
/** @var int */ |
17
|
|
|
private $weekNumber; |
18
|
|
|
|
19
|
|
|
/** @var array */ |
20
|
|
|
private $days = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Week constructor. |
24
|
|
|
* @param $weekNumber |
25
|
|
|
*/ |
26
|
3 |
|
public function __construct($weekNumber) |
27
|
|
|
{ |
28
|
3 |
|
$this->weekNumber = $weekNumber; |
29
|
3 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param \stdClass $jsonObject |
33
|
|
|
* @return Week |
34
|
|
|
*/ |
35
|
3 |
|
public static function fromJson(\stdClass $jsonObject): self |
36
|
|
|
{ |
37
|
3 |
|
$week = new Week($jsonObject->number); |
38
|
|
|
|
39
|
3 |
|
foreach ($jsonObject->days as $jsonDayObject) { |
40
|
3 |
|
$day = Day::fromJson($jsonDayObject); |
41
|
3 |
|
$week->addDay($day); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
return $week; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return int |
49
|
|
|
*/ |
50
|
3 |
|
public function getWeekNumber(): int |
51
|
|
|
{ |
52
|
3 |
|
return $this->weekNumber; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Day $day |
57
|
|
|
*/ |
58
|
3 |
|
public function addDay(Day $day): void |
59
|
|
|
{ |
60
|
3 |
|
$this->days[$day->getDayOfWeek() - 1] = $day; |
61
|
3 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param int $dayOfWeek |
65
|
|
|
* @return Day |
66
|
|
|
* @throws DayNotFoundException |
67
|
|
|
*/ |
68
|
3 |
|
public function getDay(int $dayOfWeek): Day |
69
|
|
|
{ |
70
|
3 |
|
if (empty($this->days[$dayOfWeek - 1])) { |
71
|
|
|
throw new DayNotFoundException(); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
return $this->days[$dayOfWeek - 1]; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Retrieve an external iterator |
79
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
80
|
|
|
* @return Traversable An instance of an object implementing <b>Iterator</b> or |
81
|
|
|
* <b>Traversable</b> |
82
|
|
|
* @since 5.0.0 |
83
|
|
|
*/ |
84
|
|
|
public function getIterator(): Traversable |
85
|
|
|
{ |
86
|
|
|
return new ArrayIterator($this->days); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Specify data which should be serialized to JSON |
91
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
92
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
93
|
|
|
* which is a value of any type other than a resource. |
94
|
|
|
* @since 5.4.0 |
95
|
|
|
*/ |
96
|
|
|
public function jsonSerialize(): array |
97
|
|
|
{ |
98
|
|
|
return [ |
99
|
|
|
'number' => $this->getWeekNumber(), |
100
|
|
|
'days' => array_map(function(Day $day) { |
101
|
|
|
return $day->jsonSerialize(); |
102
|
|
|
}, $this->days) |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
} |