|
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
|
85 |
|
public function incrementId(?GameContainer $sender = null) : void { |
|
40
|
|
|
// Increment |
|
41
|
85 |
|
$this->autoIncrement++; |
|
42
|
|
|
// Propagate to parent |
|
43
|
85 |
|
if (isset($this->parent) && $this->parent !== $sender) { |
|
44
|
39 |
|
$this->parent->incrementId($this); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
// Propagate to children |
|
47
|
85 |
|
foreach ($this->children as $child) { |
|
48
|
39 |
|
if ($child !== $sender) { |
|
49
|
27 |
|
$child->incrementId($this); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
85 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return int |
|
56
|
|
|
* @since 0.5 |
|
57
|
|
|
*/ |
|
58
|
85 |
|
public function getAutoIncrement() : int { |
|
59
|
85 |
|
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
|
70 |
|
public function setAutoIncrement(int $autoIncrement) : GameContainer { |
|
74
|
70 |
|
$this->autoIncrement = $autoIncrement; |
|
75
|
70 |
|
$this->firstIncrement = $autoIncrement; |
|
76
|
70 |
|
foreach ($this->children as $child) { |
|
77
|
|
|
$child->setAutoIncrement($autoIncrement); |
|
78
|
|
|
} |
|
79
|
70 |
|
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
|
4 |
|
public function resetAutoIncrement(?GameContainer $sender = null) : GameContainer { |
|
94
|
4 |
|
$this->autoIncrement = $this->firstIncrement; |
|
95
|
|
|
// Propagate to parent |
|
96
|
4 |
|
if (isset($this->parent) && $this->parent !== $sender) { |
|
97
|
|
|
$this->parent->resetAutoIncrement($this); |
|
|
|
|
|
|
98
|
|
|
} |
|
99
|
|
|
// Propagate to children |
|
100
|
4 |
|
foreach ($this->children as $child) { |
|
101
|
|
|
if ($child !== $sender) { |
|
102
|
|
|
$child->resetAutoIncrement($this); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
4 |
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return int |
|
110
|
|
|
* @since 0.5 |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getFirstIncrement() : int { |
|
113
|
|
|
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
|
70 |
|
public function addChild(BaseContainer ...$containers) : BaseContainer { |
|
128
|
70 |
|
foreach ($containers as $container) { |
|
129
|
70 |
|
if (!$container instanceof self) { |
|
130
|
|
|
throw new InvalidArgumentException('GameContainer must contain only other GameContainers.'); |
|
131
|
|
|
} |
|
132
|
70 |
|
$container->setAutoIncrement($this->autoIncrement); |
|
133
|
|
|
} |
|
134
|
70 |
|
parent::addChild(...$containers); |
|
135
|
70 |
|
return $this; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
} |