src/Entity/CompanyFile.php line 18

Open in your IDE?
  1. <?php
  2. /**
  3.  * Copyright (c) 2019, MND Next GmbH - www.mndnext.de
  4.  */
  5. namespace App\Entity;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use Symfony\Component\Validator\Constraints as Assert;
  8. use Symfony\Component\HttpFoundation\File\File;
  9. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  10. /**
  11.  * @ORM\Entity
  12.  * @ORM\Table(name="company_files")
  13.  * @Vich\Uploadable
  14.  */
  15. class CompanyFile
  16. {
  17.     /**
  18.      * @ORM\Id
  19.      * @ORM\Column(type="integer")
  20.      * @ORM\GeneratedValue(strategy="AUTO")
  21.      */
  22.     private $id;
  23.     /**
  24.      * NOTE: This is not a mapped field of entity metadata, just a simple property.
  25.      *
  26.      * @Vich\UploadableField(mapping="company_file", fileNameProperty="localName", size="size", originalName="originalName", mimeType="mimeType")
  27.      * @Assert\NotBlank(message="Es wurde noch keine Datei ausgewählt.")
  28.      *
  29.      * @var File
  30.      */
  31.     private $file;
  32.     /**
  33.      * @ORM\Column(type="string", length=255)
  34.      *
  35.      * @var string
  36.      */
  37.     private $name;
  38.     /**
  39.      * @ORM\Column(type="string", length=255)
  40.      *
  41.      * @var string
  42.      */
  43.     private $localName;
  44.     /**
  45.      * @ORM\Column(type="string", length=255)
  46.      *
  47.      * @var string
  48.      */
  49.     private $originalName;
  50.     /**
  51.      * @ORM\Column(type="integer")
  52.      *
  53.      * @var integer
  54.      */
  55.     private $size;
  56.     /**
  57.      * @ORM\Column(type="string", length=255)
  58.      *
  59.      * @var string
  60.      */
  61.     private $mimeType;
  62.     /**
  63.      * @ORM\Column(type="datetime")
  64.      *
  65.      * @var \DateTime
  66.      */
  67.     private $created;
  68.     /**
  69.      * @var bool
  70.      * @ORM\Column(name="visible", type="boolean", nullable=false, options={"default":false})
  71.      */
  72.     private $visible;
  73.     /**
  74.      * @var bool
  75.      * @ORM\Column(name="sensitive_data", type="boolean", nullable=false, options={"default":false})
  76.      */
  77.     private $sensitive;
  78.     /**
  79.      * @ORM\ManyToOne(targetEntity="Company", inversedBy="files", cascade={"persist"})
  80.      * @Assert\NotBlank(message="Bitte wählen Sie die zugehörige Firma aus.")
  81.      */
  82.     private $company;
  83.     public function getId()
  84.     {
  85.         return $this->id;
  86.     }
  87.     /**
  88.      * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
  89.      * of 'UploadedFile' is injected into this setter to trigger the update. If this
  90.      * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
  91.      * must be able to accept an instance of 'File' as the bundle will inject one here
  92.      * during Doctrine hydration.
  93.      *
  94.      * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
  95.      * @throws \Exception
  96.      */
  97.     public function setFile(?File $file null): void
  98.     {
  99.         $this->file $file;
  100.         if (null !== $file) {
  101.             // It is required that at least one field changes if you are using doctrine
  102.             // otherwise the event listeners won't be called and the file is lost
  103.             $this->created = new \DateTimeImmutable();
  104.         }
  105.     }
  106.     public function getFile(): ?File
  107.     {
  108.         return $this->file;
  109.     }
  110.     public function setName(?string $name): void
  111.     {
  112.         $this->name $name;
  113.     }
  114.     public function getName(): ?string
  115.     {
  116.         return $this->name;
  117.     }
  118.     public function setLocalName(?string $localName): void
  119.     {
  120.         $this->localName $localName;
  121.     }
  122.     public function getLocalName(): ?string
  123.     {
  124.         return $this->localName;
  125.     }
  126.     public function setOriginalName(?string $name): void
  127.     {
  128.         $this->originalName $name;
  129.     }
  130.     public function getOriginalName(): ?string
  131.     {
  132.         return $this->originalName;
  133.     }
  134.     public function setSize(?int $size): void
  135.     {
  136.         $this->size $size;
  137.     }
  138.     public function getSize(): ?int
  139.     {
  140.         return $this->size;
  141.     }
  142.     function getFormattedSize()
  143.     {
  144.         $units = array( 'B''KB''MB''GB''TB''PB''EB''ZB''YB');
  145.         $power $this->size floor(log($this->size1024)) : 0;
  146.         return number_format($this->size pow(1024$power), 2'.'',') . ' ' $units[$power];
  147.     }
  148.     public function setMimeType(?string $mimeType): void
  149.     {
  150.         $this->mimeType $mimeType;
  151.     }
  152.     public function getMimeType(): ?string
  153.     {
  154.         return $this->mimeType;
  155.     }
  156.     public function setCompany(Company $company null): void
  157.     {
  158.         $this->company $company;
  159.     }
  160.     public function getCompany() : ?Company
  161.     {
  162.         return $this->company;
  163.     }
  164.     public function getCreated(): ?\DateTime
  165.     {
  166.         return $this->created;
  167.     }
  168.     public function setVisible(int $visible): void
  169.     {
  170.         $this->visible $visible;
  171.     }
  172.     public function isVisible() : ?bool
  173.     {
  174.         return $this->visible;
  175.     }
  176.     public function setSensitive(bool $sensitive): void
  177.     {
  178.         $this->sensitive $sensitive;
  179.     }
  180.     public function isSensitive() : ?bool
  181.     {
  182.         return $this->sensitive;
  183.     }
  184.     public function __toString()
  185.     {
  186.         return $this->getName();
  187.     }
  188. }