src/Entity/CompanyAboutUs.php line 146

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\CompanyAboutUsRepository")
  10.  */
  11. class CompanyAboutUs 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_about_us',
  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\ManyToOne(targetEntity="App\Entity\Company", inversedBy="aboutUs")
  35.      */
  36.     private $company;
  37.     public function getId(): ?int
  38.     {
  39.         return $this->id;
  40.     }
  41.     public function getActive(): ?bool
  42.     {
  43.         return $this->active;
  44.     }
  45.     public function setActive(bool $active): self
  46.     {
  47.         $this->active = $active;
  48.         return $this;
  49.     }
  50.     /**
  51.      * @return string|null
  52.      */
  53.     public function getPicturePreview(): ?string {
  54.         $file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  55.         if ($file) {
  56.             $filePath = $file->getFilePath() . DIRECTORY_SEPARATOR . $file->getFileNameHash();
  57.             
  58.             return $filePath;
  59.         }
  60.         return null;
  61.     }
  62.     /**
  63.      * @return File|null
  64.      */
  65.     public function getPicture(){
  66.         return $this->getFileService()->getFile($this, self::FILE_PICTURE);
  67.     }
  68.     /**
  69.      * @param UploadedFile|null $file
  70.      *
  71.      * @return Candidate
  72.      */
  73.     public function setPicture(?UploadedFile $file): self{
  74.         if ($file) {
  75.             $oldFile = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  76.             $newFile = $this->getFileService()->upload($file, $this, self::FILE_PICTURE);
  77.             if ($newFile && $oldFile){
  78.                 $this->getFileService()->delete($oldFile);
  79.             }
  80.         }
  81.         return $this;
  82.     }
  83.     /**
  84.      * @return self
  85.      */
  86.     public function removePicture(): self{
  87.         $file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
  88.         if ($file) {
  89.             $this->getFileService()->delete($file);
  90.         }
  91.         return $this;
  92.     }
  93.     /**
  94.      * @return Company|null
  95.      */
  96.     public function getCompany(): ?Company {
  97.         return $this->company;
  98.     }
  99.     /**
  100.      * @param Company $company
  101.      *
  102.      * @return CompanyAboutUs
  103.      */
  104.     public function setCompany(Company $company): CompanyAboutUs {
  105.         $this->company = $company;
  106.         return $this;
  107.     }
  108.     public function __get($title)
  109.     {
  110.         return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $title);
  111.     }
  112.     /**
  113.      * Specify data which should be serialized to JSON
  114.      *
  115.      * @link https://php.net/manual/en/jsonserializable.jsonserialize.php
  116.      * @return mixed data which can be serialized by <b>json_encode</b>,
  117.      * which is a value of any type other than a resource.
  118.      * @since 5.4.0
  119.      */
  120.     public function jsonSerialize() {
  121.         return [
  122.             'id' => $this->getId(),
  123.             'active' => $this->getActive(),
  124.             'title' => $this->translate()->getTitle(),
  125.             'description' => $this->translate()->getDescription(),
  126.             'picture' => $this->getPicture()
  127.         ];
  128.     }
  129. }