src/Entity/Otpusk/Chain.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Otpusk;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Component\HttpFoundation\File\UploadedFile;
  7. /**
  8.  * @ORM\Table(name="chains")
  9.  * @ORM\Entity(repositoryClass="App\Repository\Otpusk\ChainRepository")
  10.  *
  11.  */
  12. class Chain
  13. {
  14.     const SERVER_PATH_TO_IMAGE_FOLDER 'upload/media/chains/';
  15.     /**
  16.      * @ORM\Id()
  17.      * @ORM\GeneratedValue()
  18.      * @ORM\Column(type="integer")
  19.      */
  20.     private $id;
  21.     /**
  22.      * @ORM\Column(type="string", length=255)
  23.      */
  24.     private $cName;
  25.     /**
  26.      * @ORM\Column(type="boolean", nullable=true, options={"default"=0})
  27.      */
  28.     private $showFilters;
  29.     /**
  30.      * @ORM\Column(type="boolean", nullable=true, options={"default"=0})
  31.      */
  32.     private $showLanding;
  33.     /**
  34.      * @ORM\OneToMany(targetEntity="App\Entity\Otpusk\Hotel", mappedBy="chainId")
  35.      */
  36.     private $hotels;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=true)
  39.      */
  40.     private $file;
  41.     /**
  42.      * @ORM\Column(type="string", length=255, nullable=true)
  43.      */
  44.     private $url;
  45.     public function __construct()
  46.     {
  47.         $this->hotels = new ArrayCollection();
  48.     }
  49.     public function getId(): ?int
  50.     {
  51.         return $this->id;
  52.     }
  53.     public function getCName(): ?string
  54.     {
  55.         return $this->cName;
  56.     }
  57.     public function setCName(string $cName): self
  58.     {
  59.         $this->cName $cName;
  60.         return $this;
  61.     }
  62.     public function getShowFilters(): ?bool
  63.     {
  64.         return $this->showFilters;
  65.     }
  66.     public function setShowFilters(bool $showFilters): self
  67.     {
  68.         $this->showFilters $showFilters;
  69.         return $this;
  70.     }
  71.     public function getShowLanding(): ?bool
  72.     {
  73.         return $this->showLanding;
  74.     }
  75.     public function setShowLanding(bool $showLanding): self
  76.     {
  77.         $this->showLanding $showLanding;
  78.         return $this;
  79.     }
  80.     /**
  81.      * @return Collection|Hotel[]
  82.      */
  83.     public function getHotels(): Collection
  84.     {
  85.         return $this->hotels;
  86.     }
  87.     public function addHotel(Hotel $hotel): self
  88.     {
  89.         if (!$this->hotels->contains($hotel)) {
  90.             $this->hotels[] = $hotel;
  91.             $hotel->setChainId($this);
  92.         }
  93.         return $this;
  94.     }
  95.     public function removeHotel(Hotel $hotel): self
  96.     {
  97.         if ($this->hotels->contains($hotel)) {
  98.             $this->hotels->removeElement($hotel);
  99.             // set the owning side to null (unless already changed)
  100.             if ($hotel->getChainId() === $this) {
  101.                 $hotel->setChainId(null);
  102.             }
  103.         }
  104.         return $this;
  105.     }
  106.     public function getHotelsCount(): int
  107.     {
  108.         return $this->hotels->count();
  109.     }
  110.     public function __toString()
  111.     {
  112.         return $this->getCName();
  113.     }
  114.     public function setFile($file)
  115.     {
  116.         $this->file $file;
  117.     }
  118.     /**
  119.      * @return UploadedFile|Null
  120.      */
  121.     public function getFile()
  122.     {
  123.         return $this->file ?? NULL;
  124.     }
  125.     public function getFilePath()
  126.     {
  127.         return $this->getFile() ? 'http://' $_SERVER['SERVER_NAME'] . '/' $this->getFile() : false;
  128.     }
  129.     public function upload()
  130.     {
  131.         // the file property can be empty if the field is not required
  132.         if (null === $this->getFile()) {
  133.             return;
  134.         }
  135.         $extension strripos($this->getFile()->getClientOriginalName(), '.');
  136.         $extension substr($this->getFile()->getClientOriginalName(), $extensionstrlen($this->getFile()->getClientOriginalName()) - 1);
  137.         $fileName $this->getId() . $extension;
  138.         $path self::SERVER_PATH_TO_IMAGE_FOLDER sprintf("%02d/%02d/", ($this->getId() / 1000) % 100, ($this->getId() / 10) % 100);
  139.         // we use the original file name here but you should
  140.         // sanitize it at least to avoid any security issues
  141.         // move takes the target directory and target filename as params
  142.         $this->getFile()->move(
  143.             $path,
  144.             $fileName
  145.         );
  146.         // set the path property to the filename where you've saved the file
  147.         $this->setFile($path .  $fileName);
  148.     }
  149.     public function getUrl(): ?string
  150.     {
  151.         return $this->url;
  152.     }
  153.     public function setUrl(?string $url): self
  154.     {
  155.         $this->url $url;
  156.         return $this;
  157.     }
  158. }