src/Entity/Location/Station.php line 26

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by simpson <simpsonwork@gmail.com>
  4.  * Date: 2019-03-19
  5.  * Time: 17:13
  6.  */
  7. namespace App\Entity\Location;
  8. use AngelGamez\TranslatableBundle\Entity\TranslatableValue;
  9. use ApiPlatform\Core\Annotation\ApiResource;
  10. use App\Entity\Location\Subway\Line;
  11. use App\Repository\StationRepository;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Nelmio\ApiDocBundle\Model\Model;
  15. use Nelmio\ApiDocBundle\ModelDescriber\SelfDescribingModelInterface;
  16. use OpenApi\Annotations\Schema;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use OpenApi\Attributes as OA;
  20. #[ORM\Table(name: 'city_stations')]
  21. #[ORM\Entity(repositoryClass: StationRepository::class)]
  22. #[ORM\Cache(usage: 'NONSTRICT_READ_WRITE', region: 'geoposition')]
  23. class Station implements SelfDescribingModelInterface
  24. {
  25.     #[ORM\Id]
  26.     #[ORM\Column(name: 'id', type: 'integer')]
  27.     #[ORM\GeneratedValue(strategy: 'AUTO')]
  28.     #[Groups('profile', 'listing')]
  29.     protected int $id;
  30.     #[ORM\JoinColumn(name: 'city_id', referencedColumnName: 'id')]
  31.     #[ORM\ManyToOne(targetEntity: City::class, inversedBy: 'stations')]
  32.     #[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
  33.     protected City $city;
  34.     #[ORM\JoinColumn(name: 'county_id', referencedColumnName: 'id')]
  35.     #[ORM\ManyToOne(targetEntity: County::class, inversedBy: 'stations')]
  36.     #[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
  37.     protected ?County $county;
  38.     #[ORM\JoinColumn(name: 'district_id', referencedColumnName: 'id')]
  39.     #[ORM\ManyToOne(targetEntity: District::class, inversedBy: 'stations')]
  40.     #[ORM\Cache(usage: 'READ_ONLY', region: 'geoposition')]
  41.     protected ?District $district;
  42.     #[ORM\JoinTable(name: 'city_subway_station_lines')]
  43.     #[ORM\JoinColumn(name: 'station_id', referencedColumnName: 'id')]
  44.     #[ORM\InverseJoinColumn(name: 'line_id', referencedColumnName: 'id')]
  45.     #[ORM\ManyToMany(targetEntity: Line::class, fetch: 'EXTRA_LAZY')]
  46.     #[Groups('profile', 'listing')]
  47.     protected Collection $lines;
  48.     #[ORM\Column(name: 'name', type: 'translatable')]
  49.     #[Groups('profile', 'listing')]
  50.     protected TranslatableValue $name;
  51.     #[ORM\Column(name: 'uri_identity', type: 'string', length: 128)]
  52.     #[Groups('profile', 'listing')]
  53.     protected string $uriIdentity;
  54.     public function __construct(City $city, ?County $county, ?District $district, TranslatableValue $name, string $uriIdentity)
  55.     {
  56.         $this->city = $city;
  57.         $this->county = $county;
  58.         $this->district = $district;
  59.         $this->name = $name;
  60.         $this->uriIdentity = $uriIdentity;
  61.         $this->lines = new ArrayCollection();
  62.     }
  63.     public function rename(TranslatableValue $name, string $uriIdentity): void
  64.     {
  65.         $this->name = $name;
  66.         $this->uriIdentity = $uriIdentity;
  67.     }
  68.     public function relocate(City $city, ?County $county, ?District $district): void
  69.     {
  70.         $this->city = $city;
  71.         $this->county = (null !== $county && $this->city->hasCounty($county)) ? $county : null;
  72.         $this->district = (
  73.             null !== $district
  74.             && $this->city->hasDistrict($district)
  75.             && (null === $this->county || $this->county->hasDistrict($district))
  76.         ) ? $district : null;
  77.     }
  78.     public function getId(): int
  79.     {
  80.         return $this->id;
  81.     }
  82.     public function getCity(): City
  83.     {
  84.         return $this->city;
  85.     }
  86.     public function getCounty(): ?County
  87.     {
  88.         return $this->county;
  89.     }
  90.     public function getDistrict(): ?District
  91.     {
  92.         return $this->district;
  93.     }
  94.     public function getName(): TranslatableValue
  95.     {
  96.         return $this->name;
  97.     }
  98.     public function getUriIdentity(): string
  99.     {
  100.         return $this->uriIdentity;
  101.     }
  102.     public function setDistrict(?District $district): void
  103.     {
  104.         $this->district = $district;
  105.     }
  106.     public function lines(): iterable
  107.     {
  108.         return $this->lines;
  109.     }
  110.     public function specifyLines(Line ...$lines): void
  111.     {
  112.         $this->lines = new ArrayCollection($lines);
  113.     }
  114.     /**
  115.      * @inheritDoc
  116.      */
  117.     public function __toString(): string
  118.     {
  119.         return (string) $this->name;
  120.     }
  121.     public static function describe(Schema $schema, Model $model): void
  122.     {
  123.         $schema->type = 'object';
  124.         $schema->properties = [
  125.             new OA\Property(property: 'id', type: 'integer'),
  126.             new OA\Property(property: 'name', type: 'object'),
  127.             new OA\Property(property: 'uriIdentity', type: 'string'),
  128.             new OA\Property(property: 'lines', type: 'array', items: new OA\Items(properties: [
  129.                 new OA\Property(property: 'id', type: 'integer'),
  130.                 new OA\Property(property: 'name', type: 'string'),
  131.                 new OA\Property(property: 'color', type: 'string'),
  132.             ], type: 'object')),
  133.         ];
  134.         $schema->description = 'Subway station model';
  135.         $schema->example = [
  136.             'id' => 1,
  137.             'name' => ['ru' => 'Академическая', 'en' => 'Akademicheskaya'],
  138.             'uriIdentity' => 'akademicheskaya',
  139.             'lines' => [
  140.                 'id' => 1,
  141.                 'name' => 'Калининская',
  142.                 'color' => '#000000',
  143.             ],
  144.         ];
  145.     }
  146. }