src/Entity/CompanyGallery.php line 163

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  4. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  5. use App\Utils\File\FileServiceTrait;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * @ORM\Entity(repositoryClass="App\Repository\CompanyGalleryRepository")
  10.  */
  11. class CompanyGallery implements \JsonSerializable, TranslatableInterface
  12. {
  13.     use TranslatableTrait;
  14.     use FileServiceTrait;
  15.     const FILE_PICTURE = "picture";
  16.     const FILES = [
  17.         self::FILE_PICTURE => [
  18.             'types' => ['gif', 'jpg', 'jpeg', 'png'],
  19.             'dir' => '/company_gallery',
  20.             'size' => 5*1024*1024,
  21.         ]
  22.     ];
  23.     /**
  24.      * @ORM\Id()
  25.      * @ORM\GeneratedValue()
  26.      * @ORM\Column(type="integer")
  27.      */
  28.     private $id;
  29.     /**
  30.      * @ORM\Column(type="boolean", options={"default":"1"})
  31.      */
  32.     private $active = true;
  33.     /**
  34.      * @ORM\Column(type="integer", options={"default":"0"})
  35.      */
  36.     private $position;
  37.     /**
  38.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="gallery")
  39.      */
  40.     private $company;
  41.     public function getId(): ?int
  42.     {
  43.         return $this->id;
  44.     }
  45.     public function getActive(): ?bool
  46.     {
  47.         return $this->active;
  48.     }
  49.     public function setActive(bool $active): self
  50.     {
  51.         $this->active = $active;
  52.         return $this;
  53.     }
  54.     public function getPosition(): ?int
  55.     {
  56.         return $this->position;
  57.     }
  58.     public function setPosition(int $position): self
  59.     {
  60.         $this->position = $position;
  61.         return $this;
  62.     }
  63.     /**
  64.      * @return string|null
  65.      */
  66.     public function getPicturePreview(): ?string {
  67.         $file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  68.         if ($file) {
  69.             $filePath = $file->getFilePath() . DIRECTORY_SEPARATOR . $file->getFileNameHash();
  70.             
  71.             return $filePath;
  72.         }
  73.         return null;
  74.     }
  75.     /**
  76.      * @return File|null
  77.      */
  78.     public function getPicture(){
  79.         return $this->getFileService()->getFile($this, self::FILE_PICTURE);
  80.     }
  81.     /**
  82.      * @param UploadedFile|null $file
  83.      *
  84.      * @return self
  85.      */
  86.     public function setPicture(?UploadedFile $file): self{
  87.         if ($file) {
  88.             $oldFile = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  89.             $newFile = $this->getFileService()->upload($file, $this, self::FILE_PICTURE);
  90.             if ($newFile && $oldFile){
  91.                 $this->getFileService()->delete($oldFile);
  92.             }
  93.         }
  94.         return $this;
  95.     }
  96.     /**
  97.      * @return self
  98.      */
  99.     public function removePicture(): self{
  100.         $file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  101.         if ($file) {
  102.             $this->getFileService()->delete($file);
  103.         }
  104.         return $this;
  105.     }
  106.     /**
  107.      * @return Company|null
  108.      */
  109.     public function getCompany(): ?Company {
  110.         return $this->company;
  111.     }
  112.     /**
  113.      * @param Company $company
  114.      *
  115.      * @return CompanyGallery
  116.      */
  117.     public function setCompany(Company $company): CompanyGallery {
  118.         $this->company = $company;
  119.         return $this;
  120.     }
  121.     public function __get($title)
  122.     {
  123.         return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $title);
  124.     }
  125.     /**
  126.      * Specify data which should be serialized to JSON
  127.      *
  128.      * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
  129.      * @return mixed data which can be serialized by <b>json_encode</b>,
  130.      * which is a value of any type other than a resource.
  131.      * @since 5.4.0
  132.      */
  133.     public function jsonSerialize() {
  134.         return [
  135.             'id' => $this->getId(),
  136.             'title' => $this->translate()->getTitle(),
  137.             'description' => $this->translate()->getDescription(),
  138.             'active' => $this->getActive(),
  139.             'position' => $this->getPosition(),
  140.             'picture' => $this->getPicture()
  141.         ];
  142.     }
  143. }