Habe mal ein wenig gebastelt:
PHP-Code:
<?php
interface CalendarEventInterface
{
public function getStartDate();
}
abstract class CalendarEventAbstract implements CalendarEventInterface
{
protected $startDate;
public function __construct(DateTime $startDate)
{
$this->startDate = $startDate;
}
public function getStartDate()
{
return $this->startDate;
}
}
class CalendarEventMatch extends CalendarEventAbstract
{
protected $group;
protected $match;
public function getGroup()
{
return $this->group;
}
public function setGroup(Group $group)
{
$this->group = $group;
}
public function getMatch()
{
return $this->match;
}
public function setMatch(Match $match)
{
$this->match = $match;
}
public function __toString()
{
$homeName = ($this->match->getHomeTeam() !== null)
? $this->match->getHomeTeam()->getName()
: '(wildcard)';
$awayName = ($this->match->getAwayTeam() !== null)
? $this->match->getAwayTeam()->getName()
: '(wildcard)';
return $this->group->getName() . ': ' . $homeName . ' - ' . $awayName;
}
}
class Calendar
{
protected $events;
public function addEvent(CalendarEventInterface $event)
{
$y = $event->getStartDate()->format('Y');
$m = $event->getStartDate()->format('m');
$d = $event->getStartDate()->format('d');
$this->events[$y][$m][$d][] = $event;
}
public function getEventsByDate(DateTime $date)
{
$y = $date->format('Y');
$m = $date->format('m');
$d = $date->format('d');
if (isset($this->events[$y][$m][$d])) {
return $this->events[$y][$m][$d];
}
return array();
}
}
interface FixtureStrategyInterface
{
}
class FixtureStrategyRoundRobin implements FixtureStrategyInterface
{
protected $addReturnFixtures;
public function __construct($addReturnFixtures = true)
{
$this->addReturnFixtures = $addReturnFixtures;
}
public function getFixtures(array $teams)
{
$addReturnFixtures = $this->addReturnFixtures;
if (count($teams) % 2 !== 0) {
array_push($teams, null);
}
$anz = count($teams); // Anzahl der Teams im Array $teams
$paare = $anz / 2; // Anzahl der möglichen Spielpaare
$tage = $anz - 1; // Anzahl der Spieltage pro Runde
$spiele = $paare * $tage; // Anzahl der Spiele pro Hin-/Rück-Runde
$plan = array(); // Array für den kompletten Spielplan
$xpos = $anz - 1; // höchster Key im Array $teams
$tag = 0; // Zähler für Spieltag
$spnr = 0; // Zähler für Spielnummer
$sppaar = 0; // Zähler für Spielpaar
// =====================================================================
for ($tag = 1; $tag < $anz; $tag++) {
array_splice($teams, 1, 1, array(array_pop($teams), $teams[1]));
$firstLegMatches = array();
$secondLegMatches = array();
for ($sppaar = 0; $sppaar < $paare; $sppaar++) {
$spnr++;
// wechseln zwischen G und H -Spiel:
if (($spnr % $anz !== 1) && ($sppaar % 2 === 0)) {
$hteam = $teams[$sppaar];
$gteam = $teams[$xpos - $sppaar];
} else {
$gteam = $teams[$sppaar];
$hteam = $teams[$xpos - $sppaar];
}
// Hinrunde
$firstLegMatches[] = new Match($hteam, $gteam);
// $tag, $spnr
// Rückrunde
if ($addReturnFixtures) {
$secondLegMatches[] = new Match($hteam, $gteam);
}
// $tag + $tage, $spnr + $spiele
}
$plan[] = new Matchday($tag, $firstLegMatches);
if ($addReturnFixtures) {
$plan[] = new Matchday($tag + $tage, $secondLegMatches);
}
}
// Nach Spieltagen sortieren
usort($plan, function ($a, $b) { return $a->getDay() - $b->getDay(); });
return $plan;
}
}
class Group
{
protected $name;
protected $teams;
protected $matchdays;
public function __construct($name, array $teams,
FixtureStrategyInterface $fixtureStrategy)
{
$this->name = $name;
$this->teams = $teams;
$this->matchdays = $fixtureStrategy->getFixtures($teams);
}
public function getName()
{
return $this->name;
}
public function getTeams()
{
return $this->teams;
}
public function getMatchdays()
{
return $this->matchdays;
}
}
class Team
{
protected $name;
public function __construct($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}
class Match
{
protected $homeTeam;
protected $awayTeam;
public function __construct(Team $homeTeam = null, Team $awayTeam = null)
{
$this->homeTeam = $homeTeam;
$this->awayTeam = $awayTeam;
}
public function getHomeTeam()
{
return $this->homeTeam;
}
public function getAwayTeam()
{
return $this->awayTeam;
}
}
class Matchday
{
protected $matches;
protected $day;
public function __construct($day, array $matches)
{
$this->day = $day;
$this->matches = $matches;
}
public function getMatches()
{
return $this->matches;
}
public function getDay()
{
return $this->day;
}
}
$teamify = function(array $names)
{
$teams = array();
foreach ($names as $name) {
$teams[] = new Team($name);
}
return $teams;
};
$fs = new FixtureStrategyRoundRobin(false);
$groups = array(
new Group('A', $teamify(array('Griechenland', 'Polen', 'Russland', 'Tschechien')), $fs),
new Group('B', $teamify(array('Dänemark', 'Deutschland', 'Niederlande', 'Portugal')), $fs),
new Group('C', $teamify(array('Irland', 'Italien', 'Kroatien', 'Spanien')), $fs),
new Group('D', $teamify(array('Frankreich', 'England', 'Ukraine', 'Schweden')), $fs),
#new Group('X', $teamify(array('Brasilien', 'Argentinien', 'Mexiko')), $fs)
);
$calendar = new Calendar();
// Add matches to calendar
foreach ($groups as $group) {
$dt = new DateTime();
foreach ($group->getMatchdays() as $matchday) {
/* @var $matchday Matchday */
foreach ($matchday->getMatches() as $match) {
$myDt = clone $dt;
$cem = new CalendarEventMatch($myDt);
$cem->setGroup($group);
$cem->setMatch($match);
$calendar->addEvent($cem);
$dt->add(new DateInterval('P1D'));
}
}
}
// Display calendar
$dt = new DateTime();
for ($i = 0; $i < 10; $i++) {
echo '[' . $dt->format('Y-m-d') . ']' . "\n";
$events = $calendar->getEventsByDate($dt);
if (count($events) > 0) {
foreach ($events as $event) {
echo $event . "\n";
}
} else {
echo ' -' . "\n";
}
echo "\n";
$dt->add(new DateInterval('P1D'));
}