1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TournamentGenerator\Containers; |
5
|
|
|
|
6
|
|
|
use Exception; |
7
|
|
|
use InvalidArgumentException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class GameContainer |
11
|
|
|
* |
12
|
|
|
* Special container for games. |
13
|
|
|
* |
14
|
|
|
* @package TournamentGenerator\Containers |
15
|
|
|
* @author Tomáš Vojík <[email protected]> |
16
|
|
|
* @since 0.4 |
17
|
|
|
*/ |
18
|
|
|
class GameContainer extends BaseContainer |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** @var GameContainer[] Direct child containers */ |
22
|
|
|
protected array $children = []; |
23
|
|
|
/** @var int First auto increment value for recalculating the ids */ |
24
|
|
|
protected int $firstIncrement = 1; |
25
|
|
|
/** @var int Autoincrement for contained games */ |
26
|
|
|
protected int $autoIncrement = 1; |
27
|
|
|
/** @var GameContainer|null Parent container reference */ |
28
|
|
|
protected ?BaseContainer $parent; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Increments the auto-incremented id |
32
|
|
|
* |
33
|
|
|
* @param GameContainer|null $sender |
34
|
|
|
* |
35
|
|
|
* @post Propagates to all children but the sender |
36
|
|
|
* @post Propagates to the parent if not the sender |
37
|
|
|
* @since 0.5 |
38
|
|
|
*/ |
39
|
178 |
|
public function incrementId(?GameContainer $sender = null) : void { |
40
|
|
|
// Increment |
41
|
178 |
|
$this->autoIncrement++; |
42
|
|
|
// Propagate to parent |
43
|
178 |
|
if (isset($this->parent) && $this->parent !== $sender) { |
44
|
110 |
|
$this->parent->incrementId($this); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
// Propagate to children |
47
|
178 |
|
foreach ($this->children as $child) { |
48
|
110 |
|
if ($child !== $sender) { |
49
|
97 |
|
$child->incrementId($this); |
50
|
|
|
} |
51
|
|
|
} |
52
|
178 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return int |
56
|
|
|
* @since 0.5 |
57
|
|
|
*/ |
58
|
178 |
|
public function getAutoIncrement() : int { |
59
|
178 |
|
return $this->autoIncrement; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets the autoincrement number for games |
64
|
|
|
* |
65
|
|
|
* @param int $autoIncrement |
66
|
|
|
* |
67
|
|
|
* @post The value is propagated to child containers |
68
|
|
|
* @post The firstIncrement value is set too |
69
|
|
|
* |
70
|
|
|
* @return GameContainer |
71
|
|
|
* @since 0.5 |
72
|
|
|
*/ |
73
|
162 |
|
public function setAutoIncrement(int $autoIncrement) : GameContainer { |
74
|
162 |
|
$this->autoIncrement = $autoIncrement; |
75
|
162 |
|
$this->firstIncrement = $autoIncrement; |
76
|
162 |
|
foreach ($this->children as $child) { |
77
|
1 |
|
$child->setAutoIncrement($autoIncrement); |
78
|
|
|
} |
79
|
162 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Resets the autoincrement number for games to the FirstIncrement |
84
|
|
|
* |
85
|
|
|
* @param GameContainer|null $sender |
86
|
|
|
* |
87
|
|
|
* @return GameContainer |
88
|
|
|
* @post Propagates to all children but the sender |
89
|
|
|
* @post Propagates to the parent if not the sender |
90
|
|
|
* |
91
|
|
|
* @since 0.5 |
92
|
|
|
*/ |
93
|
5 |
|
public function resetAutoIncrement(?GameContainer $sender = null) : GameContainer { |
94
|
5 |
|
$this->autoIncrement = $this->firstIncrement; |
95
|
|
|
// Propagate to parent |
96
|
5 |
|
if (isset($this->parent) && $this->parent !== $sender) { |
97
|
1 |
|
$this->parent->resetAutoIncrement($this); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
// Propagate to children |
100
|
5 |
|
foreach ($this->children as $child) { |
101
|
1 |
|
if ($child !== $sender) { |
102
|
1 |
|
$child->resetAutoIncrement($this); |
103
|
|
|
} |
104
|
|
|
} |
105
|
5 |
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return int |
110
|
|
|
* @since 0.5 |
111
|
|
|
*/ |
112
|
1 |
|
public function getFirstIncrement() : int { |
113
|
1 |
|
return $this->firstIncrement; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Adds a child container |
119
|
|
|
* |
120
|
|
|
* @param GameContainer[] $containers |
121
|
|
|
* |
122
|
|
|
* @return $this |
123
|
|
|
* @post Parent container is set for the added children |
124
|
|
|
* @post Autoincrement value is propagated to added child containers |
125
|
|
|
* @throws Exception |
126
|
|
|
*/ |
127
|
144 |
|
public function addChild(BaseContainer ...$containers) : BaseContainer { |
128
|
144 |
|
foreach ($containers as $container) { |
129
|
144 |
|
if (!$container instanceof self) { |
130
|
1 |
|
throw new InvalidArgumentException('GameContainer must contain only other GameContainers.'); |
131
|
|
|
} |
132
|
143 |
|
$container->setAutoIncrement($this->autoIncrement); |
133
|
|
|
} |
134
|
143 |
|
parent::addChild(...$containers); |
135
|
143 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
} |