You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
6.6 KiB
PHP

<?php
namespace App\Entity;
use App\Repository\DocumentRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity(repositoryClass: DocumentRepository::class)]
#[Gedmo\Uploadable(
pathMethod: 'getFileDirectoryPath',
callback: 'onFileMove',
filenameGenerator: 'SHA1',
maxSize: 20 * 1024 * 1024, // 20 Mo
)]
class Document {
use TEntityBase;
/**
* @var User The owner
*/
#[ORM\ManyToOne]
#[ORM\JoinColumn(nullable: false)]
#[Assert\Valid]
private User $owner;
/**
* @var string The file path, relative to owner directory (aka “file name”)
*/
#[ORM\Column(length: 255)]
#[Gedmo\UploadableFileName]
#[Assert\NotBlank]
private string $filePath;
/**
* @var string The original file name
*/
#[ORM\Column(length: 255)]
#[Assert\NotBlank]
private string $fileName;
/**
* @var string The file mime type
*/
#[ORM\Column(length: 255)]
#[Gedmo\UploadableFileMimeType]
#[Assert\NotBlank]
private string $fileType;
/**
* @var int The file size, in bytes
*/
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 0)] // En "decimal" dans Doctrine car Gedmo\UploadableFileSize l'impose, mais bien un entier derrière
#[Gedmo\UploadableFileSize]
#[Assert\Positive]
private int $fileSize;
/**
* @var string|null The description
*/
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string $description = null;
/**
* @var Collection<int, Tag> The {@see Tag tags}
*/
#[ORM\ManyToMany(targetEntity: Tag::class, inversedBy: 'documents')]
private Collection $tags;
/**
* Initialization
*/
public function __construct () {
$this->tags = new ArrayCollection();
}
/**
* The owner
*
* @return User The owner
*/
public function getOwner (): User {
return $this->owner;
}
/**
* Change the owner
*
* @param User $owner The new owner
*
* @return $this
*/
public function setOwner (User $owner): self {
$this->owner = $owner;
return $this;
}
/**
* The file path, relative to owner directory (aka “file name”)
*
* @return string|null The file path, relative to owner directory (aka “file name”)
*/
public function getFilePath (): ?string {
return $this->filePath;
}
/**
* Change the file path, relative to owner directory (aka “file name”)
*
* @param string $filePath The new file path, relative to owner directory (aka “file name”)
*
* @return $this
*/
public function setFilePath (string $filePath): static {
$this->filePath = $filePath;
return $this;
}
/**
* The original file name
*
* @return string|null The original file name
*/
public function getFileName (): ?string {
return $this->fileName;
}
/**
* Change the original file name
*
* @param string $fileName The new original file name
*
* @return $this
*/
public function setFileName (string $fileName): static {
$this->fileName = $fileName;
return $this;
}
/**
* The file mime type
*
* @return string|null The file mime type
*/
public function getFileType (): ?string {
return $this->fileType;
}
/**
* Change the file mime type
*
* @param string $fileType The new file mime type
*
* @return $this
*/
public function setFileType (string $fileType): static {
$this->fileType = $fileType;
return $this;
}
/**
* The file size, in bytes
*
* @return int|null The file size, in bytes
*/
public function getFileSize (): ?int {
return $this->fileSize;
}
/**
* Change the file size, in bytes
*
* @param int $fileSize The new file size, in bytes
*
* @return $this
*/
public function setFileSize (int $fileSize): static {
$this->fileSize = $fileSize;
return $this;
}
/**
* The description
*
* @return string|null The description
*/
public function getDescription (): ?string {
return $this->description;
}
/**
* Change the description
*
* @param string|null $description The new description
*
* @return $this
*/
public function setDescription (?string $description): self {
$this->description = $description;
return $this;
}
/**
* The {@see Tag tags}
*
* @return Collection<int, Tag> The {@see Tag tags}
*/
public function getTags (): Collection {
return $this->tags;
}
/**
* Add a tag
*
* Do nothing if the tag is already present
*
* @param Tag $tag The tag to add
*
* @return $this
*/
public function addTag (Tag $tag): self {
if (!$this->tags->contains($tag)) {
$this->tags->add($tag);
}
return $this;
}
/**
* Remove a tag
*
* @param Tag $tag The tag to remove
*
* @return $this
*/
public function removeTag (Tag $tag): self {
$this->tags->removeElement($tag);
return $this;
}
/**
* The file directory path
*
* @param string $globalFileDirectoryPath The global file directory path
*
* @return string The file directory path
*
* @see \Gedmo\Mapping\Annotation\Uploadable::$pathMethod
* @link https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/uploadable.md#uploadable-annotations-and-attributes
*
* @noinspection PhpUnused
*/
public function getFileDirectoryPath (string $globalFileDirectoryPath): string {
return $globalFileDirectoryPath . DIRECTORY_SEPARATOR . $this->owner->getId();
}
/**
* @param array $fileInfo
*
* @return void
*
* @see \Gedmo\Mapping\Annotation\Uploadable::$callback
* @link https://github.com/doctrine-extensions/DoctrineExtensions/blob/main/doc/uploadable.md#uploadable-annotations-and-attributes
*
* @noinspection PhpUnused *
*/
public function onFileMove (array $fileInfo): void {
$this->fileName = $fileInfo['origFileName']; // Information absente de la doc mais bien alimentée
}
}