Files
biblio/src/Entity/File.php
2022-06-10 14:05:23 +02:00

114 lines
2.5 KiB
PHP

<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\FileRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
#[ApiResource(collectionOperations: ['get'], itemOperations: ['get'], denormalizationContext: ['groups' => ['file:write']], normalizationContext: ['groups' => ['file:read']])]
#[ORM\Entity(repositoryClass: FileRepository::class)]
class File
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
#[Groups('file:read')]
private $id;
#[ORM\Column(type: 'string', length: 255)]
#[Groups('file:read')]
private $file_name;
#[ORM\Column(type: 'integer')]
private $file_size;
#[ORM\Column(type: 'string', length: 255)]
#[Groups('file:read')]
private $extension;
#[ORM\Column(type: 'string', length: 255)]
private $type = 'ebook';
#[ORM\ManyToOne(targetEntity: Book::class, inversedBy: 'files')]
#[ORM\JoinColumn(nullable: false)]
#[Groups('file:read')]
private $book;
public function getId(): ?int
{
return $this->id;
}
public function getFileName(): ?string
{
return $this->file_name;
}
public function setFileName(string $file_name): self
{
$this->file_name = $file_name;
return $this;
}
public function getFileSize(): ?int
{
return $this->file_size;
}
#[Groups('file:read')]
#[SerializedName('file_size_human')]
public function getFileSizeHuman(): string
{
$base = log($this->file_size) / log(1024);
$suffix = array("B", "KB", "MB", "GB", "TB")[floor($base)];
return round(pow(1024, $base - floor($base)), 2) . $suffix;
}
public function setFileSize(int $file_size): self
{
$this->file_size = $file_size;
return $this;
}
public function getExtension(): ?string
{
return $this->extension;
}
public function setExtension(string $extension): self
{
$this->extension = $extension;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type = 'ebook'): self
{
$this->type = $type;
return $this;
}
public function getBook(): ?Book
{
return $this->book;
}
public function setBook(?Book $book): self
{
$this->book = $book;
return $this;
}
}