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(['preview', '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.     #[Groups(['preview', 'listing'])]
  42.     protected ?District $district;
  43.     #[ORM\JoinTable(name: 'city_subway_station_lines')]
  44.     #[ORM\JoinColumn(name: 'station_id', referencedColumnName: 'id')]
  45.     #[ORM\InverseJoinColumn(name: 'line_id', referencedColumnName: 'id')]
  46.     #[ORM\ManyToMany(targetEntity: Line::class, fetch: 'EXTRA_LAZY')]
  47.     #[Groups(['preview', 'listing'])]
  48.     protected Collection $lines;
  49.     #[ORM\Column(name: 'name', type: 'translatable')]
  50.     #[Groups(['preview', 'listing'])]
  51.     protected TranslatableValue $name;
  52.     #[ORM\Column(name: 'uri_identity', type: 'string', length: 128)]
  53.     #[Groups(['preview', 'listing'])]
  54.     protected string $uriIdentity;
  55.     public function __construct(City $city, ?County $county, ?District $district, TranslatableValue $name, string $uriIdentity)
  56.     {
  57.         $this->city = $city;
  58.         $this->county = $county;
  59.         $this->district = $district;
  60.         $this->name = $name;
  61.         $this->uriIdentity = $uriIdentity;
  62.         $this->lines = new ArrayCollection();
  63.     }
  64.     public function rename(TranslatableValue $name, string $uriIdentity): void
  65.     {
  66.         $this->name = $name;
  67.         $this->uriIdentity = $uriIdentity;
  68.     }
  69.     public function relocate(City $city, ?County $county, ?District $district): void
  70.     {
  71.         $this->city = $city;
  72.         $this->county = (null !== $county && $this->city->hasCounty($county)) ? $county : null;
  73.         $this->district = (
  74.             null !== $district
  75.             && $this->city->hasDistrict($district)
  76.             && (null === $this->county || $this->county->hasDistrict($district))
  77.         ) ? $district : null;
  78.     }
  79.     public function getId(): int
  80.     {
  81.         return $this->id;
  82.     }
  83.     public function getCity(): City
  84.     {
  85.         return $this->city;
  86.     }
  87.     public function getCounty(): ?County
  88.     {
  89.         return $this->county;
  90.     }
  91.     public function getDistrict(): ?District
  92.     {
  93.         return $this->district;
  94.     }
  95.     public function getName(): TranslatableValue
  96.     {
  97.         return $this->name;
  98.     }
  99.     public function getUriIdentity(): string
  100.     {
  101.         return $this->uriIdentity;
  102.     }
  103.     public function setDistrict(?District $district): void
  104.     {
  105.         $this->district = $district;
  106.     }
  107.     public function lines(): iterable
  108.     {
  109.         return $this->lines;
  110.     }
  111.     public function specifyLines(Line ...$lines): void
  112.     {
  113.         $this->lines = new ArrayCollection($lines);
  114.     }
  115.     /**
  116.      * @inheritDoc
  117.      */
  118.     public function __toString(): string
  119.     {
  120.         return (string) $this->name;
  121.     }
  122.     public static function describe(Schema $schema, Model $model): void
  123.     {
  124.         $schema->type = 'object';
  125.         $schema->properties = [
  126.             new OA\Property(property: 'id', type: 'integer'),
  127.             new OA\Property(property: 'name', type: 'object'),
  128.             new OA\Property(property: 'uriIdentity', type: 'string'),
  129.             new OA\Property(property: 'lines', type: 'array', items: new OA\Items(properties: [
  130.                 new OA\Property(property: 'id', type: 'integer'),
  131.                 new OA\Property(property: 'name', type: 'string'),
  132.                 new OA\Property(property: 'color', type: 'string'),
  133.             ], type: 'object')),
  134.         ];
  135.         $schema->description = 'Subway station model';
  136.         $schema->example = [
  137.             'id' => 1,
  138.             'name' => ['ru' => 'Академическая', 'en' => 'Akademicheskaya'],
  139.             'uriIdentity' => 'akademicheskaya',
  140.             'lines' => [
  141.                 'id' => 1,
  142.                 'name' => 'Калининская',
  143.                 'color' => '#000000',
  144.             ],
  145.         ];
  146.     }
  147. }