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