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