src/Entity/Image.php line 15
<?php
namespace App\Entity;
use App\Repository\ImageRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Uid\Uuid;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[ORM\Entity(repositoryClass: ImageRepository::class)]
#[Vich\Uploadable]
class Image
{
use TimestampableEntity;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'CUSTOM')]
#[ORM\Column(type: 'uuid', unique: true)]
#[ORM\CustomIdGenerator(class: 'doctrine.uuid_generator')]
private ?Uuid $id = null;
#[Vich\UploadableField(mapping: 'images', fileNameProperty: 'name', size: 'size', mimeType: 'mimeType', originalName: 'originalName', dimensions: 'dimensions')]
private ?File $file = null;
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\Column]
private ?int $size = null;
#[ORM\Column(length: 255)]
private ?string $mimeType = null;
#[ORM\Column(length: 255)]
private ?string $originalName = null;
#[ORM\Column]
private ?array $dimensions = [];
private ?string $publicUrl = null;
public function getId(): ?Uuid
{
return $this->id;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|UploadedFile|null $file
*/
public function setFile(?File $file = null): void
{
$this->file = $file;
if (null !== $file) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getFile(): ?File
{
return $this->file;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
public function getSize(): ?int
{
return $this->size;
}
public function setSize(?int $size): self
{
$this->size = $size;
return $this;
}
public function getMimeType(): ?string
{
return $this->mimeType;
}
public function setMimeType(?string $mimeType): self
{
$this->mimeType = $mimeType;
return $this;
}
public function getOriginalName(): ?string
{
return $this->originalName;
}
public function setOriginalName(?string $originalName): self
{
$this->originalName = $originalName;
return $this;
}
public function getDimensions(): array
{
if (empty($this->dimensions)) {
return [];
}
return [
'width' => $this->dimensions[0],
'height' => $this->dimensions[1],
];
}
public function setDimensions(?array $dimensions): self
{
if (!is_array($dimensions)) {
$dimensions = [];
}
if (key_exists('width', $dimensions) && key_exists('height', $dimensions)) {
$this->dimensions = [$dimensions['width'], $dimensions['height']];
return $this;
}
$this->dimensions = $dimensions;
return $this;
}
/**
* @return string|null
*/
public function getPublicUrl(): ?string
{
return $this->publicUrl;
}
/**
* @param string|null $publicUrl
*/
public function setPublicUrl(?string $publicUrl): void
{
$this->publicUrl = $publicUrl;
}
}