<?php
namespace MLDev\CatalogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
use MLDev\BaseBundle\Entity\Traits\Timestampable;
use Symfony\Component\HttpFoundation\File\File;
use MLDev\CatalogBundle\Repository\ProductImageRepository;
use function file_exists;
/**
* @ORM\Table(name="MLDev_Product_Image")
* @ORM\Entity(repositoryClass=ProductImageRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class ProductImage implements JsonSerializable
{
use Timestampable;
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(type="string", name="name", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", name="path", length=255)
*/
private $path;
/**
* @ORM\Column(type="string", name="uri", length=255)
*/
private $uri;
/**
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="images", cascade={"persist"})
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
private $product;
/**
* @var File|null
*/
private $file;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getPath(): ?string
{
return $this->path;
}
public function setPath(?string $path): void
{
$this->path = $path;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): void
{
$this->product = $product;
}
public function getFile(): ?File
{
if ($this->file instanceof File) {
return $this->file;
}
if (file_exists($this->path)) {
return new File($this->path);
}
return null;
}
public function setFile(File $file): void
{
$this->file = $file;
}
public function getUri(): ?string
{
return $this->uri;
}
public function setUri(?string $uri)
{
$this->uri = $uri;
}
public function jsonSerialize(): array
{
return [
'id' => $this->getId(),
'name' => $this->getName(),
'url' => $this->getUri(),
'path' => $this->getPath(),
];
}
}