welcome back to dyb-tech
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace DMD\LaLigaApi\Dto;
|
||||
|
||||
use DMD\LaLigaApi\Entity\Season;
|
||||
use DMD\LaLigaApi\Exception\ValidationException;
|
||||
|
||||
class SeasonDto
|
||||
{
|
||||
public int $id;
|
||||
public \DateTime $dateStart;
|
||||
public array $teamDtoList;
|
||||
public array $facilityDtoList;
|
||||
public int $leagueId;
|
||||
public array $gameDtoList;
|
||||
public array $seasonDataDtoList;
|
||||
public bool $active;
|
||||
public array $validationErrors;
|
||||
|
||||
public function toArray(): array
|
||||
{
|
||||
$teamList = [];
|
||||
$facilityList = [];
|
||||
if (!empty($this->teamDtoList))
|
||||
{
|
||||
foreach ($this->teamDtoList as $teamDto)
|
||||
{
|
||||
$teamList[] = $teamDto->toArray();
|
||||
}
|
||||
}
|
||||
if (!empty($this->facilityDtoList))
|
||||
{
|
||||
foreach ($this->facilityDtoList as $facilityDto)
|
||||
{
|
||||
$facilityList[] = $facilityDto->toArray();
|
||||
}
|
||||
}
|
||||
return [
|
||||
'id' => $this->id ?? null,
|
||||
'dateStart' => !empty($this->dateStart) ? $this->dateStart->format('Y-m'): null,
|
||||
'leagueId' => $this->leagueId ?? null,
|
||||
'teamList' => $teamList,
|
||||
'facilityList' => $facilityList,
|
||||
'active' => $this->active ?? null
|
||||
];
|
||||
}
|
||||
|
||||
public function fillFromArray(array $dataList): void
|
||||
{
|
||||
if (isset($dataList['id']))
|
||||
{
|
||||
$this->id = (int) $dataList['id'];
|
||||
}
|
||||
if (!empty($dataList['dateStart']))
|
||||
{
|
||||
$dateStart = \DateTime::createFromFormat('Y-m' ,$dataList['dateStart']);
|
||||
if ($dateStart)
|
||||
{
|
||||
$this->dateStart = $dateStart;
|
||||
}
|
||||
}
|
||||
if (isset($dataList['active']))
|
||||
{
|
||||
$this->active = $dataList['active'];
|
||||
}
|
||||
if (isset($dataList['teamList']))
|
||||
{
|
||||
foreach ($dataList['teamList'] as $teamName)
|
||||
{
|
||||
$teamDto = new TeamDto();
|
||||
$teamDto->name = $teamName;
|
||||
$this->teamDtoList[] = $teamDto;
|
||||
}
|
||||
}
|
||||
if (!empty($dataList['facilityList']))
|
||||
{
|
||||
foreach ($dataList['facilityList'] as $facilityItem)
|
||||
{
|
||||
$facilityDto = new FacilityDto();
|
||||
$facilityDto->fillFromArray($facilityItem);
|
||||
$this->facilityDtoList[] = $facilityDto;
|
||||
}
|
||||
}
|
||||
if (isset($dataList['gameList']))
|
||||
{
|
||||
foreach ($dataList['gameList'] as $gameItem)
|
||||
{
|
||||
$gameDto = new GameDto();
|
||||
$gameDto->fillFromArray($gameItem);
|
||||
$this->gameDtoList[] = $gameDto;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function fillFromObject(Season $seasonObj): void
|
||||
{
|
||||
if ($seasonObj->getId() !== null)
|
||||
{
|
||||
$this->id = $seasonObj->getId();
|
||||
}
|
||||
if ($seasonObj->getDateStart() !== null)
|
||||
{
|
||||
$this->dateStart = $seasonObj->getDateStart();
|
||||
}
|
||||
$leagueObj = $seasonObj->getLeague();
|
||||
if ($leagueObj !== null)
|
||||
{
|
||||
$this->leagueId = $leagueObj->getId();
|
||||
}
|
||||
if ($leagueObj->isActive() !== null)
|
||||
{
|
||||
$this->active = $leagueObj->isActive();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function validate(): void
|
||||
{
|
||||
if (!empty($this->validationErrors))
|
||||
{
|
||||
throw new ValidationException($this->validationErrors);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user