vendor/score/cms/src/Entity/Document/DocumentFile.php line 13

Open in your IDE?
  1. <?php
  2. namespace Score\CmsBundle\Entity\Document;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Score\BaseBundle\Entity\BaseEntity;
  5. /**
  6.  * @ORM\Entity()
  7.  * @ORM\Table(name="cms_document_file")
  8.  * @ORM\HasLifecycleCallbacks()
  9.  */
  10. class DocumentFile extends BaseEntity {
  11.     /**
  12.      * @ORM\ManyToOne(targetEntity=Document::class, inversedBy="documentFiles")
  13.      * @ORM\JoinColumn(name="document_id", referencedColumnName="id")
  14.      */
  15.     protected $document;
  16.     /**
  17.      * @ORM\Column(name="d_is_external", type="boolean", nullable=true)
  18.      */
  19.     protected $isExternal;
  20.     /**
  21.      * @ORM\Column(name="d_url", type="string", length=500)
  22.      */
  23.     protected $url;
  24.     /**
  25.      * @ORM\Column(name="d_filename", type="string", length=1000, nullable=true)
  26.      */
  27.     protected $filename;
  28.     /**
  29.      * @ORM\Column(name="d_size", type="string", length=255, nullable=true)
  30.      */
  31.     protected $size;
  32.     /**
  33.      * @ORM\Column(name="d_type", type="string", length=255, nullable=true)
  34.      */
  35.     protected $type;
  36.     /**
  37.      * @ORM\Column(name="d_language", type="string", length=255, nullable=true)
  38.      */
  39.     protected $language;
  40.     /**
  41.      * @ORM\Column(name="d_info", type="string", length=255)
  42.      */
  43.     protected $info;
  44.     /**
  45.      * @ORM\Column(name="d_is_current", type="boolean", nullable=true)
  46.      */
  47.     protected $isCurrent;
  48.     /**
  49.      * @ORM\Column(name="d_code", type="string", length=255, nullable=true)
  50.      */
  51.     protected $code;
  52.     /**
  53.      * @ORM\Column(name="d_extension", type="string", length=255, nullable=true)
  54.      */
  55.     protected $extension;
  56.     /**
  57.      * @ORM\PrePersist()
  58.      * @ORM\PreUpdate()
  59.      */
  60.     public function preEditedInfoUpdate() {
  61.         if ($this->getIsExternal())
  62.             return;
  63.         $stringSize = $this->parseSizeString($this->getSize());
  64.         $this->setInfo(strtoupper($this->getExtension()) . ", " . $stringSize);
  65.         if ($this->getLanguage()) {
  66.             $this->setInfo($this->getLanguage() . ', ' . $this->getInfo());
  67.         }
  68.     }
  69.     public function getSizeHumanReadable() {
  70.         return $this->parseSizeString($this->getSize());
  71.     }
  72.     /**
  73.      * Converts bytes into human readable file size.
  74.      *
  75.      * @param string $bytes
  76.      * @return string human readable file size (2,87 Мб)
  77.      * @author Mogilev Arseny
  78.      */
  79.     protected function parseSizeString($bytes) {
  80.         $result = null;
  81.         $bytes = floatval($bytes);
  82.         $arBytes = [
  83.             [
  84.                 "UNIT" => "TB",
  85.                 "VALUE" => pow(1024, 4)
  86.             ], [
  87.                 "UNIT" => "GB",
  88.                 "VALUE" => pow(1024, 3)
  89.             ], [
  90.                 "UNIT" => "MB",
  91.                 "VALUE" => pow(1024, 2)
  92.             ], [
  93.                 "UNIT" => "KB",
  94.                 "VALUE" => 1024
  95.             ], [
  96.                 "UNIT" => "B",
  97.                 "VALUE" => 1
  98.             ],
  99.         ];
  100.         foreach ($arBytes as $arItem) {
  101.             if ($bytes >= $arItem["VALUE"]) {
  102.                 $result = $bytes / $arItem["VALUE"];
  103.                 $result = str_replace(".", ",", strval(round($result, 1))) . " " . $arItem["UNIT"];
  104.                 break;
  105.             }
  106.         }
  107.         return $result;
  108.     }
  109.     public function getDocument(): ?Document {
  110.         return $this->document;
  111.     }
  112.     public function setDocument(?Document $document): self {
  113.         $this->document = $document;
  114.         return $this;
  115.     }
  116.     public function getIsExternal(): ?bool {
  117.         return $this->isExternal;
  118.     }
  119.     public function setIsExternal(?bool $isExternal): self {
  120.         $this->isExternal = $isExternal;
  121.         return $this;
  122.     }
  123.     public function getUrl(): ?string {
  124.         return $this->url;
  125.     }
  126.     public function setUrl(string $url): self {
  127.         $this->url = $url;
  128.         return $this;
  129.     }
  130.     public function getFilename(): ?string {
  131.         return $this->filename;
  132.     }
  133.     public function setFilename(string $filename): self {
  134.         $this->filename = $filename;
  135.         return $this;
  136.     }
  137.     public function getSize(): ?string {
  138.         return $this->size;
  139.     }
  140.     public function setSize(?string $size): self {
  141.         $this->size = $size;
  142.         return $this;
  143.     }
  144.     public function getType(): ?string {
  145.         return $this->type;
  146.     }
  147.     public function setType(?string $type): self {
  148.         $this->type = $type;
  149.         return $this;
  150.     }
  151.     public function getLanguage(): ?string {
  152.         return $this->language;
  153.     }
  154.     public function setLanguage(?string $language): self {
  155.         $this->language = $language;
  156.         return $this;
  157.     }
  158.     public function getInfo(): ?string {
  159.         return $this->info;
  160.     }
  161.     public function setInfo(string $info): self {
  162.         $this->info = $info;
  163.         return $this;
  164.     }
  165.     public function getIsCurrent(): ?bool {
  166.         return $this->isCurrent;
  167.     }
  168.     public function setIsCurrent(?bool $isCurrent): self {
  169.         $this->isCurrent = $isCurrent;
  170.         return $this;
  171.     }
  172.     public function getCode(): ?string {
  173.         return $this->code;
  174.     }
  175.     public function setCode(?string $code): self {
  176.         $this->code = $code;
  177.         return $this;
  178.     }
  179.     public function getExtension(): ?string {
  180.         return $this->extension;
  181.     }
  182.     public function setExtension(?string $extension): self {
  183.         $this->extension = $extension;
  184.         return $this;
  185.     }
  186. }