Skip to content
Snippets Groups Projects
Commit 7121b9d1 authored by Александр Плохих's avatar Александр Плохих :waxing_gibbous_moon:
Browse files

STA-960 | add dto and cast from entities

parent 6c9559e6
No related branches found
No related tags found
1 merge request!16Sta 960
......@@ -2,6 +2,8 @@
namespace App\Entity;
use App\Dto\DtoInterface;
use App\Dto\FileDto;
use App\Repository\FileRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
......@@ -9,7 +11,7 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: FileRepository::class)]
class File
class File implements PrototypeDto
{
#[ORM\Id]
#[ORM\Column(type: Types::GUID)]
......@@ -288,4 +290,19 @@ class File
return $this;
}
/** @inheritDoc */
public function getDto(): FileDto
{
$dto = new FileDto();
$dto->id = $this->getId();
$dto->name = $this->getName();
$dto->description = $this->getDescription();
$dto->size = $this->getSize();
$dto->type = $this->getType();
$dto->url = $this->getUrl();
return $dto;
}
}
......@@ -2,6 +2,8 @@
namespace App\Entity;
use App\Dto\DtoInterface;
use App\Dto\KitchenTypeDto;
use App\Repository\KitchensRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
......@@ -9,7 +11,7 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: KitchensRepository::class)]
class Kitchens
class Kitchens implements PrototypeDto
{
#[ORM\Id]
#[ORM\Column(type: Types::GUID)]
......@@ -94,4 +96,16 @@ class Kitchens
return $this;
}
/** @inheritDoc */
public function getDto(): DtoInterface
{
$dto = new KitchenTypeDto();
$dto->id = $this->getId();
$dto->name = $this->getName();
$dto->code = $this->getCode();
return $dto;
}
}
......@@ -2,6 +2,9 @@
namespace App\Entity;
use App\Dto\DtoInterface;
use App\Dto\NewsDetailElementDto;
use App\Dto\NewsListingElementDto;
use App\Repository\NewsRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
......@@ -9,7 +12,7 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: NewsRepository::class)]
class News
class News implements PrototypeElementDto
{
#[ORM\Id]
#[ORM\Column(type: Types::GUID)]
......@@ -242,4 +245,34 @@ class News
return $this;
}
/** @inheritDoc */
public function getDto(): DtoInterface
{
$entity = new NewsListingElementDto();
$entity->id = $this->getId();
$entity->name = $this->getName();
$entity->description = $this->getDetailText();
$entity->image = $this->getPreviewImage()?->getDto();
$entity->create_at = $this->getCreatedAt();
$entity->detail_link = "api/v1/restaurants/" . $this->getId();
return $entity;
}
/** @inheritDoc */
public function getExtendedDto(): DtoInterface
{
$entity = new NewsDetailElementDto();
$entity->id = $this->getId();
$entity->name = $this->getName();
$entity->description = $this->getPreviewText();
$entity->text = $this->getDetailText();
$entity->image = $this->getPreviewImage()?->getDto();
$entity->create_at = $this->getCreatedAt();
return $entity;
}
}
......@@ -2,6 +2,8 @@
namespace App\Entity;
use App\Dto\DtoInterface;
use App\Dto\NewsCategoryDto;
use App\Repository\NewsCategoriesRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
......@@ -9,7 +11,7 @@ use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: NewsCategoriesRepository::class)]
class NewsCategories
class NewsCategories implements PrototypeDto
{
#[ORM\Id]
#[ORM\Column(type: Types::GUID)]
......@@ -94,4 +96,16 @@ class NewsCategories
return $this;
}
/** @inheritDoc */
public function getDto(): DtoInterface
{
$dto = new NewsCategoryDto();
$dto->id = $this->getId();
$dto->name = $this->getName();
$dto->code = $this->getCode();
return $dto;
}
}
......@@ -2,6 +2,7 @@
namespace App\Entity;
use App\Dto\DtoInterface;
use App\Repository\NewsTypeRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
......
<?php
namespace App\Entity;
use App\Dto\DtoInterface;
/** Интерфейс прототипа */
interface PrototypeDto
{
/** Породить dto из сущности */
public function getDto(): DtoInterface;
}
<?php
namespace App\Entity;
use App\Dto\DtoInterface;
interface PrototypeElementDto
{
/** Породить dto элемента листинга */
public function getDto(): DtoInterface;
/** Породить dto детальной информации */
public function getExtendedDto(): DtoInterface;
}
......@@ -2,6 +2,8 @@
namespace App\Entity;
use App\Dto\DtoInterface;
use App\Dto\RestaurantTypeDto;
use App\Repository\RestaurantTypesRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
......@@ -10,7 +12,7 @@ use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\GeneratedValue;
#[ORM\Entity(repositoryClass: RestaurantTypesRepository::class)]
class RestaurantTypes
class RestaurantTypes implements PrototypeDto
{
#[ORM\Id]
#[ORM\Column(type: Types::GUID)]
......@@ -98,4 +100,15 @@ class RestaurantTypes
return $this;
}
public function getDto(): DtoInterface
{
$dto = new RestaurantTypeDto();
$dto->id = $this->getId();
$dto->name = $this->getName();
$dto->code = $this->getCode();
return $dto;
}
}
......@@ -2,14 +2,22 @@
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
class Restaurants implements PrototypeElementDto
{
#[ORM\Id]
#[ORM\Column(type: Types::GUID)]
......@@ -392,4 +400,114 @@ class Restaurants
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()
);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment