<?php
namespace App\Entity;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Material
*
* @ORM\Table(name="material")
* @ORM\Entity(repositoryClass="App\Repository\MaterialRepository")
* @Vich\Uploadable
*/
class Material implements TranslatableInterface
{
use TranslatableTrait;
/**
* @var int
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $image;
/**
* @Vich\UploadableField(mapping="material_images", fileNameProperty="image")
* @var File
*/
private $imageFile;
/**
* @var \DateTime|null
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Exercise", mappedBy="materials")
*/
private $exercises;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\ExerciseUser", mappedBy="materials")
*/
private $exercises_user;
public function __construct()
{
$this->exercises = new ArrayCollection();
$this->exercises_user = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
/**
* @param File|null $image
* @return Material
*/
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;
}
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 $this->getName();
}
public function __toArray($imagePath = "")
{
return [
"id"=>$this->id,
"name"=>$this->getName(),
"image"=> !empty($this->getImage()) ? $imagePath . $this->getImage() : ""
];
}
/**
* @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->setMaterial($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->getMaterial() === $this) {
$exercise->remove(null);
}
}
return $this;
}
/**
* @return Collection|ExerciseUser[]
*/
public function getExercisesUser(): Collection
{
return $this->exercises_user;
}
public function addExerciseUser(ExerciseUser $exercise): self
{
if (!$this->exercises_user->contains($exercise)) {
$this->exercises_user[] = $exercise;
$exercise->setMaterial($this);
}
return $this;
}
public function removeExerciseUser(ExerciseUser $exercise): self
{
if ($this->exercises_user->contains($exercise)) {
$this->exercises_user->removeElement($exercise);
// set the owning side to null (unless already changed)
if ($exercise->getMaterial() === $this) {
$exercise->removeMaterial($this);
}
}
return $this;
}
}