<?php
/**
* Copyright (c) 2019, MND Next GmbH - www.mndnext.de
*/
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity
* @ORM\Table(name="company_files")
* @Vich\Uploadable
*/
class CompanyFile
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="company_file", fileNameProperty="localName", size="size", originalName="originalName", mimeType="mimeType")
* @Assert\NotBlank(message="Es wurde noch keine Datei ausgewählt.")
*
* @var File
*/
private $file;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $name;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $localName;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $originalName;
/**
* @ORM\Column(type="integer")
*
* @var integer
*/
private $size;
/**
* @ORM\Column(type="string", length=255)
*
* @var string
*/
private $mimeType;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTime
*/
private $created;
/**
* @var bool
* @ORM\Column(name="visible", type="boolean", nullable=false, options={"default":false})
*/
private $visible;
/**
* @var bool
* @ORM\Column(name="sensitive_data", type="boolean", nullable=false, options={"default":false})
*/
private $sensitive;
/**
* @ORM\ManyToOne(targetEntity="Company", inversedBy="files", cascade={"persist"})
* @Assert\NotBlank(message="Bitte wählen Sie die zugehörige Firma aus.")
*/
private $company;
public function getId()
{
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|\Symfony\Component\HttpFoundation\File\UploadedFile $file
* @throws \Exception
*/
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->created = new \DateTimeImmutable();
}
}
public function getFile(): ?File
{
return $this->file;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getName(): ?string
{
return $this->name;
}
public function setLocalName(?string $localName): void
{
$this->localName = $localName;
}
public function getLocalName(): ?string
{
return $this->localName;
}
public function setOriginalName(?string $name): void
{
$this->originalName = $name;
}
public function getOriginalName(): ?string
{
return $this->originalName;
}
public function setSize(?int $size): void
{
$this->size = $size;
}
public function getSize(): ?int
{
return $this->size;
}
function getFormattedSize()
{
$units = array( 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$power = $this->size > 0 ? floor(log($this->size, 1024)) : 0;
return number_format($this->size / pow(1024, $power), 2, '.', ',') . ' ' . $units[$power];
}
public function setMimeType(?string $mimeType): void
{
$this->mimeType = $mimeType;
}
public function getMimeType(): ?string
{
return $this->mimeType;
}
public function setCompany(Company $company = null): void
{
$this->company = $company;
}
public function getCompany() : ?Company
{
return $this->company;
}
public function getCreated(): ?\DateTime
{
return $this->created;
}
public function setVisible(int $visible): void
{
$this->visible = $visible;
}
public function isVisible() : ?bool
{
return $this->visible;
}
public function setSensitive(bool $sensitive): void
{
$this->sensitive = $sensitive;
}
public function isSensitive() : ?bool
{
return $this->sensitive;
}
public function __toString()
{
return $this->getName();
}
}