src/Entity/Material.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\Collection;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  7. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  10. /**
  11.  * Material
  12.  *
  13.  * @ORM\Table(name="material")
  14.  * @ORM\Entity(repositoryClass="App\Repository\MaterialRepository")
  15.  * @Vich\Uploadable
  16.  */
  17. class Material implements TranslatableInterface
  18. {
  19.     use TranslatableTrait;
  20.     /**
  21.      * @var int
  22.      *
  23.      * @ORM\Column(name="id", type="integer", nullable=false)
  24.      * @ORM\Id
  25.      * @ORM\GeneratedValue(strategy="IDENTITY")
  26.      */
  27.     private $id;
  28.     /**
  29.      * @ORM\Column(type="string", length=255, nullable=true)
  30.      */
  31.     private $image;
  32.     /**
  33.      * @Vich\UploadableField(mapping="material_images", fileNameProperty="image")
  34.      * @var File
  35.      */
  36.     private $imageFile;
  37.     /**
  38.      * @var \DateTime|null
  39.      *
  40.      * @ORM\Column(name="updated_at", type="datetime", nullable=true)
  41.      */
  42.     private $updatedAt;
  43.     /**
  44.      * @ORM\ManyToMany(targetEntity="App\Entity\Exercise", mappedBy="materials")
  45.      */
  46.     private $exercises;
  47.     /**
  48.      * @ORM\ManyToMany(targetEntity="App\Entity\ExerciseUser", mappedBy="materials")
  49.      */
  50.     private $exercises_user;
  51.     public function __construct()
  52.     {
  53.         $this->exercises = new ArrayCollection();
  54.         $this->exercises_user = new ArrayCollection();
  55.     }
  56.     public function getId(): ?int
  57.     {
  58.         return $this->id;
  59.     }
  60.     /**
  61.      * @param File|null $image
  62.      * @return Material
  63.      */
  64.     public function setImageFile(File $image null)
  65.     {
  66.       $this->imageFile $image;
  67.         // VERY IMPORTANT:
  68.         // It is required that at least one field changes if you are using Doctrine,
  69.         // otherwise the event listeners won't be called and the file is lost
  70.         if ($image) {
  71.             // if 'updatedAt' is not defined in your entity, use another property
  72.             $this->updatedAt = new \DateTime('now');
  73.         }
  74.         return $this;
  75.     }
  76.     public function getImageFile()
  77.     {
  78.         return $this->imageFile;
  79.     }
  80.     public function getImage(): ?string
  81.     {
  82.         return $this->image;
  83.     }
  84.     public function setImage(?string $image): self
  85.     {
  86.         $this->image $image;
  87.         return $this;
  88.     }
  89.     public function __call($method$arguments)
  90.     {
  91.       $method = ('get' === substr($method03) || 'set' === substr($method03)) ? $method 'get'ucfirst($method);
  92.       return $this->proxyCurrentLocaleTranslation($method$arguments);
  93.     }
  94.     public function __get($name)
  95.     {
  96.       $method 'get'ucfirst($name);
  97.       $arguments = [];
  98.       return $this->proxyCurrentLocaleTranslation($method$arguments);
  99.     }
  100.     /**
  101.      * @return mixed
  102.      */
  103.     public function __toString()
  104.     {
  105.       return $this->getName();
  106.     }
  107.     public function __toArray($imagePath "")
  108.     {
  109.       return [
  110.         "id"=>$this->id,
  111.         "name"=>$this->getName(),
  112.         "image"=> !empty($this->getImage()) ? $imagePath $this->getImage() : ""
  113.       ];
  114.     }
  115.     /**
  116.      * @return Collection|Exercise[]
  117.      */
  118.     public function getExercises(): Collection
  119.     {
  120.         return $this->exercises;
  121.     }
  122.     public function addExercise(Exercise $exercise): self
  123.     {
  124.         if (!$this->exercises->contains($exercise)) {
  125.             $this->exercises[] = $exercise;
  126.             $exercise->setMaterial($this);
  127.         }
  128.         return $this;
  129.     }
  130.     public function removeExercise(Exercise $exercise): self
  131.     {
  132.         if ($this->exercises->contains($exercise)) {
  133.             $this->exercises->removeElement($exercise);
  134.             // set the owning side to null (unless already changed)
  135.             if ($exercise->getMaterial() === $this) {
  136.                 $exercise->remove(null);
  137.             }
  138.         }
  139.         return $this;
  140.     }
  141.     /**
  142.      * @return Collection|ExerciseUser[]
  143.      */
  144.     public function getExercisesUser(): Collection
  145.     {
  146.         return $this->exercises_user;
  147.     }
  148.     public function addExerciseUser(ExerciseUser $exercise): self
  149.     {
  150.         if (!$this->exercises_user->contains($exercise)) {
  151.             $this->exercises_user[] = $exercise;
  152.             $exercise->setMaterial($this);
  153.         }
  154.         return $this;
  155.     }
  156.     public function removeExerciseUser(ExerciseUser $exercise): self
  157.     {
  158.         if ($this->exercises_user->contains($exercise)) {
  159.             $this->exercises_user->removeElement($exercise);
  160.             // set the owning side to null (unless already changed)
  161.             if ($exercise->getMaterial() === $this) {
  162.                 $exercise->removeMaterial($this);
  163.             }
  164.         }
  165.         return $this;
  166.     }
  167. }