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