Introduction
Category is a class that is used in order to separate a category into multiple categories with an ability to progress teams from one category to the other.
Creating a new category class
Basic construction
include_once 'vendor/autoload.php';
$tournament = new TournamentGenerator\Tournament('Tournament name');
$category = new TournamentGenerator\Category('Category name');
$tournament->addCategory($category);
From a category class
Recomended
This automatically assigns the category to the category.
include_once 'vendor/autoload.php';
$tournament = new TournamentGenerator\Tournament('Tournament name');
$category = $tournament->category('Category name');
Properties
Scope | Name | Type | Default | Description |
---|---|---|---|---|
protected | $name |
string |
'' |
Name of the category |
protected | $id |
string|int |
null |
Id of the category |
private | $rounds |
array |
[] |
List of category rounds |
private | $teams |
array |
[] |
List of teams |
private | $allowSkip |
bool |
false |
If true , generator will skip unplayed games without throwing exception |
Methods
Method list
Scope | Name | Return | Description |
---|---|---|---|
public | __construct |
$this |
Construct method |
public | __toString |
string |
Returns the name of the category |
public | setName |
$this |
Sets name of the category. |
public | getName |
string |
Gets name of the category. |
public | setId |
$this |
Sets id of the category. |
public | getId |
string|int |
Gets id of the category. |
public | allowSkip |
$this |
Allow skipping of unplayed games while progressing. |
public | disallowSkip |
$this |
Disllow skipping of unplayed games while progressing. |
public | setSkip |
$this |
Sets whether to skip unplayed games while progressing or not. |
public | getSkip |
bool |
Gets whether to skip unplayed games while progressing or not. |
public | addRound |
$this |
Adds created Round to Category. |
public | round |
TournamentGenerator\Round |
Creates and adds new Round to Category. |
public | getRounds |
array |
Gets an array of all Rounds from Category. |
public | getGroups |
array |
Gets an array of all Groups from Category. |
public | addTeam |
$this |
Adds created Team to Category. |
public | team |
new TournamentGenerator\Team |
Creates and adds new Team to Category. |
public | getTeams |
array |
Gets an array of all Teams from Category. |
public | sortTeams |
array |
Sorts all Teams from Category and returns them. |
public | getGames |
array |
Gets an array of all Games from Category. |
public | splitTeams |
$this |
Splits all Teams from Category to given Rounds (or all Rounds from a Category). |
public | genGamesSimulate |
array |
Generate and simulate all Games from Category without real teams (just to export) and returns array of all Games or caculated category time. |
public | genGamesSimulateReal |
array |
Generate and simulate all Games from Category with real teams (just as it was played for real) and returns array of all Games or caculated category time. |
Category __construct(string $name, string|int $id = null)
Creates a new Category class
Parameters
Name | Type | Default | Description |
---|---|---|---|
$name |
string |
'' |
Name of the category |
$id |
string|int |
'' |
Name of the category |
Return value
new TournamentGenerator\Category();
Code
public function __construct(string $name = '', $id = null) {
$this->setName($name);
$this->setId(isset($id) ? $id : uniqid());
}
public string __toString()
Returns category name
Return value
string $this->name;
Code
public function __toString() {
return $this->name;
}
public Category setName(string $name)
Sets the name of the category.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$name |
string |
Name of the category |
Return value
TournamentGenerator\Category $this
Code
public function setName(string $name) {
$this->name = $name;
return $this;
}
public string getName()
Gets the name of the category.
Return value
string $this->name;
Code
public function getName() {
return $this->name;
}
public Category setId(string|int $id)
Sets the name of the category.
Parameters
Id | Type | Default | Description |
---|---|---|---|
$id |
string|int |
Id of the category |
Return value
TournamentGenerator\Category $this
Code
public function setId($id) {
if (!is_string($id) && !is_int($id)) {
$this->id = uniqid();
throw new \Exception('Unsupported id type ('.gettype($id).') - expected type of string or int');
}
$this->id = $id;
}
public string|int getId()
Gets the name of the category.
Return value
string|int $this->id;
Code
public function getId() {
return $this->id;
}
public Category allowSkip()
Allow skipping of unplayed games while progressing.
Return value
TournamentGenerator\Category $this
Code
public function allowSkip(){
$this->allowSkip = true;
return $this;
}
public Category disallowSkip()
Disallow skipping of unplayed games while progressing.
Return value
TournamentGenerator\Category $this
Code
public function disallowSkip(){
$this->allowSkip = false;
return $this;
}
public Category setSkip(bool $skip)
Sets whether an unplayed games should be skipped while progressing or not.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$skip |
bool |
Skip or not |
Return value
TournamentGenerator\Category $this
Code
public function setSkip(bool $skip) {
$this->allowSkip = $skip;
return $this;
}
public bool getSkip()
Gets whether an unplayed games should be skipped while progressing or not.
Return value
bool $this->allowSkip
Code
public function getSkip(bool $skip) {
return $this->allowSkip;
}
public Category addRound(TournamentGenerator\Round ...$rounds)
Adds created Round to Category.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$rounds |
TournamentGenerator\Round |
[] |
One or more instances of Round class. |
Return value
$this
Code
public function addRound(Round ...$rounds) {
foreach ($rounds as $round) {
$this->rounds[] = $round;
}
return $this;
}
public Round round(string $name)
Creates and adds new Round to Category.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$name |
string |
'' |
Name of the new Round. |
$id |
string|int |
null |
Id of the new Round. |
Return value
new TournamentGenerator\Round
Code
public function round(string $name = '', $id = null) {
$r = new Round($name, $id);
$this->rounds[] = $r->setSkip($this->allowSkip);
return $r;
}
public array getRounds()
Gets an array of all Rounds from Category.
Return value
array of TournamentGenerator\Round
Code
public function getRounds() {
if (count($this->categories) > 0) {
$rounds = [];
foreach ($this->categories as $category) {
$rounds = array_merge($rounds, $category->getRounds());
}
return $rounds;
}
return $this->rounds;
}
public array getGroups()
Gets an array of all Groups from Category.
Return value
array of TournamentGenerator\Group
Code
public function getGroups() {
$groups = [];
foreach ($this->getRounds() as $round) {
$groups = array_merge($groups, $round->getGroups());
}
return $groups;
}
public Category addTeam(TournamentGenerator\Team ...$teams)
Adds created Team to Category.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$teams |
TournamentGenerator\Team |
[] |
One or more instances of Team class. |
Return value
$this
Code
public function addTeam(Team ...$teams) {
foreach ($teams as $team) {
$this->teams[] = $team;
}
return $this;
}
public Team team(string $name)
Creates and adds new Team to Category.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$name |
string |
'' |
Name of the new Team. |
$id |
string|int |
null |
Id of the new Team. |
Return value
new TournamentGenerator\Team
Code
public function team(string $name = '', $id = null) {
$t = new Team($name, $id);
$this->teams[] = $t;
return $t;
}
public array getTeams(bool $ordered = false, $ordering = POINTS)
Gets an array of all Teams from Category.
If passed true
as the first argument, teams will be ordered.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$ordered |
bool |
false |
If teams should be ordered. |
$ordering |
\TournamentGenerator\Constants::POINTS | \TournamentGenerator\Constants::SCORE |
\TournamentGenerator\Constants::POINTS |
What to order the teams by. |
$filters |
array |
[] |
Filters to filter the teams. |
Return value
array of TournamentGenerator\Team
Code
public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) {
$teams = $this->teams;
foreach ($this->rounds as $round) {
$teams = \array_merge($teams, $round->getTeams());
}
$teams = \array_unique($teams);
$this->teams = $teams;
if ($ordered) $teams = $this->sortTeams($ordering);
// APPLY FILTERS
$filter = new Filter($this->getGroups(), $filters);
$filter->filter($teams);
return $teams;
}
public array sortTeams($ordering = POINTS)
Sorts all Teams from Category and returns them.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$ordering |
\TournamentGenerator\Constants::POINTS | \TournamentGenerator\Constants::SCORE |
\TournamentGenerator\Constants::POINTS |
What to order the teams by. |
$filters |
array |
[] |
Filters to filter the teams. |
Return value
array of TournamentGenerator\Team
Code
public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) {
$teams = [];
for ($i = count($this->rounds)-1; $i >= 0; $i--) {
$rTeams = array_filter($this->rounds[$i]->getTeams(true, $ordering), function($a) use ($teams) { return !in_array($a, $teams); });
$teams = array_merge($teams, $rTeams);
}
$this->teams = $teams;
// APPLY FILTERS
$filter = new Filter($this->getGroups(), $filters);
$filter->filter($teams);
return $teams;
}
public array getGames()
Gets an array of all Games from Category.
Return value
array of TournamentGenerator\Game
Code
public function getGames() {
$games = [];
foreach ($this->getRounds() as $round) {
$games = array_merge($games, $round->getGames());
}
return $games;
}
public Category splitTeams(TournamentGenerator\Round ...$wheres)
Split all Teams from Category into given Rounds. If no argument is given, method will split into all available Rounds in Category.
Parameters
Name | Type | Default | Description |
---|---|---|---|
$wheres |
TournamentGenerator\Round |
[] |
One or more instances of Round class to split the teams into. |
Return value
$this
Code
public function splitTeams(Round ...$wheres) {
if (count($wheres) === 0) $wheres = $this->getRounds();
$teams = $this->getTeams();
shuffle($teams);
while (count($teams) > 0) {
foreach ($wheres as $where) {
if (count($teams) > 0) $where->addTeam(array_shift($teams));
}
}
foreach ($wheres as $where) {
$where->splitTeams();
}
return $this;
}
public array genGamesSimulate(bool $returnTime)
Generate and simulate all Games from Category without real teams (just to export) and returns array of all Games. It uses BlankTeam class for progressing.
Return value
array of TournamentGenerator\Games
Code
public function genGamesSimulate() {
$games = Utilis\Simulator::simulateCategory($this);
return $games;
}
public array genGamesSimulateReal(bool $returnTime)
Generate and simulate all Games from Category with real teams (just as it was played for real) and returns array of all Games.
Could be used for testing and demonstration purposes.
Return value
array of TournamentGenerator\Games
Code
public function genGamesSimulateReal() {
$games = Utilis\Simulator::simulateCategoryReal($this);
return $games;
}