<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\AwardRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=AwardRepository::class)
* @ApiResource(
* normalizationContext={
* "groups"={"award:read"}
* },
* denormalizationContext={
* "groups"={"award:write"}
* },
* collectionOperations={
* "get"={"security"="is_granted('ROLE_USER')"},
* "post"={"security"="is_granted('ROLE_ADMIN')"}
* },
* itemOperations={
* "get"={"security"="is_granted('ROLE_USER')"},
* "patch"={"security"="is_granted('ROLE_ADMIN')"},
* "patch_award"={
* "method"="PATCH",
* "security"="is_granted('ROLE_ADMIN')",
* "path"="/custom/awards/{id}",
* "controller"="App\Controller\Api\AwardController::patchAward",
* "input_formats"={"json"={"application/merge-patch+json"}}
* },
* "delete"={"security"="is_granted('ROLE_ADMIN')"}
* }
* )
* @ApiFilter(OrderFilter::class)
* @ApiFilter(SearchFilter::class)
* @ApiFilter(BooleanFilter::class, properties={"active"})
* @ApiFilter(GroupFilter::class, arguments={"parameterName": "groups", "overrideDefaultGroups": true, "whitelist": {
* "homepage:read"
* }})
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class Award
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"award:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"award:read", "award:write"})
* @Assert\NotNull
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"award:read", "award:write"})
*/
private $description = null;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"award:read", "award:write"})
* @Assert\NotNull(message="award.date_start.blank")
* @Assert\LessThanOrEqual(propertyPath="dateEnd", message="award.date_start.constraint_date")
*/
private $dateStart = null;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"award:read", "award:write"})
* @Assert\NotNull(message="award.date_end.blank")
* @Assert\GreaterThanOrEqual(propertyPath="dateStart", message="award.date_end.constraint_date")
*/
private $dateEnd = null;
/**
* @ORM\Column(type="boolean", options={"default": "0"})
* @Groups({"award:read", "award:write"})
*/
private $active = false;
/**
* @ORM\ManyToMany(targetEntity=TvCompany::class, inversedBy="awards")
* @Groups({"award:read", "award:write"})
*/
private $tvCompanies;
/**
* @ORM\ManyToMany(targetEntity=Company::class, inversedBy="awards")
* @Groups({"award:read", "award:write"})
* @Assert\NotNull
*/
private $companies;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"award:read", "award:write"})
*/
private $rules = null;
/**
* @ORM\OneToMany(targetEntity=AwardConfig::class, mappedBy="award", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
*/
private $awardConfigs;
/**
* @ORM\OneToMany(targetEntity=AwardLog::class, mappedBy="award", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
*/
private $awardLogs;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="update")
*/
private $updatedAt;
public function __construct()
{
$this->active = false;
$this->tvCompanies = new ArrayCollection();
$this->companies = new ArrayCollection();
$this->awardConfigs = new ArrayCollection();
$this->awardLogs = new ArrayCollection();
$this->rules = null;
$this->createdAt = new \DateTime("now");
$this->updatedAt = new \DateTime("now");
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getDateStart(): ?\DateTimeInterface
{
return $this->dateStart;
}
public function setDateStart(?\DateTimeInterface $dateStart): self
{
$this->dateStart = $dateStart;
if($this->dateStart instanceof \DateTime) $this->dateStart->setTime(0,0,0);
return $this;
}
public function getDateEnd(): ?\DateTimeInterface
{
return $this->dateEnd;
}
public function setDateEnd(?\DateTimeInterface $dateEnd): self
{
$this->dateEnd = $dateEnd;
if($this->dateEnd instanceof \DateTime) $this->dateEnd->setTime(23,59,59);
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
/**
* @return Collection|TvCompany[]
*/
public function getTvCompanies(): Collection
{
return $this->tvCompanies;
}
/**
* @var Collection|TvCompany[]
* @Groups({"award:write"})
*/
public function setTvCompanies(array $tvCompanies): self
{
$this->tvCompanies = new ArrayCollection($tvCompanies);
return $this;
}
public function addTvCompany(TvCompany $tvCompany): self
{
if (!$this->tvCompanies->contains($tvCompany)) {
$this->tvCompanies[] = $tvCompany;
}
return $this;
}
public function removeTvCompany(TvCompany $tvCompany): self
{
$this->tvCompanies->removeElement($tvCompany);
return $this;
}
/**
* @return Collection|Company[]
*/
public function getCompanies(): Collection
{
return $this->companies;
}
/**
* @var Collection|Company[]
* @Groups({"award:write"})
*/
public function setCompanies(array $companies): self
{
$this->companies = new ArrayCollection($companies);
return $this;
}
public function addCompany(Company $company): self
{
if (!$this->companies->contains($company)) {
$this->companies[] = $company;
}
return $this;
}
public function removeCompany(Company $company): self
{
$this->companies->removeElement($company);
return $this;
}
public function getRules(): ?MediaObject
{
return $this->rules;
}
public function setRules(?MediaObject $rules): self
{
$this->rules = $rules;
return $this;
}
/**
* @Groups({"award:write"})
*/
public function setRulesFile($file = null): self
{
if($file instanceof UploadedFile) {
$rules = empty($this->rules) ? new MediaObject : $this->rules;
$rules->setFile($file);
$this->setRules($rules);
}
return $this;
}
/**
* @return Collection<int, AwardConfig>
*/
public function getAwardConfigs(): Collection
{
return $this->awardConfigs;
}
public function addAwardConfig(AwardConfig $awardConfig): self
{
if (!$this->awardConfigs->contains($awardConfig)) {
$this->awardConfigs[] = $awardConfig;
$awardConfig->setAward($this);
}
return $this;
}
public function removeAwardConfig(AwardConfig $awardConfig): self
{
if ($this->awardConfigs->removeElement($awardConfig)) {
// set the owning side to null (unless already changed)
if ($awardConfig->getAward() === $this) {
$awardConfig->setAward(null);
}
}
return $this;
}
/**
* @return Collection|AwardLog[]
*/
public function getAwardLogs(): Collection
{
return $this->awardLogs;
}
public function addAwardLog(AwardLog $awardLog): self
{
if (!$this->awardLogs->contains($awardLog)) {
$this->awardLogs[] = $awardLog;
$awardLog->setAward($this);
}
return $this;
}
public function removeAwardLog(AwardLog $awardLog): self
{
if ($this->awardLogs->removeElement($awardLog)) {
// set the owning side to null (unless already changed)
if ($awardLog->getAward() === $this) {
$awardLog->setAward(null);
}
}
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}