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.     public function __construct(TranslatableValue $namestring $uriIdentitystring $countryCode, ?int $cityGroup null, ?MapCoordinate $mapCoordinate null)
  72.     {
  73.         $this->rename($name$uriIdentity);
  74.         $this->relocate($countryCode$cityGroup$mapCoordinate);
  75.         $this->counties = new ArrayCollection();
  76.         $this->districts = new ArrayCollection();
  77.         $this->stations = new ArrayCollection();
  78.     }
  79.     /**
  80.      * @param City|string $cityOrUriIdentity Instance of City class or URI identity string
  81.      */
  82.     public function equals(City|string $cityOrUriIdentity): bool
  83.     {
  84.         if ($cityOrUriIdentity instanceof City) {
  85.             return $cityOrUriIdentity->id === $this->id;
  86.         } elseif (is_string($cityOrUriIdentity)) {
  87.             return $cityOrUriIdentity === $this->uriIdentity;
  88.         }
  89.         throw new \InvalidArgumentException(__METHOD__.' can accept either string with URI identity or instance of App\\Entity\\Location\\City class.');
  90.     }
  91.     public function rename(TranslatableValue $namestring $uriIdentity): void
  92.     {
  93.         $this->name $name;
  94.         $this->uriIdentity $uriIdentity;
  95.     }
  96.     public function relocate(string $countryCode, ?int $cityGroup, ?MapCoordinate $mapCoordinate): void
  97.     {
  98.         $this->countryCode $countryCode;
  99.         $this->cityGroup $cityGroup;
  100.         $this->mapCoordinate $mapCoordinate;
  101.     }
  102.     public function getId(): int
  103.     {
  104.         return $this->id;
  105.     }
  106.     public function getName(): TranslatableValue
  107.     {
  108.         return $this->name;
  109.     }
  110.     public function getUriIdentity(): string
  111.     {
  112.         return $this->uriIdentity;
  113.     }
  114.     public function getCountryCode(): string
  115.     {
  116.         return $this->countryCode;
  117.     }
  118.     public function getCityGroup(): ?int
  119.     {
  120.         return $this->cityGroup;
  121.     }
  122.     public function getCityPriceCategory(): ?int
  123.     {
  124.         return $this->cityPriceCategory;
  125.     }
  126.     public function setCityPriceCategory(?int $cityPriceCategory): void
  127.     {
  128.         $this->cityPriceCategory $cityPriceCategory;
  129.     }
  130.     /**
  131.      * @return County[]
  132.      */
  133.     public function getCounties(): Collection
  134.     {
  135.         return $this->counties;
  136.     }
  137.     public function hasCounty(County $county): bool
  138.     {
  139.         return $this->counties->contains($county);
  140.     }
  141.     /**
  142.      * @return District[]
  143.      */
  144.     public function getDistricts(): Collection
  145.     {
  146.         return $this->districts;
  147.     }
  148.     public function hasDistrict(District $district): bool
  149.     {
  150.         return $this->districts->contains($district);
  151.     }
  152.     /**
  153.      * @return Station[]
  154.      */
  155.     public function getStations(): Collection
  156.     {
  157.         return $this->stations;
  158.     }
  159.     public function hasStation(Station $station): bool
  160.     {
  161.         return $this->stations->contains($station);
  162.     }
  163.     //TODO return type?
  164.     public function getMapCoordinate(): ?MapCoordinate
  165.     {
  166.         return $this->mapCoordinate;
  167.     }
  168.     public function getTimezone(): ?string
  169.     {
  170.         return $this->timezone;
  171.     }
  172.     public function setTimezone(string $timezone): void
  173.     {
  174.         $this->timezone $timezone;
  175.     }
  176.     /**
  177.      * @inheritDoc
  178.      */
  179.     public function __toString(): string
  180.     {
  181.         return (string) $this->name;
  182.     }
  183.     public static function describe(Schema $schemaModel $model): void
  184.     {
  185.         $schema->type 'object';
  186.         $schema->properties = [
  187.             new OA\Property(property'id'type'integer'),
  188.             new OA\Property(property'name'type'object'),
  189.             new OA\Property(property'uriIdentity'type'string'),
  190.             new OA\Property(property'countryCode'type'string'),
  191.             new OA\Property(property'cityGroup'type'integer'nullabletrue),
  192.             new OA\Property(property'mapCoordinate'properties: [new OA\Property(property'latitude'type'string'), new OA\Property(property'longitude'type'string')], type'object'nullabletrue),
  193.             new OA\Property(property'timezone'type'string'nullabletrue),
  194.         ];
  195.         $schema->description 'City model';
  196.         $schema->example = [
  197.             'id' => 1,
  198.             'name' => ['ru' => 'Москва''en' => 'Moscow'],
  199.             'uriIdentity' => 'moscow',
  200.             'countryCode' => 'RU',
  201.             'cityGroup' => 1,
  202.             'mapCoordinate' => ['latitude' => '55.755825''longitude' => '37.617298'],
  203.             'timezone' => '+03:00',
  204.         ];
  205.     }
  206. }