src/Entity/Location/City.php line 23

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 Symfony\Component\Serializer\Annotation\Groups;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Doctrine\ORM\Mapping\Index;
  16. #[ORM\Table(name'cities')]
  17. #[Index(name'idx_uri_identity'columns: ['uri_identity'])]
  18. #[ORM\Entity(repositoryClassCityRepository::class)]
  19. #[ORM\Cache(usage'NONSTRICT_READ_WRITE'region'geoposition')]
  20. class City
  21. {
  22.     const GROUP_MEGALOPOLIS 1;
  23.     const GROUP_MOSCOW_REGION 2;
  24.     const PRICE_CATEGORY_1 1;
  25.     const PRICE_CATEGORY_2 2;
  26.     const PRICE_CATEGORY_3 3;
  27.     const PRICE_CATEGORY_4 4;
  28.     const PRICE_CATEGORY_5 5;
  29.     #[ORM\Id]
  30.     #[ORM\Column(name'id'type'integer')]
  31.     #[ORM\GeneratedValue(strategy'AUTO')]
  32.     #[Groups('profile')]
  33.     protected int $id;
  34.     #[ORM\Column(name'name'type'translatable')]
  35.     #[Groups('profile')]
  36.     protected TranslatableValue $name;
  37.     #[ORM\Column(name'uri_identity'type'string'length128)]
  38.     #[Groups('profile')]
  39.     protected string $uriIdentity;
  40.     #[ORM\Column(name'country_code'type'string'length2)]
  41.     #[Groups('profile')]
  42.     protected string $countryCode;
  43.     #[ORM\Column(name'city_group'type'smallint'nullabletrue)]
  44.     protected ?int $cityGroup;
  45.     #[ORM\Column(name'city_price_category'type'smallint'nullabletrue)]
  46.     protected ?int $cityPriceCategory null;
  47.     /** @var County[] */
  48.     #[ORM\OneToMany(targetEntityCounty::class, mappedBy'city')]
  49.     #[ORM\Cache(usage'READ_ONLY'region'geoposition')]
  50.     protected Collection $counties;
  51.     /** @var District[] */
  52.     #[ORM\OneToMany(targetEntityDistrict::class, mappedBy'city')]
  53.     #[ORM\Cache(usage'READ_ONLY'region'geoposition')]
  54.     protected Collection $districts;
  55.     /** @var Station[] */
  56.     #[ORM\OneToMany(targetEntityStation::class, mappedBy'city')]
  57.     #[ORM\OrderBy(['name' => 'ASC'])]
  58.     #[ORM\Cache(usage'READ_ONLY'region'geoposition')]
  59.     protected Collection $stations;
  60.     #[ORM\Embedded(class: MapCoordinate::class, columnPrefixfalse)]
  61.     protected ?MapCoordinate $mapCoordinate;
  62.     #[ORM\Column(name'timezone'type'string'length6nullabletrue)]
  63.     protected ?string $timezone;
  64.     public function __construct(TranslatableValue $namestring $uriIdentitystring $countryCode, ?int $cityGroup null, ?MapCoordinate $mapCoordinate null)
  65.     {
  66.         $this->rename($name$uriIdentity);
  67.         $this->relocate($countryCode$cityGroup$mapCoordinate);
  68.         $this->counties = new ArrayCollection();
  69.         $this->districts = new ArrayCollection();
  70.         $this->stations = new ArrayCollection();
  71.     }
  72.     /**
  73.      * @param City|string $cityOrUriIdentity Instance of City class or URI identity string
  74.      */
  75.     public function equals(City|string $cityOrUriIdentity): bool
  76.     {
  77.         if ($cityOrUriIdentity instanceof City) {
  78.             return $cityOrUriIdentity->id === $this->id;
  79.         } elseif (is_string($cityOrUriIdentity)) {
  80.             return $cityOrUriIdentity === $this->uriIdentity;
  81.         }
  82.         throw new \InvalidArgumentException(__METHOD__.' can accept either string with URI identity or instance of App\\Entity\\Location\\City class.');
  83.     }
  84.     public function rename(TranslatableValue $namestring $uriIdentity): void
  85.     {
  86.         $this->name $name;
  87.         $this->uriIdentity $uriIdentity;
  88.     }
  89.     public function relocate(string $countryCode, ?int $cityGroup, ?MapCoordinate $mapCoordinate): void
  90.     {
  91.         $this->countryCode $countryCode;
  92.         $this->cityGroup $cityGroup;
  93.         $this->mapCoordinate $mapCoordinate;
  94.     }
  95.     public function getId(): int
  96.     {
  97.         return $this->id;
  98.     }
  99.     public function getName(): TranslatableValue
  100.     {
  101.         return $this->name;
  102.     }
  103.     public function getUriIdentity(): string
  104.     {
  105.         return $this->uriIdentity;
  106.     }
  107.     public function getCountryCode(): string
  108.     {
  109.         return $this->countryCode;
  110.     }
  111.     public function getCityGroup(): ?int
  112.     {
  113.         return $this->cityGroup;
  114.     }
  115.     public function getCityPriceCategory(): ?int
  116.     {
  117.         return $this->cityPriceCategory;
  118.     }
  119.     public function setCityPriceCategory(?int $cityPriceCategory): void
  120.     {
  121.         $this->cityPriceCategory $cityPriceCategory;
  122.     }
  123.     /**
  124.      * @return County[]
  125.      */
  126.     public function getCounties(): Collection
  127.     {
  128.         return $this->counties;
  129.     }
  130.     public function hasCounty(County $county): bool
  131.     {
  132.         return $this->counties->contains($county);
  133.     }
  134.     /**
  135.      * @return District[]
  136.      */
  137.     public function getDistricts(): Collection
  138.     {
  139.         return $this->districts;
  140.     }
  141.     public function hasDistrict(District $district): bool
  142.     {
  143.         return $this->districts->contains($district);
  144.     }
  145.     /**
  146.      * @return Station[]
  147.      */
  148.     public function getStations(): Collection
  149.     {
  150.         return $this->stations;
  151.     }
  152.     public function hasStation(Station $station): bool
  153.     {
  154.         return $this->stations->contains($station);
  155.     }
  156.     //TODO return type?
  157.     public function getMapCoordinate(): ?MapCoordinate
  158.     {
  159.         return $this->mapCoordinate;
  160.     }
  161.     public function getTimezone(): ?string
  162.     {
  163.         return $this->timezone;
  164.     }
  165.     public function setTimezone(string $timezone): void
  166.     {
  167.         $this->timezone $timezone;
  168.     }
  169.     /**
  170.      * @inheritDoc
  171.      */
  172.     public function __toString(): string
  173.     {
  174.         return (string) $this->name;
  175.     }
  176. }