src/Entity/CompanyMosaic.php line 175

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\CompanyMosaicRepository")
  10.  */
  11. class CompanyMosaic 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_mosaic',
  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="text", nullable=true)
  31.      */
  32.     private $videourl;
  33.     /**
  34.      * @ORM\Column(type="boolean", options={"default":"1"})
  35.      */
  36.     private $active = true;
  37.     /**
  38.      * @ORM\Column(type="integer", options={"default":"0"})
  39.      */
  40.     private $position;
  41.     /**
  42.      * @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="mosaic")
  43.      */
  44.     private $company;
  45.     public function getId(): ?int
  46.     {
  47.         return $this->id;
  48.     }
  49.     public function getVideourl(): ?string
  50.     {
  51.         return $this->videourl;
  52.     }
  53.     public function setVideourl(string $videourl): self
  54.     {
  55.         $this->videourl = $videourl;
  56.         return $this;
  57.     }
  58.     public function getActive(): ?bool
  59.     {
  60.         return $this->active;
  61.     }
  62.     public function setActive(bool $active): self
  63.     {
  64.         $this->active = $active;
  65.         return $this;
  66.     }
  67.     public function getPosition(): ?int
  68.     {
  69.         return $this->position;
  70.     }
  71.     public function setPosition(int $position): self
  72.     {
  73.         $this->position = $position;
  74.         return $this;
  75.     }
  76.     /**
  77.      * @return string|null
  78.      */
  79.     public function getPicturePreview(): ?string {
  80.         $file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  81.         if ($file) {
  82.             $filePath = $file->getFilePath() . DIRECTORY_SEPARATOR . $file->getFileNameHash();
  83.             
  84.             return $filePath;
  85.         }
  86.         return null;
  87.     }
  88.     /**
  89.      * @return File|null
  90.      */
  91.     public function getPicture(){
  92.         return $this->getFileService()->getFile($this, self::FILE_PICTURE);
  93.     }
  94.     /**
  95.      * @param UploadedFile|null $file
  96.      *
  97.      * @return self
  98.      */
  99.     public function setPicture(?UploadedFile $file): self{
  100.         if ($file) {
  101.             $oldFile = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  102.             $newFile = $this->getFileService()->upload($file, $this, self::FILE_PICTURE);
  103.             if ($newFile && $oldFile){
  104.                 $this->getFileService()->delete($oldFile);
  105.             }
  106.         }
  107.         return $this;
  108.     }
  109.     /**
  110.      * @return self
  111.      */
  112.     public function removePicture(): self{
  113.         $file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  114.         if ($file) {
  115.             $this->getFileService()->delete($file);
  116.         }
  117.         return $this;
  118.     }
  119.     /**
  120.      * @return Company|null
  121.      */
  122.     public function getCompany(): ?Company {
  123.         return $this->company;
  124.     }
  125.     /**
  126.      * @param Company $company
  127.      *
  128.      * @return CompanyMosaic
  129.      */
  130.     public function setCompany(Company $company): CompanyMosaic {
  131.         $this->company = $company;
  132.         return $this;
  133.     }
  134.     /**
  135.      * Specify data which should be serialized to JSON
  136.      *
  137.      * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
  138.      * @return mixed data which can be serialized by <b>json_encode</b>,
  139.      * which is a value of any type other than a resource.
  140.      * @since 5.4.0
  141.      */
  142.     public function jsonSerialize() {
  143.         return [
  144.             'id' => $this->getId(),
  145.             'active' => $this->getActive(),
  146.             'position' => $this->getPosition(),
  147.             'title' => $this->translate()->getTitle(),
  148.             'description' => $this->translate()->getDescription(),
  149.             'videourl' => $this->getVideourl(),
  150.             'picture' => $this->getPicture()
  151.         ];
  152.     }
  153. }