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