<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Enum\AnnualReportType;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum as EnumAssert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\AnnualReportRepository")
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity(
* fields={"year", "type", "company"},
* errorPath="root",
* message="This annual report already exists"
* )
*/
#[ApiResource]
class AnnualReport
{
use TimestampableEntity;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer")
* @Assert\NotBlank()
* @Assert\Type(type="integer")
*/
private $year;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="annualReports")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
*/
public ?Company $company = null;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ReportMetric", cascade={"persist"}, mappedBy="annualReport")
*/
private $metrics;
/**
* @ORM\OneToMany(targetEntity="App\Entity\ReportTotal", cascade={"persist"}, mappedBy="annualReport")
*/
private $totals;
/**
* @ORM\Column(type="string", length=128)
* @Assert\NotBlank()
* @Assert\Length(max=128)
* @EnumAssert(class="App\Enum\AnnualReportType")
*/
private $type;
public $orderedMetrics;
public function __construct()
{
$this->metrics = new ArrayCollection();
$this->totals = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getYear(): ?int
{
return $this->year;
}
public function setYear(int $year): self
{
$this->year = $year;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
return $this;
}
/**
* @return Collection|ReportMetric[]
*/
public function getMetrics(): Collection
{
return $this->metrics;
}
public function addMetric(ReportMetric $metric): self
{
if (!$this->metrics->contains($metric)) {
$this->metrics[] = $metric;
$metric->setAnnualReport($this);
}
return $this;
}
public function removeMetric(ReportMetric $metric): self
{
if ($this->metrics->contains($metric)) {
$this->metrics->removeElement($metric);
// set the owning side to null (unless already changed)
if ($metric->getAnnualReport() === $this) {
$metric->setAnnualReport(null);
}
}
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getOrderedMetrics(): Collection
{
return $this->orderedMetrics;
}
public function hydrateOrderedMetrics(): void
{
foreach ($this->getMetrics() as $metric) {
switch ($this->getType()) {
case AnnualReportType::OVERALL:
$this->orderedMetrics[$metric->getReportGender()->getId()][$metric->getReportRace()->getId()][$metric->getReportJobCategory()->getId()]
= $metric->getValue();
break;
case AnnualReportType::BOARD:
$this->orderedMetrics[$metric->getReportGender() ? $metric->getReportGender()->getId() : 0][$metric->getReportRace() ? $metric->getReportRace()->getId() : 0]
= $metric->getValue();
break;
}
}
}
/**
* @return Collection|ReportTotal[]
*/
public function getTotals(): Collection
{
return $this->totals;
}
public function addTotal(ReportTotal $total): self
{
if (!$this->totals->contains($total)) {
$this->totals[] = $total;
$total->setAnnualReport($this);
}
return $this;
}
public function removeTotal(ReportTotal $total): self
{
if ($this->totals->contains($total)) {
$this->totals->removeElement($total);
// set the owning side to null (unless already changed)
if ($total->getAnnualReport() === $this) {
$total->setAnnualReport(null);
}
}
return $this;
}
}