<?php
namespace App\Entity;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use App\Utils\File\FileServiceTrait;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="App\Repository\CompanyGalleryRepository")
*/
class CompanyGallery implements \JsonSerializable, TranslatableInterface
{
use TranslatableTrait;
use FileServiceTrait;
const FILE_PICTURE = "picture";
const FILES = [
self::FILE_PICTURE => [
'types' => ['gif', 'jpg', 'jpeg', 'png'],
'dir' => '/company_gallery',
'size' => 5*1024*1024,
]
];
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="boolean", options={"default":"1"})
*/
private $active = true;
/**
* @ORM\Column(type="integer", options={"default":"0"})
*/
private $position;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="gallery")
*/
private $company;
public function getId(): ?int
{
return $this->id;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(int $position): self
{
$this->position = $position;
return $this;
}
/**
* @return string|null
*/
public function getPicturePreview(): ?string {
$file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
if ($file) {
$filePath = $file->getFilePath() . DIRECTORY_SEPARATOR . $file->getFileNameHash();
return $filePath;
}
return null;
}
/**
* @return File|null
*/
public function getPicture(){
return $this->getFileService()->getFile($this, self::FILE_PICTURE);
}
/**
* @param UploadedFile|null $file
*
* @return self
*/
public function setPicture(?UploadedFile $file): self{
if ($file) {
$oldFile = $this->getFileService()->getFile($this, self::FILE_PICTURE);
$newFile = $this->getFileService()->upload($file, $this, self::FILE_PICTURE);
if ($newFile && $oldFile){
$this->getFileService()->delete($oldFile);
}
}
return $this;
}
/**
* @return self
*/
public function removePicture(): self{
$file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
if ($file) {
$this->getFileService()->delete($file);
}
return $this;
}
/**
* @return Company|null
*/
public function getCompany(): ?Company {
return $this->company;
}
/**
* @param Company $company
*
* @return CompanyGallery
*/
public function setCompany(Company $company): CompanyGallery {
$this->company = $company;
return $this;
}
public function __get($title)
{
return PropertyAccess::createPropertyAccessor()->getValue($this->translate(), $title);
}
/**
* Specify data which should be serialized to JSON
*
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
return [
'id' => $this->getId(),
'title' => $this->translate()->getTitle(),
'description' => $this->translate()->getDescription(),
'active' => $this->getActive(),
'position' => $this->getPosition(),
'picture' => $this->getPicture()
];
}
}