Skip to content

Commit

Permalink
fix(validation): first validate than create
Browse files Browse the repository at this point in the history
  • Loading branch information
thepercival committed May 29, 2024
1 parent 2963447 commit 4f99213
Show file tree
Hide file tree
Showing 35 changed files with 1,951 additions and 1,031 deletions.
1,093 changes: 874 additions & 219 deletions composer.lock

Large diffs are not rendered by default.

60 changes: 33 additions & 27 deletions domain/Batch/SelfReferee.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use SportsPlanning\Batch;
use SportsPlanning\Batch\SelfReferee\OtherPoule as SelfRefereeOtherPoule;
use SportsPlanning\Batch\SelfReferee\SamePoule as SelfRefereeSamePoule;
use SportsPlanning\Counters\GamePlacesCounterForPoule;
use SportsPlanning\Counters\PouleCounter;
use SportsPlanning\Game\Against as AgainstGame;
use SportsPlanning\Game\Together as TogetherGame;
use SportsPlanning\Place;
use SportsPlanning\Poule;
use SportsPlanning\Poule\PouleCounter;

abstract class SelfReferee
{
Expand All @@ -20,15 +21,15 @@ abstract class SelfReferee
*/
protected array $placesAsRefereeMap = [];
/**
* @var array<int,PouleCounter>
* @var array<int,GamePlacesCounterForPoule>
*/
protected array $previousTotalPouleCounterMap = [];
/**
* @var array<int|string,int>
*/
protected array $previousTotalNrOfForcedRefereePlacesMap = [];
/**
* @var array<int,PouleCounter>
* @var array<int,GamePlacesCounterForPoule>
*/
protected array $pouleCounterMap = [];

Expand Down Expand Up @@ -105,26 +106,26 @@ public function isParticipatingAsReferee(Place $placeReferee): bool
}

/**
* @param array<int,PouleCounter> $previousTotalPouleCounterMap
* @param array<int,GamePlacesCounterForPoule> $previousTotalPouleCounterMap
* @param array<int|string,int> $previousPreviousTotalNrOfForcedRefereePlacesMap
*/
public function getCopyPreviousTotals(array &$previousTotalPouleCounterMap, array &$previousPreviousTotalNrOfForcedRefereePlacesMap): void
{
foreach ($this->previousTotalPouleCounterMap as $key => $pouleCounterMap) {
$copiedPouleCounterMap = new PouleCounter(
$pouleCounterMap->getPoule(),
$pouleCounterMap->getNrOfPlacesAssigned()
foreach ($this->previousTotalPouleCounterMap as $key => $gamePlacesCounterForPoule) {
$copiedGamePlacesCounterForPoule = new GamePlacesCounterForPoule(
$gamePlacesCounterForPoule->getPoule(),
$gamePlacesCounterForPoule->getNrOfPlacesAssigned(),
$gamePlacesCounterForPoule->getNrOfGames()
);
$copiedPouleCounterMap->addNrOfGames($pouleCounterMap->getNrOfGames());
$previousTotalPouleCounterMap[$key] = $copiedPouleCounterMap;
$previousTotalPouleCounterMap[$key] = $gamePlacesCounterForPoule;
}
foreach ($this->previousTotalNrOfForcedRefereePlacesMap as $key => $nrOfForcedRefereePlace) {
$previousPreviousTotalNrOfForcedRefereePlacesMap[$key] = $nrOfForcedRefereePlace;
}
}

/**
* @param array<int,PouleCounter> $previousPreviousTotalPouleCounterMap
* @param array<int,GamePlacesCounterForPoule> $previousPreviousTotalPouleCounterMap
* @param array<string|int,int> $previousPreviousTotalNrOfForcedRefereePlacesMap
* @param self $previousBatch
* @return void
Expand Down Expand Up @@ -173,27 +174,33 @@ protected function addForcedRefereePlacesMaps(
}

/**
* @return array<int,PouleCounter>
* @return array<int,GamePlacesCounterForPoule>
*/
public function getTotalPouleCounters(): array
{
$previousTotalPouleCounterMap = [];
foreach ($this->previousTotalPouleCounterMap as $key => $it) {
$previousTotalPouleCounterMap[$key] = new PouleCounter($it->getPoule(), $it->getNrOfPlacesAssigned());
$previousTotalPouleCounterMap[$key]->addNrOfGames($it->getNrOfGames());
$previousTotalPouleCounterMap[$key] = new GamePlacesCounterForPoule(
$it->getPoule(),
$it->getNrOfPlacesAssigned(),
$it->getNrOfGames()
);
}
$pouleCounterMap = [];
foreach ($this->pouleCounterMap as $key => $it) {
$pouleCounterMap[$key] = new PouleCounter($it->getPoule(), $it->getNrOfPlacesAssigned());
$pouleCounterMap[$key]->addNrOfGames($it->getNrOfGames());
$pouleCounterMap[$key] = new GamePlacesCounterForPoule(
$it->getPoule(),
$it->getNrOfPlacesAssigned(),
$it->getNrOfGames()
);
}
return $this->addPouleCounters($previousTotalPouleCounterMap, $pouleCounterMap);
}

/**
* @param array<int,PouleCounter> $previousPreviousTotalPouleCounterMap
* @param array<int,PouleCounter> $previousBatchPouleCounterMap
* @return array<int,PouleCounter>
* @param array<int,GamePlacesCounterForPoule> $previousPreviousTotalPouleCounterMap
* @param array<int,GamePlacesCounterForPoule> $previousBatchPouleCounterMap
* @return array<int,GamePlacesCounterForPoule>
*/
protected function addPouleCounters(
array $previousPreviousTotalPouleCounterMap,
Expand All @@ -204,12 +211,11 @@ protected function addPouleCounters(
if (!array_key_exists($previousPouleNr, $previousPreviousTotalPouleCounterMap)) {
$previousPreviousTotalPouleCounterMap[$previousPouleNr] = $previousBatchPouleCounter;
} else {
$previousPreviousTotalPouleCounterMap[$previousPouleNr]->addNrOfGames(
$previousBatchPouleCounter->getNrOfGames()
);
$previousPreviousTotalPouleCounterMap[$previousPouleNr]->addNrOfAssignedPlaces(
$previousBatchPouleCounter->getNrOfPlacesAssigned()
);
$previousPreviousTotalPouleCounterMap[$previousPouleNr] =
$previousPreviousTotalPouleCounterMap[$previousPouleNr]->add(
$previousBatchPouleCounter->getNrOfPlacesAssigned(),
$previousBatchPouleCounter->getNrOfGames()
);
}
}
return $previousPreviousTotalPouleCounterMap;
Expand Down Expand Up @@ -240,7 +246,7 @@ function (Place $place): bool {
}

/**
* @return array<int,PouleCounter>
* @return array<int,GamePlacesCounterForPoule>
*/
public function getPouleCounters(): array
{
Expand All @@ -265,7 +271,7 @@ public function add(TogetherGame|AgainstGame $game): void

$poule = $game->getPoule();
if (!array_key_exists($poule->getNumber(), $this->pouleCounterMap)) {
$this->pouleCounterMap[$poule->getNumber()] = new PouleCounter($poule);
$this->pouleCounterMap[$poule->getNumber()] = new GamePlacesCounterForPoule($poule);
}
$this->pouleCounterMap[$poule->getNumber()]->add($game->getPlaces()->count());
}
Expand Down
31 changes: 16 additions & 15 deletions domain/Combinations/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@

namespace SportsPlanning\Combinations;

use SportsPlanning\PlaceCounter;
use SportsPlanning\Poule;
use SportsPlanning\Place;
use SportsHelpers\Sport\Variant\Against\H2h as AgainstH2h;
use SportsHelpers\Sport\Variant\Against\GamesPerPlace as AgainstGpp;
use SportsHelpers\Sport\Variant\Against\H2h as AgainstH2h;
use SportsPlanning\Counters\CounterForPlace;
use SportsPlanning\Counters\CounterForPlaceCombination;
use SportsPlanning\Place;
use SportsPlanning\Poule;

class Mapper
{
/**
* @param Poule $poule
* @param list<AgainstH2h|AgainstGpp> $againstVariants
* @return array<string, PlaceCombinationCounter>
* @param list<AgainstGpp|AgainstH2h> $againstVariants
* @return array<string, CounterForPlaceCombination>
*/
public function getWithMap(Poule $poule, array $againstVariants): array
{
Expand All @@ -26,7 +27,7 @@ public function getWithMap(Poule $poule, array $againstVariants): array

/**
* @param Poule $poule
* @return array<string, PlaceCombinationCounter>
* @return array<string, CounterForPlaceCombination>
*/
public function getAgainstMap(Poule $poule): array
{
Expand All @@ -37,7 +38,7 @@ public function getAgainstMap(Poule $poule): array


/**
* @param list<AgainstH2h|AgainstGpp> $againstVariants
* @param list<AgainstGpp|AgainstH2h> $againstVariants
* @return list<int>
*/
protected function getNrOfSidePlaces(array $againstVariants): array {
Expand All @@ -52,7 +53,7 @@ protected function getNrOfSidePlaces(array $againstVariants): array {
/**
* @param Poule $poule
* @param int $nrOfSidePlaces
* @return array<string, PlaceCombinationCounter>
* @return array<string, CounterForPlaceCombination>
*/
public function getPlaceCombinationMap(Poule $poule, int $nrOfSidePlaces): array
{
Expand All @@ -62,7 +63,7 @@ public function getPlaceCombinationMap(Poule $poule, int $nrOfSidePlaces): array
}

/**
* @param array<string, PlaceCombinationCounter> $map
* @param array<string, CounterForPlaceCombination> $map
* @param Poule $poule
* @param int $nrOfSidePlaces
*/
Expand All @@ -86,25 +87,25 @@ protected function addToPlaceCombinationMap(array &$map, Poule $poule, int $nrOf


/**
* @param array<string, PlaceCombinationCounter> $map
* @param array<string, CounterForPlaceCombination> $map
* @param list<Place> $places
*/
protected function addPlacesToPlaceCombinationMap(array &$map, array $places): void
{
$placeCombination = new PlaceCombination($places);
$map[$placeCombination->getIndex()] = new PlaceCombinationCounter($placeCombination);
$map[$placeCombination->getIndex()] = new CounterForPlaceCombination($placeCombination);
}


/**
* @param Poule $poule
* @return array<int, PlaceCounter>
* @return array<int, CounterForPlace>
*/
public function getPlaceMap(Poule $poule): array
public function getPlaceCounterMap(Poule $poule): array
{
$map = [];
foreach ($poule->getPlaces() as $place) {
$map[$place->getPlaceNr()] = new PlaceCounter($place);
$map[$place->getPlaceNr()] = new CounterForPlace($place);
}
return $map;
}
Expand Down
14 changes: 7 additions & 7 deletions domain/Combinations/PlaceCombination.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,30 @@

use SportsPlanning\Place;

class PlaceCombination implements \Stringable
readonly class PlaceCombination implements \Stringable
{
private string|null $index = null;
/**
* @var list<Place> $places
*/
private array $places;
private string $index;

/**
* @param list<Place> $places
*/
public function __construct(array $places)
{
uasort($places, function(Place $place1, Place $place2): int {
$uniquePlaces = array_unique($places);
uasort($uniquePlaces, function(Place $place1, Place $place2): int {
return $place1->getPlaceNr() - $place2->getPlaceNr();
});
$this->places = array_values($places);
$this->places = array_values($uniquePlaces);

$this->index = (string)$this;
}

public function getIndex(): string
{
if( $this->index === null) {
$this->index = (string)$this;
}
return $this->index;
}

Expand Down
Loading

0 comments on commit 4f99213

Please sign in to comment.