Skip to content

Commit

Permalink
Convenience methods (#3)
Browse files Browse the repository at this point in the history
* Add trait for common tasks in entities

* Fix bug where entity tried to create entity with already created entities in prop

* Add convenience methods
  • Loading branch information
mentisy authored Jul 3, 2022
1 parent ceb8424 commit 93dc2c0
Show file tree
Hide file tree
Showing 16 changed files with 652 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ public function __construct($properties = [])
}
$propertyValues = [];
foreach ($value as $array) {
$object = new $className((array)$array);
$propertyValues[] = $object;
if ($array instanceof EntityInterface) {
// We don't need to create an entity, since it already is an entity
$propertyValues[] = $array;
} else {
// Create new entity based on values in array
$object = new $className((array)$array);
$propertyValues[] = $object;
}
}
$this->$property = $propertyValues;
} else {
Expand Down
55 changes: 55 additions & 0 deletions src/Entity/EntityHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Avolle\Fotballdata\Entity;

/**
* Helper methods for entities
*/
trait EntityHelperTrait
{
/**
* Convert a property name's value into a datetime string
*
* @param string $property Property name to convert date
* @return string
*/
public function toDate(string $property): string
{
#/Date(1650578307030-0000)/
$pattern = '/\/Date\((\d+)-(\d+)\)\//';
if (!isset($this->$property) || !preg_match($pattern, $this->$property, $matches)) {
return 'Unknown';
}
[, $timeInMilliseconds] = $matches;

return date('Y-m-d H:i:s', (int)($timeInMilliseconds / 1000));
}

/**
* Returns an array of name parts for a given full name.
* Makes a best-guess where only the last part of the name is determined to be the surname, and the rest first name.
*
* If $indexed = false:
* - Key 0: First name
* - Key 1: Surname
*
* If $indexed = true
* - Key `firstName` = First name
* - Key `surname` = Surname
*
* @return array<string>
*/
public function toNameParts(string $fullName, bool $indexed = false): array
{
$firstName = substr($fullName, 0, strrpos($fullName, ' '));
$surname = substr($fullName, strrpos($fullName, ' ') + 1, strlen($fullName));

if ($indexed) {
return compact('firstName', 'surname');
}

return [$firstName, $surname];
}
}
153 changes: 153 additions & 0 deletions src/Entity/Game.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
*/
class Game extends Entity
{
use EntityHelperTrait;

/**
* @inheritdoc
*/
Expand All @@ -75,4 +77,155 @@ class Game extends Entity
'AwayTeamPlayer' => 'Player',
'HomeTeamPlayer' => 'Player',
];

/**
* Compiles the home team information into a Team entity
* Contact information is not compiled as it is not part of the Team entity
*
* @throws \Exception
*/
public function homeTeam(): Team
{
return $this->toTeam(true);
}

/**
* Compiles the away team information into a Team entity
* Contact information is not compiled as it is not part of the Team entity
*
* @throws \Exception
*/
public function awayTeam(): Team
{
return $this->toTeam(false);
}

/**
* Get referee info in match as Referee entity
* When calling matches/get, you get the referee as a collection of properties in this entity.
* When calling matches/people, you get the referees as an array in the $Referees property
*
* @return \Avolle\Fotballdata\Entity\Referee
* @throws \Exception
*/
public function referee(): Referee
{
[$firstName, $surname] = $this->toNameParts($this->RefereeName);

return new Referee([
'Email' => $this->RefereeEmail,
'MobilePhone' => $this->RefereeMobilePhone,
'PersonInfoHidden' => $this->RefereePersonInfoHidden,
'RefereeClub' => $this->RefereeClub,
'RefereeClubId' => $this->RefereeClubId,
'RefereeId' => $this->RefereeId,
'FirstName' => $firstName,
'SurName' => $surname,
'RefereeNumber' => $this->RefereeNumber,
]);
}

/**
* Did your team win the match
*
* @param int $yourTeamId The teamId for your team
* @return bool
*/
public function won(int $yourTeamId): bool
{
if ($this->isHome($yourTeamId)) {
return $this->HomeTeamGoals > $this->AwayTeamGoals;
}

return $this->AwayTeamGoals > $this->HomeTeamGoals;
}

/**
* Did the game end up a draw
*
* @return bool
*/
public function draw(): bool
{
return $this->HomeTeamGoals === $this->AwayTeamGoals;
}

/**
* Did your team lose the match
*
* @param int $yourTeamId The teamId for your team
* @return bool
*/
public function lost(int $yourTeamId): bool
{
if ($this->isHome($yourTeamId)) {
return $this->HomeTeamGoals < $this->AwayTeamGoals;
}

return $this->AwayTeamGoals < $this->HomeTeamGoals;
}

/**
* Is your team at home. Is tested against the `Fotballdata` configuration `clubId`
*
* @param int $yourTeamId The teamId for your team
* @return bool
*/
public function isHome(int $yourTeamId): bool
{
return $yourTeamId === $this->HomeTeamId;
}

/**
* Is match in the future
*
* @return bool
*/
public function isFuture(): bool
{
$matchTime = $this->toDate('MatchStartDate');
$matchTime = strtotime($matchTime);
$now = time();

return $now < $matchTime;
}

/**
* Is match in the past
*
* @return bool
*/
public function isPast(): bool
{
return !$this->isFuture();
}

/**
* Compiles team information into a Team entity. If $home is true, returns home team's information.
* Otherwise, returns away team
*
* @param bool $home Whether to compile the home team or the away team
* @return \Avolle\Fotballdata\Entity\Team
* @throws \Exception
*/
protected function toTeam(bool $home): Team
{
[$FirstName, $SurName] = $this->toNameParts(
$home
? $this->HomeTeamContactPersonName
: $this->AwayTeamContactPersonName
);
$MobilePhone = $home ? $this->HomeTeamContactPersonMobilePhone : $this->AwayTeamContactPersonMobilePhone;
$Email = $home ? $this->HomeTeamContactPersonEmail : $this->AwayTeamContactPersonEmail;
$PersonInfoHidden = $home ? $this->HomeTeamContactPersonInfoHidden : $this->AwayTeamContactPersonInfoHidden;

$person = new Person(compact('FirstName', 'SurName', 'MobilePhone', 'Email', 'PersonInfoHidden'));

$ClubId = $home ? $this->HomeTeamClubId : $this->AwayTeamClubId;
$TeamId = $home ? $this->HomeTeamId : $this->AwayTeamId;
$TeamName = $home ? $this->HomeTeamName : $this->AwayTeamName;
$Persons = [$person];

return new Team(compact('ClubId', 'TeamId', 'TeamName', 'Persons'));
}
}
10 changes: 10 additions & 0 deletions src/Entity/Person.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class Person extends Entity
23 => 'Inkluderingsansvarlig',
];

/**
* Get the person's full name
*
* @return string
*/
public function fullName(): string
{
return $this->FirstName . ' ' . $this->SurName;
}

public function toRoleName(): string
{
if (!isset($this->RoleId, self::ROLES[$this->RoleId])) {
Expand Down
9 changes: 9 additions & 0 deletions src/Entity/Player.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,13 @@
*/
class Player extends Entity
{
/**
* Get the player's full name
*
* @return string
*/
public function fullName(): string
{
return $this->FirstName . ' ' . $this->SurName;
}
}
11 changes: 11 additions & 0 deletions src/Entity/Referee.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* @property string $FirstName
* @property string $SurName
* @property bool $PersonInfoHidden
* @property int $RefereeTypeId
* @property string $RefereeType
* @property string $Email
* @property string $MobilePhone
* @property int $RefereeClubId
Expand All @@ -20,4 +22,13 @@
*/
class Referee extends Entity
{
/**
* Get the referee's full name
*
* @return string
*/
public function fullName(): string
{
return $this->FirstName . ' ' . $this->SurName;
}
}
1 change: 1 addition & 0 deletions src/Entity/Season.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
*/
class Season extends Entity
{
use EntityHelperTrait;
}
2 changes: 2 additions & 0 deletions src/Entity/Stadium.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
*/
class Stadium extends Entity
{
use EntityHelperTrait;

/*
* @inheritdoc
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Entity/Team.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
*/
class Team extends Entity
{
/**
* @inheritdoc
*/
protected array $hasMany = [
'Persons' => 'Person',
];

/*
* @inheritdoc
*/
Expand Down
2 changes: 2 additions & 0 deletions src/Entity/Tournament.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
*/
class Tournament extends Entity
{
use EntityHelperTrait;

/*
* @inheritdoc
*/
Expand Down
Loading

0 comments on commit 93dc2c0

Please sign in to comment.