src/Entity/Location/City.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-19
  5.  * Time: 17:12
  6.  */
  7. namespace App\Entity\Location;
  8. use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
  9. //use ApiPlatform\Core\Annotation\ApiResource;
  10. use App\Repository\CityRepository;
  11. use Doctrine\Common\Collections\Collection;
  12. use Nelmio\ApiDocBundle\Model\Model;
  13. use Nelmio\ApiDocBundle\ModelDescriber\SelfDescribingModelInterface;
  14. use OpenApi\Annotations\Schema;
  15. use Symfony\Component\Serializer\Annotation\Groups;
  16. use Doctrine\Common\Collections\ArrayCollection;
  17. use Doctrine\ORM\Mapping as ORM;
  18. use Doctrine\ORM\Mapping\Index;
  19. use OpenApi\Attributes as OA;
  20. #[ORM\Table(name'cities')]
  21. #[Index(name'idx_uri_identity'columns: ['uri_identity'])]
  22. #[ORM\Entity(repositoryClassCityRepository::class)]
  23. #[ORM\Cache(usage'NONSTRICT_READ_WRITE'region'geoposition')]
  24. class City implements SelfDescribingModelInterface
  25. {
  26.     const GROUP_MEGALOPOLIS 1;
  27.     const GROUP_MOSCOW_REGION 2;
  28.     const PRICE_CATEGORY_1 1;
  29.     const PRICE_CATEGORY_2 2;
  30.     const PRICE_CATEGORY_3 3;
  31.     const PRICE_CATEGORY_4 4;
  32.     const PRICE_CATEGORY_5 5;
  33.     #[ORM\Id]
  34.     #[ORM\Column(name'id'type'integer')]
  35.     #[ORM\GeneratedValue(strategy'AUTO')]
  36.     #[Groups(['preview''listing'])]
  37.     protected int $id;
  38.     #[ORM\Column(name'name'type'translatable')]
  39.     #[Groups(['preview''listing'])]
  40.     protected TranslatableValue $name;
  41.     #[ORM\Column(name'uri_identity'type'string'length128)]
  42.     #[Groups(['preview''listing'])]
  43.     protected string $uriIdentity;
  44.     #[ORM\Column(name'country_code'type'string'length2)]
  45.     #[Groups(['preview''listing'])]
  46.     protected string $countryCode;
  47.     #[ORM\Column(name'city_group'type'smallint'nullabletrue)]
  48.     #[Groups('listing')]
  49.     protected ?int $cityGroup;
  50.     #[ORM\Column(name'city_price_category'type'smallint'nullabletrue)]
  51.     protected ?int $cityPriceCategory null;
  52.     /** @var County[] */
  53.     #[ORM\OneToMany(targetEntityCounty::class, mappedBy'city')]
  54.     #[ORM\Cache(usage'READ_ONLY'region'geoposition')]
  55.     protected Collection $counties;
  56.     /** @var District[] */
  57.     #[ORM\OneToMany(targetEntityDistrict::class, mappedBy'city')]
  58.     #[ORM\Cache(usage'READ_ONLY'region'geoposition')]
  59.     protected Collection $districts;
  60.     /** @var Station[] */
  61.     #[ORM\OneToMany(targetEntityStation::class, mappedBy'city')]
  62.     #[ORM\OrderBy(['name' => 'ASC'])]
  63.     #[ORM\Cache(usage'READ_ONLY'region'geoposition')]
  64.     protected Collection $stations;
  65.     #[ORM\Embedded(class: MapCoordinate::class, columnPrefixfalse)]
  66.     #[Groups('listing')]
  67.     protected ?MapCoordinate $mapCoordinate;
  68.     #[ORM\Column(name'timezone'type'string'length6nullabletrue)]
  69.     #[Groups('listing')]
  70.     protected ?string $timezone;
  71.     /**
  72.      * Региональное разделение городов
  73.      * Например: мск+МО и спб+ЛО
  74.      */
  75.     #[ORM\Column(name'region'type'string'length128nullabletrue)]
  76.     #[Groups(['listing''preview'])]
  77.     protected ?string $region;
  78.     public function __construct(TranslatableValue $namestring $uriIdentitystring $countryCode, ?int $cityGroup null, ?MapCoordinate $mapCoordinate null, ?string $region null)
  79.     {
  80.         $this->rename($name$uriIdentity);
  81.         $this->relocate($countryCode$cityGroup$mapCoordinate$region);
  82.         $this->counties = new ArrayCollection();
  83.         $this->districts = new ArrayCollection();
  84.         $this->stations = new ArrayCollection();
  85.     }
  86.     /**
  87.      * @param City|string $cityOrUriIdentity Instance of City class or URI identity string
  88.      */
  89.     public function equals(City|string $cityOrUriIdentity): bool
  90.     {
  91.         if ($cityOrUriIdentity instanceof City) {
  92.             return $cityOrUriIdentity->id === $this->id;
  93.         } elseif (is_string($cityOrUriIdentity)) {
  94.             return $cityOrUriIdentity === $this->uriIdentity;
  95.         }
  96.         throw new \InvalidArgumentException(__METHOD__.' can accept either string with URI identity or instance of App\\Entity\\Location\\City class.');
  97.     }
  98.     public function rename(TranslatableValue $namestring $uriIdentity): void
  99.     {
  100.         $this->name $name;
  101.         $this->uriIdentity $uriIdentity;
  102.     }
  103.     public function relocate(string $countryCode, ?int $cityGroup, ?MapCoordinate $mapCoordinate, ?string $region null): void
  104.     {
  105.         $this->countryCode $countryCode;
  106.         $this->cityGroup $cityGroup;
  107.         $this->mapCoordinate $mapCoordinate;
  108.         $this->region $region;
  109.     }
  110.     public function getId(): int
  111.     {
  112.         return $this->id;
  113.     }
  114.     public function getName(): TranslatableValue
  115.     {
  116.         return $this->name;
  117.     }
  118.     public function getUriIdentity(): string
  119.     {
  120.         return $this->uriIdentity;
  121.     }
  122.     public function getCountryCode(): string
  123.     {
  124.         return $this->countryCode;
  125.     }
  126.     public function getCityGroup(): ?int
  127.     {
  128.         return $this->cityGroup;
  129.     }
  130.     public function getCityPriceCategory(): ?int
  131.     {
  132.         return $this->cityPriceCategory;
  133.     }
  134.     public function setCityPriceCategory(?int $cityPriceCategory): void
  135.     {
  136.         $this->cityPriceCategory $cityPriceCategory;
  137.     }
  138.     /**
  139.      * @return County[]
  140.      */
  141.     public function getCounties(): Collection
  142.     {
  143.         return $this->counties;
  144.     }
  145.     public function hasCounty(County $county): bool
  146.     {
  147.         return $this->counties->contains($county);
  148.     }
  149.     /**
  150.      * @return District[]
  151.      */
  152.     public function getDistricts(): Collection
  153.     {
  154.         return $this->districts;
  155.     }
  156.     public function hasDistrict(District $district): bool
  157.     {
  158.         return $this->districts->contains($district);
  159.     }
  160.     /**
  161.      * @return Station[]
  162.      */
  163.     public function getStations(): Collection
  164.     {
  165.         return $this->stations;
  166.     }
  167.     public function hasStation(Station $station): bool
  168.     {
  169.         return $this->stations->contains($station);
  170.     }
  171.     //TODO return type?
  172.     public function getMapCoordinate(): ?MapCoordinate
  173.     {
  174.         return $this->mapCoordinate;
  175.     }
  176.     public function getTimezone(): ?string
  177.     {
  178.         return $this->timezone;
  179.     }
  180.     public function setTimezone(string $timezone): void
  181.     {
  182.         $this->timezone $timezone;
  183.     }
  184.     public function getRegion(): ?string
  185.     {
  186.         return $this->region;
  187.     }
  188.     public function setRegion(?string $region): void
  189.     {
  190.         $this->region $region;
  191.     }
  192.     /**
  193.      * @inheritDoc
  194.      */
  195.     public function __toString(): string
  196.     {
  197.         return (string) $this->name;
  198.     }
  199.     public static function describe(Schema $schemaModel $model): void
  200.     {
  201.         $schema->type 'object';
  202.         $schema->properties = [
  203.             new OA\Property(property'id'type'integer'),
  204.             new OA\Property(property'name'type'object'),
  205.             new OA\Property(property'uriIdentity'type'string'),
  206.             new OA\Property(property'countryCode'type'string'),
  207.             new OA\Property(property'cityGroup'type'integer'nullabletrue),
  208.             new OA\Property(property'mapCoordinate'properties: [new OA\Property(property'latitude'type'string'), new OA\Property(property'longitude'type'string')], type'object'nullabletrue),
  209.             new OA\Property(property'timezone'type'string'nullabletrue),
  210.             new OA\Property(property'region'description'Региональное разделение городов'type'string'nullabletrue),
  211.         ];
  212.         $schema->description 'City model';
  213.         $schema->example = [
  214.             'id' => 1,
  215.             'name' => ['ru' => 'Москва''en' => 'Moscow'],
  216.             'uriIdentity' => 'moscow',
  217.             'countryCode' => 'RU',
  218.             'cityGroup' => 1,
  219.             'mapCoordinate' => ['latitude' => '55.755825''longitude' => '37.617298'],
  220.             'timezone' => '+03:00',
  221.             'region' => 'moscow',
  222.         ];
  223.     }
  224. }