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