src/Entity/AnnualReport.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Enum\AnnualReportType;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Timestampable\Traits\TimestampableEntity;
  9. use Greg0ire\Enum\Bridge\Symfony\Validator\Constraint\Enum as EnumAssert;
  10. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. /**
  13.  * @ORM\Entity(repositoryClass="App\Repository\AnnualReportRepository")
  14.  * @ORM\HasLifecycleCallbacks()
  15.  * @UniqueEntity(
  16.  *     fields={"year", "type", "company"},
  17.  *     errorPath="root",
  18.  *     message="This annual report already exists"
  19.  * )
  20.  */
  21. #[ApiResource]
  22. class AnnualReport
  23. {
  24.     use TimestampableEntity;
  25.     /**
  26.      * @ORM\Id()
  27.      * @ORM\GeneratedValue()
  28.      * @ORM\Column(type="integer")
  29.      */
  30.     private $id;
  31.     /**
  32.      * @ORM\Column(type="integer")
  33.      * @Assert\NotBlank()
  34.      * @Assert\Type(type="integer")
  35.      */
  36.     private $year;
  37.     /**
  38.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="annualReports")
  39.      * @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
  40.      */
  41.     public ?Company $company null;
  42.     /**
  43.      * @ORM\OneToMany(targetEntity="App\Entity\ReportMetric", cascade={"persist"}, mappedBy="annualReport")
  44.      */
  45.     private $metrics;
  46.     /**
  47.      * @ORM\OneToMany(targetEntity="App\Entity\ReportTotal", cascade={"persist"}, mappedBy="annualReport")
  48.      */
  49.     private $totals;
  50.     /**
  51.      * @ORM\Column(type="string", length=128)
  52.      * @Assert\NotBlank()
  53.      * @Assert\Length(max=128)
  54.      * @EnumAssert(class="App\Enum\AnnualReportType")
  55.      */
  56.     private $type;
  57.     public $orderedMetrics;
  58.     public function __construct()
  59.     {
  60.         $this->metrics = new ArrayCollection();
  61.         $this->totals = new ArrayCollection();
  62.     }
  63.     public function getId(): ?int
  64.     {
  65.         return $this->id;
  66.     }
  67.     public function getYear(): ?int
  68.     {
  69.         return $this->year;
  70.     }
  71.     public function setYear(int $year): self
  72.     {
  73.         $this->year $year;
  74.         return $this;
  75.     }
  76.     public function getCompany(): ?Company
  77.     {
  78.         return $this->company;
  79.     }
  80.     public function setCompany(?Company $company): self
  81.     {
  82.         $this->company $company;
  83.         return $this;
  84.     }
  85.     /**
  86.      * @return Collection|ReportMetric[]
  87.      */
  88.     public function getMetrics(): Collection
  89.     {
  90.         return $this->metrics;
  91.     }
  92.     public function addMetric(ReportMetric $metric): self
  93.     {
  94.         if (!$this->metrics->contains($metric)) {
  95.             $this->metrics[] = $metric;
  96.             $metric->setAnnualReport($this);
  97.         }
  98.         return $this;
  99.     }
  100.     public function removeMetric(ReportMetric $metric): self
  101.     {
  102.         if ($this->metrics->contains($metric)) {
  103.             $this->metrics->removeElement($metric);
  104.             // set the owning side to null (unless already changed)
  105.             if ($metric->getAnnualReport() === $this) {
  106.                 $metric->setAnnualReport(null);
  107.             }
  108.         }
  109.         return $this;
  110.     }
  111.     public function getType(): ?string
  112.     {
  113.         return $this->type;
  114.     }
  115.     public function setType(string $type): self
  116.     {
  117.         $this->type $type;
  118.         return $this;
  119.     }
  120.     public function getOrderedMetrics(): Collection
  121.     {
  122.         return $this->orderedMetrics;
  123.     }
  124.     public function hydrateOrderedMetrics(): void
  125.     {
  126.         foreach ($this->getMetrics() as $metric) {
  127.             switch ($this->getType()) {
  128.                 case AnnualReportType::OVERALL:
  129.                     $this->orderedMetrics[$metric->getReportGender()->getId()][$metric->getReportRace()->getId()][$metric->getReportJobCategory()->getId()]
  130.                         = $metric->getValue();
  131.                     break;
  132.                 case AnnualReportType::BOARD:
  133.                     $this->orderedMetrics[$metric->getReportGender() ? $metric->getReportGender()->getId() : 0][$metric->getReportRace() ? $metric->getReportRace()->getId() : 0]
  134.                         = $metric->getValue();
  135.                     break;
  136.             }
  137.         }
  138.     }
  139.     /**
  140.      * @return Collection|ReportTotal[]
  141.      */
  142.     public function getTotals(): Collection
  143.     {
  144.         return $this->totals;
  145.     }
  146.     public function addTotal(ReportTotal $total): self
  147.     {
  148.         if (!$this->totals->contains($total)) {
  149.             $this->totals[] = $total;
  150.             $total->setAnnualReport($this);
  151.         }
  152.         return $this;
  153.     }
  154.     public function removeTotal(ReportTotal $total): self
  155.     {
  156.         if ($this->totals->contains($total)) {
  157.             $this->totals->removeElement($total);
  158.             // set the owning side to null (unless already changed)
  159.             if ($total->getAnnualReport() === $this) {
  160.                 $total->setAnnualReport(null);
  161.             }
  162.         }
  163.         return $this;
  164.     }
  165. }