src/Entity/CompanyAdvantage.php line 124

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use App\Utils\File\FileServiceTrait;
  5. use Symfony\Component\PropertyAccess\PropertyAccess;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
  8. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  9. /**
  10.  * @ORM\Entity(repositoryClass="App\Repository\CompanyAdvantageRepository")
  11.  */
  12. class CompanyAdvantage implements \JsonSerializable, TranslatableInterface
  13. {
  14.     use TranslatableTrait;
  15.     use FileServiceTrait;
  16.     const FILE_ICON = "icon";
  17.     const FILES = [
  18.         self::FILE_ICON => [
  19.             'types' => ['png'],
  20.             'dir' => '/company_advantages',
  21.             'size' => 5*1024*1024,
  22.         ]
  23.     ];
  24.     /**
  25.      * @ORM\Id()
  26.      * @ORM\GeneratedValue()
  27.      * @ORM\Column(type="integer")
  28.      */
  29.     private $id;
  30.     /**
  31.      * @ORM\Column(type="boolean", options={"default":"1"})
  32.      */
  33.     private $active = true;
  34.     public function getId(): ?int
  35.     {
  36.         return $this->id;
  37.     }
  38.     public function getActive(): ?bool
  39.     {
  40.         return $this->active;
  41.     }
  42.     public function setActive(bool $active): self
  43.     {
  44.         $this->active = $active;
  45.         return $this;
  46.     }
  47.     /**
  48.      * @return string|null
  49.      */
  50.     public function getIconPreview(): ?string {
  51.         $file = $this->getFileService()->getFile($this, self::FILE_ICON);
  52.         if ($file) {
  53.             $filePath = $file->getFilePath() . DIRECTORY_SEPARATOR . $file->getFileNameHash();
  54.             
  55.             return $filePath;
  56.         }
  57.         return null;
  58.     }
  59.     /**
  60.      * @return File|null
  61.      */
  62.     public function getIcon(){
  63.         return $this->getFileService()->getFile($this, self::FILE_ICON);
  64.     }
  65.     /**
  66.      * @param UploadedFile|null $file
  67.      *
  68.      * @return CompanyAdvantage
  69.      */
  70.     public function setIcon(?UploadedFile $file): self{
  71.         if ($file) {
  72.             $oldFile = $this->getFileService()->getFile($this, self::FILE_ICON);
  73.             $newFile = $this->getFileService()->upload($file, $this, self::FILE_ICON);
  74.             if ($newFile && $oldFile){
  75.                 $this->getFileService()->delete($oldFile);
  76.             }
  77.         }
  78.         return $this;
  79.     }
  80.     /**
  81.      * @return self
  82.      */
  83.     public function removeIcon(): self{
  84.         $file = $this->getFileService()->getFile($this, self::FILE_ICON);
  85.         if ($file) {
  86.             $this->getFileService()->delete($file);
  87.         }
  88.         return $this;
  89.     }
  90.     public function __get($name)
  91.     {
  92.         return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $name);
  93.     }
  94.     /**
  95.      * Specify data which should be serialized to JSON
  96.      *
  97.      * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
  98.      * @return mixed data which can be serialized by <b>json_encode</b>,
  99.      * which is a value of any type other than a resource.
  100.      * @since 5.4.0
  101.      */
  102.     public function jsonSerialize() {
  103.         return [
  104.             'id' => $this->getId(),
  105.             'name' => $this->name,
  106.             'icon' => $this->getIcon()
  107.         ];
  108.     }
  109. }