src/Entity/Common/File.php line 25
<?php
/**
* Created by PhpStorm.
* User: Михаил
* Date: 17.08.2018
* Time: 12:56
*/
namespace App\Entity\Common;
use App\Entity\Traits\CreatedTrait;
use App\Entity\Traits\UserCreatedInterface;
use App\Entity\Traits\UserCreatedTrait;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File as HttpFile;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
#[Vich\Uploadable()]
#[ORM\Table(name: 'files')]
#[ORM\Entity]
#[ORM\HasLifecycleCallbacks]
class File implements UserCreatedInterface
{
use CreatedTrait;
use UserCreatedTrait;
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
#[ORM\Column(type: 'integer')]
private $id;
#[Vich\UploadableField(mapping: "content_files", fileNameProperty: "data.name", size: "data.size", mimeType: "data.mimeType", originalName: "data.originalName", dimensions: "data.dimensions")]
#[Assert\NotBlank(message: 'need-file')]
#[Assert\File]
#[Assert\Image(detectCorrupted: true, corruptedMessage: 'Product photo is corrupted. Upload it again.', groups: ['Image'])]
private ?HttpFile $file = null;
/**
* @var bool
*/
#[ORM\Column(type: 'boolean', options: ['default' => true])]
private bool $public = true;
/**
* @var UploadedData
*/
#[ORM\Embedded(class: 'App\Entity\Common\UploadedData')]
private $data;
/**
* @var Owner
*/
#[ORM\Embedded(class: 'App\Entity\Common\Owner')]
private $owner;
public function __construct()
{
$this->data = new UploadedData();
$this->owner = new Owner();
}
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id): void
{
$this->id = $id;
}
/**
* @return HttpFile
*/
public function getFile(): ?HttpFile
{
return $this->file;
}
/**
* @param HttpFile $file
*/
public function setFile(HttpFile $file): 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->created_at = new DateTime();
}
}
/**
* @return UploadedData
*/
public function getData(): ?UploadedData
{
return $this->data;
}
/**
* @param UploadedData $data
*/
public function setData(UploadedData $data): void
{
$this->data = $data;
}
/**
* @return Owner
*/
public function getOwner(): Owner
{
return $this->owner;
}
/**
* @param Owner $owner
*/
public function setOwner(Owner $owner): void
{
$this->owner = $owner;
}
/**
* @return bool
*/
public function isPublic(): bool
{
return $this->public;
}
/**
* @param bool $public
*/
public function setPublic(bool $public): void
{
$this->public = $public;
}
public function getCaption(): ?string
{
return $this->getData()?->getOriginalName();
}
}