Skip to content
Snippets Groups Projects
Restaurants.php 11.2 KiB
Newer Older
<?php

namespace App\Entity;

use App\Dto\DtoCollection;
use App\Dto\DtoInterface;
use App\Dto\FileDto;
use App\Dto\KitchenTypeDto;
use App\Dto\RestaurantDetailElementDto;
use App\Dto\RestaurantListingElementDto;
use App\Dto\TagDto;
use App\Repository\RestaurantsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Ramsey\Collection\Collection as RamseyCollection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: RestaurantsRepository::class)]
class Restaurants implements PrototypeElementDto
{
    #[ORM\Id]
    #[ORM\Column(type: Types::GUID)]
    private ?string $id = null;

    #[ORM\Column]
    private ?bool $active = null;

    #[ORM\Column]
    private ?int $sort = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $createdAt = null;

    #[ORM\Column]
    private ?\DateTimeImmutable $updateAt = null;

    #[ORM\ManyToOne(inversedBy: 'restaurantsTypes')]
    private ?RestaurantTypes $type = null;

    #[ORM\ManyToOne(inversedBy: 'restaurantsSettlements')]
    private ?Settlements $settlement = null;

    #[ORM\Column(length: 255)]
    private ?string $name = null;

    #[ORM\Column(length: 255)]
    private ?string $code = null;

    #[ORM\Column(length: 255)]
    private ?string $description = null;

    #[ORM\Column(length: 255)]
    private ?string $receipt = null;

    #[ORM\Column(length: 255)]
    private ?string $receiptInfo = null;

    #[ORM\Column(length: 255)]
    private ?string $phone = null;

    #[ORM\Column(length: 255)]
    private ?string $email = null;

    #[ORM\Column(length: 255)]
    private ?string $address = null;

    #[ORM\Column(length: 255)]
    private ?string $tags = null;

    #[ORM\Column(length: 255)]
    private ?string $site = null;

    #[ORM\Column(length: 1000)]
    private ?string $coordinates = null;

    #[ORM\Column(length: 255)]
    private ?string $howToFind = null;

    /**
     * @var Collection<int, Kitchens>
     */
    #[ORM\ManyToMany(targetEntity: Kitchens::class, inversedBy: 'restaurants')]
    private Collection $kitchens;

    #[ORM\ManyToOne(inversedBy: 'restaurantsPreview')]
    private ?File $previewImage = null;

    #[ORM\ManyToOne(inversedBy: 'restaurantsDetail')]
    private ?File $detailImage = null;

    /**
     * @var Collection<int, File>
     */
    #[ORM\ManyToMany(targetEntity: File::class, inversedBy: 'restaurantsGallery')]
    private Collection $gallery;

    public function __construct()
    {
        $this->kitchens = new ArrayCollection();
        $this->gallery = new ArrayCollection();
    public function getId(): ?string
    {
        return $this->id;
    }

    public function setId(string $id): static
    {
        $this->id = $id;

        return $this;
    }

    public function isActive(): ?bool
    {
        return $this->active;
    }

    public function setActive(bool $active): static
    {
        $this->active = $active;

        return $this;
    }

    public function getSort(): ?int
    {
        return $this->sort;
    }

    public function setSort(int $sort): static
    {
        $this->sort = $sort;

        return $this;
    }

    public function getCreatedAt(): ?\DateTimeImmutable
    {
        return $this->createdAt;
    }

    public function setCreatedAt(\DateTimeImmutable $createdAt): static
    {
        $this->createdAt = $createdAt;

        return $this;
    }

    public function getUpdateAt(): ?\DateTimeImmutable
    {
        return $this->updateAt;
    }

    public function setUpdateAt(\DateTimeImmutable $updateAt): static
    {
        $this->updateAt = $updateAt;

        return $this;
    }

    public function getType(): ?RestaurantTypes
    {
        return $this->type;
    }

    public function setType(?RestaurantTypes $type): static
    {
        $this->type = $type;

        return $this;
    }

    public function getSettlement(): ?Settlements
    {
        return $this->settlement;
    }

    public function setSettlement(?Settlements $settlement): static
    {
        $this->settlement = $settlement;

        return $this;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    public function setName(string $name): static
    {
        $this->name = $name;

        return $this;
    }

    public function getCode(): ?string
    {
        return $this->code;
    }

    public function setCode(string $code): static
    {
        $this->code = $code;

        return $this;
    }

    public function getDescription(): ?string
    {
        return $this->description;
    }

    public function setDescription(string $description): static
    {
        $this->description = $description;

        return $this;
    }

    public function getReceipt(): ?string
    {
        return $this->receipt;
    }

    public function setReceipt(string $receipt): static
    {
        $this->receipt = $receipt;

        return $this;
    }

    public function getReceiptInfo(): ?string
    {
        return $this->receiptInfo;
    }

    public function setReceiptInfo(string $receiptInfo): static
    {
        $this->receiptInfo = $receiptInfo;

        return $this;
    }

    public function getPhone(): ?string
    {
        return $this->phone;
    }

    public function setPhone(string $phone): static
    {
        $this->phone = $phone;

        return $this;
    }

    public function getEmail(): ?string
    {
        return $this->email;
    }

    public function setEmail(string $email): static
    {
        $this->email = $email;

        return $this;
    }

    public function getAddress(): ?string
    {
        return $this->address;
    }

    public function setAddress(string $address): static
    {
        $this->address = $address;

        return $this;
    }

    public function getTags(): ?string
    {
        return $this->tags;
    }

    public function setTags(string $tags): static
    {
        $this->tags = $tags;

        return $this;
    }

    public function getSite(): ?string
    {
        return $this->site;
    }

    public function setSite(string $site): static
    {
        $this->site = $site;

        return $this;
    }

    public function getCoordinates(): string
    {
        return $this->coordinates;
    }

    public function setCoordinates(string $coordinates): static
    {
        $this->coordinates = $coordinates;

        return $this;
    }

    public function getHowToFind(): ?string
    public function setHowToFind(string $howToFind): static
        $this->howToFind = $howToFind;
    /**
     * @return Collection<int, Kitchens>
     */
    public function getKitchens(): Collection
    public function addKitchen(Kitchens $kitchen): static
        if (!$this->kitchens->contains($kitchen)) {
            $this->kitchens->add($kitchen);
        }
    public function removeKitchen(Kitchens $kitchen): static
        $this->kitchens->removeElement($kitchen);

        return $this;
    public function getPreviewImage(): ?File
        return $this->previewImage;
    }

    public function setPreviewImage(?File $previewImage): static
    {
        $this->previewImage = $previewImage;
    public function getDetailImage(): ?File
    public function setDetailImage(?File $detailImage): static
        $this->detailImage = $detailImage;
     * @return Collection<int, File>
    public function getGallery(): Collection
    public function addGallery(File $gallery): static
        if (!$this->gallery->contains($gallery)) {
            $this->gallery->add($gallery);
    public function removeGallery(File $gallery): static
        $this->gallery->removeElement($gallery);

        return $this;
    }

    /** @inheritDoc */
    public function getDto(): DtoInterface
    {
        $dto = new RestaurantListingElementDto();

        $dto->id = $this->getId();
        $dto->name = $this->getName();
        $dto->code = $this->getCode();
        $dto->type = $this->getType()->getDto();
        $dto->check = $this->getReceipt();
        $dto->image = $this->getPreviewImage()?->getDto();
        $dto->detail_link = 'api/v1/restaurants/' . $this->getId();

        return $dto;
    }

    /** @inheritDoc
     * @throws \JsonException Если получен неправильный json с бд
     */
    public function getExtendedDto(): DtoInterface
    {
        $dto = new RestaurantDetailElementDto();

        $dto->id = $this->getId();
        $dto->name = $this->getName();
        $dto->code = $this->getCode();
        $dto->coordinates = $this->getCoordinates();
        $dto->type = $this->getType()->getDto();
        $dto->check = $this->getReceipt();
        $dto->check_info = $this->getReceiptInfo();

        $dto->kitchen = new DtoCollection(
            KitchenTypeDto::class,
            $this->getKitchens()
                ->map(function(Kitchens $kitchen) {
                return $kitchen->getDto();
                })
                ->toArray()
        );

        $dto->phone = $this->decode($this->getPhone(), 'phone');
        $dto->email = $this->decode($this->getEmail(), 'email');
        $dto->address = $this->decode($this->getAddress(), 'address');
        $dto->tags = $this->decodeTags($this->getTags());
        $dto->site = $this->getSite();
        $dto->image = $this->getPreviewImage()?->getDto();
        $dto->gallery = new DtoCollection(
            FileDto::class,
            $this->getGallery()
                ->map(function(File $file) {
                    return $file->getDto();
                })
                ->toArray()
        );

        return $dto;
    }

    /**
     * @throws \JsonException Если получен неправильный json с бд
     * @return ?DtoCollection<string>
     */
    private function decode(?string $jsonString, string $name): ?DtoCollection
    {
        if ($jsonString === null) {
            return null;
        }
        return new DtoCollection(
            'string|null',
            json_decode(
                $jsonString,
                true,
                512,
                JSON_THROW_ON_ERROR
            )[$name]
        );
    }

    private function decodeTags(?string $jsonString): ?DtoCollection
    {
        if ($jsonString === null) {
            return null;
        }

        $jsonCollection = new RamseyCollection(
            'array',
            json_decode(
                $jsonString,
                true,
                512,
                JSON_THROW_ON_ERROR
            )
        );

        return new DtoCollection(
            TagDto::class,
            $jsonCollection
                ->map(function(array $tag) {
                    $tagDto = new TagDto();
                    $tagDto->name = key($tag);
                    $tagDto->list = new DtoCollection(
                    'string',
                    $tag
                    );
                    return $tagDto;
                })
                ->toArray()
        );
    }