<?php
namespace App\Entity;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\{Collection, ArrayCollection};
use App\Repository\ReleaseRepository;
/**
* Release
*/
#[ORM\Table('releases')]
#[ORM\Index(name: 'FK__project', columns: ['project'])]
#[ORM\Entity(ReleaseRepository::class)]
class Release implements \JsonSerializable
{
#[ORM\Column('id', 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue('IDENTITY')]
private ?int $id = null;
#[ORM\Column('path', 'string')]
private ?string $path = null;
#[ORM\Column('version', 'string')]
private ?string $version = null;
#[ORM\ManyToOne(Project::class)]
#[ORM\JoinColumn('project')]
private ?Project $project = null;
/**
* @var ArrayCollection<ReleaseDescription>|PersistentCollection<ReleaseDescription>
*/
#[ORM\OneToMany('release', ReleaseDescription::class, ['persist', 'remove'], 'EXTRA_LAZY', true)]
private Collection $descriptions;
#[ORM\Column('created', 'datetime_immutable', nullable: true)]
private ?\DateTimeImmutable $created;
public function __construct()
{
$this->descriptions = new ArrayCollection();
$this->created = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function setId(int $id): self
{
$this->id = $id;
return $this;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(string $path): self
{
$this->path = $path;
return $this;
}
public function getVersion(): ?string
{
return $this->version;
}
public function setVersion(string $version): self
{
$this->version = $version;
return $this;
}
public function getProjectTitle(): ?string
{
return $this->project?->getTitle();
}
public function getProject(): ?Project
{
return $this->project;
}
public function setProject(Project $project): self
{
$this->project = $project;
return $this;
}
public function getCreated(): ?\DateTimeImmutable
{
return $this->created;
}
public function getDescriptions(): array
{
return $this->descriptions->toArray();
}
public function setDescriptions(array $descriptions): self
{
$this->descriptions->clear();
foreach ($descriptions as $description) {
$this->addDescription($description);
}
return $this;
}
public function addDescription(ReleaseDescription $description): void
{
$this->descriptions->contains($description) or $this->descriptions->add($description);
$description->setRelease($this);
}
public function removeDescription(ReleaseDescription $description): void
{
!$this->descriptions->contains($description) or $this->descriptions->remove($description);
}
public function jsonSerialize(): array
{
return [
'id' => $this->getId(),
'path' => $this->getPath(),
'version' => $this->getVersion(),
'descriptions' => array_map(
fn (ReleaseDescription $description) => $description->jsonSerialize(),
$this->getDescriptions()
),
'created' => $this->getCreated(),
'project' => [
'id' => $this->getProject()->getId(),
'title' => $this->getProject()->getTitle(),
],
];
}
}