<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
/**
* @ORM\Entity(repositoryClass="App\Repository\CategoryRepository")
* @Vich\Uploadable
*/
class Category implements TranslatableInterface
{
use TranslatableTrait;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* @ORM\Column(type="boolean")
*/
private $isVisible;
/**
* @Vich\UploadableField(mapping="exercise_images", fileNameProperty="image")
* @var File
*/
private $imageFile;
/**
* @ORM\Column(type="datetime", nullable=true)
* @var \DateTime
*/
private $updatedAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Exercise", mappedBy="category", orphanRemoval=true)
*/
private $exercises;
public function __construct()
{
$this->exercises = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return \DateTime
*/
public function getUpdatedAt(): \DateTime
{
return $this->updatedAt;
}
/**
* @param \DateTime $updatedAt
* @return Category
*/
public function setUpdatedAt(\DateTime $updatedAt): Category
{
$this->updatedAt = $updatedAt;
return $this;
}
/**
* @param File|null $image
* @return Category
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($image) {
// if 'updatedAt' is not defined in your entity, use another property
$this->updatedAt = new \DateTime('now');
}
return $this;
}
public function getImageFile()
{
return $this->imageFile;
}
public function getImage(): ?string
{
return $this->image;
}
public function setImage(?string $image): self
{
$this->image = $image;
return $this;
}
/**
* @return Collection|Exercise[]
*/
public function getExercises(): Collection
{
return $this->exercises;
}
public function addExercise(Exercise $exercise): self
{
if (!$this->exercises->contains($exercise)) {
$this->exercises[] = $exercise;
$exercise->setCategory($this);
}
return $this;
}
public function removeExercise(Exercise $exercise): self
{
if ($this->exercises->contains($exercise)) {
$this->exercises->removeElement($exercise);
// set the owning side to null (unless already changed)
if ($exercise->getCategory() === $this) {
$exercise->setCategory(null);
}
}
return $this;
}
public function __call($method, $arguments)
{
$method = ('get' === substr($method, 0, 3) || 'set' === substr($method, 0, 3)) ? $method : 'get'. ucfirst($method);
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
public function __get($name)
{
$method = 'get'. ucfirst($name);
$arguments = [];
return $this->proxyCurrentLocaleTranslation($method, $arguments);
}
/**
* @return mixed
*/
public function __toString()
{
return self::getName();
}
public function __toArray($imagePath = "")
{
return [
"id"=>$this->getId(),
"name"=>$this->getName(),
"description"=>$this->getDescription(),
"image"=> !empty($this->getImage()) ? $imagePath . $this->getImage() : ""
];
}
public function getIsVisible()
{
return $this->isVisible;
}
public function setIsVisible(bool $isVisible): self
{
$this->isVisible = $isVisible;
return $this;
}
}