From 5fb80224cec533632a08bd57ec92fe386027d0f1 Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Wed, 8 Jan 2020 16:10:09 +0100 Subject: [PATCH] Create model for Location --- resources/components/line.html | 6 +- .../Api/v1/DeparturesController.php | 14 ++-- src/Model/Location.php | 55 +++++++++++++++ src/Model/Stop.php | 68 ++++++------------- .../ZtmGdanskDataUpdateSubscriber.php | 6 +- src/Service/EntityConverter.php | 8 +-- 6 files changed, 96 insertions(+), 61 deletions(-) create mode 100644 src/Model/Location.php diff --git a/resources/components/line.html b/resources/components/line.html index 647a2c4..e8565f2 100644 --- a/resources/components/line.html +++ b/resources/components/line.html @@ -1,11 +1,11 @@ - + - - + + {{ line.symbol }} diff --git a/src/Controller/Api/v1/DeparturesController.php b/src/Controller/Api/v1/DeparturesController.php index 12eb8d4..59e37b6 100644 --- a/src/Controller/Api/v1/DeparturesController.php +++ b/src/Controller/Api/v1/DeparturesController.php @@ -51,16 +51,22 @@ class DeparturesController extends Controller * in="query", * @SWG\Items(type="string") * ) + * + * @SWG\Parameter( + * name="limit", + * description="Max departures count", + * type="integer", + * in="query" + * ) */ public function stops(DepartureRepository $departures, StopRepository $stops, Request $request) { - $stops = collect($request->query->get('stop')) - ->map([ $stops, 'getById' ]) + $stops = $stops->getManyById($request->query->get('stop')) ->flatMap([ $departures, 'getForStop' ]) ->sortBy(function (Departure $departure) { return $departure->getEstimated(); }); - return $this->json($stops->values()); + return $this->json($stops->values()->slice(0, (int)$request->query->get('limit', 8))); } -} \ No newline at end of file +} diff --git a/src/Model/Location.php b/src/Model/Location.php new file mode 100644 index 0000000..7451708 --- /dev/null +++ b/src/Model/Location.php @@ -0,0 +1,55 @@ +set($longitude, $latitude); + } + + public function getLongitude() + { + return $this->longitude; + } + + public function setLongitude(float $longitude): void + { + $this->longitude = $longitude; + } + + public function getLatitude() + { + return $this->latitude; + } + + public function setLatitude(float $latitude): void + { + $this->latitude = $latitude; + } + + public function set(float $longitude, float $latitude) + { + $this->setLongitude($longitude); + $this->setLatitude($latitude); + } +} \ No newline at end of file diff --git a/src/Model/Stop.php b/src/Model/Stop.php index 6deb0f1..123c60d 100644 --- a/src/Model/Stop.php +++ b/src/Model/Stop.php @@ -16,6 +16,7 @@ class Stop implements Referable, Fillable /** * Stop name + * * @var string * @SWG\Property(example="Jasień PKM") * @Serializer\Type("string") @@ -24,6 +25,7 @@ class Stop implements Referable, Fillable /** * Optional stop description, should not be longer than 255 chars. + * * @var string|null * @Serializer\Type("string") */ @@ -31,6 +33,7 @@ class Stop implements Referable, Fillable /** * Optional stop variant - for example number of shed. + * * @var string|null * @SWG\Property(example="01") * @Serializer\Type("string") @@ -38,21 +41,16 @@ class Stop implements Referable, Fillable private $variant; /** - * Latitude of stop - * @var float|null - * @Serializer\Exclude() + * Optional stop location in form of latitude and longitude + * + * @var ?Location + * @Serializer\Type(Location::class) */ - private $latitude; - - /** - * Longitude of stop - * @var float|null - * @Serializer\Exclude() - */ - private $longitude; + private $location; /** * True if stop is available only on demand + * * @var bool * @Serializer\Type("bool") * @SWG\Property(example=false) @@ -89,42 +87,6 @@ class Stop implements Referable, Fillable $this->variant = $variant; } - public function getLatitude(): ?float - { - return $this->latitude; - } - - public function setLatitude(?float $latitude): void - { - $this->latitude = $latitude; - } - - public function getLongitude(): ?float - { - return $this->longitude; - } - - public function setLongitude(?float $longitude): void - { - $this->longitude = $longitude; - } - - /** - * @return string[] - * @Serializer\VirtualProperty() - * @Serializer\Type("array") - * @SWG\Property(type="array", @SWG\Items(type="string", example="1")) - */ - public function getLocation(): array - { - return [ $this->latitude, $this->longitude ]; - } - - public function setLocation(array $location) - { - [$this->latitude, $this->longitude] = $location; - } - public function isOnDemand(): bool { return $this->onDemand; @@ -134,4 +96,14 @@ class Stop implements Referable, Fillable { $this->onDemand = $onDemand; } -} \ No newline at end of file + + public function getLocation() + { + return $this->location; + } + + public function setLocation(?Location $location): void + { + $this->location = $location; + } +} diff --git a/src/Provider/ZtmGdansk/ZtmGdanskDataUpdateSubscriber.php b/src/Provider/ZtmGdansk/ZtmGdanskDataUpdateSubscriber.php index 6fb5b24..8940c53 100644 --- a/src/Provider/ZtmGdansk/ZtmGdanskDataUpdateSubscriber.php +++ b/src/Provider/ZtmGdansk/ZtmGdanskDataUpdateSubscriber.php @@ -12,6 +12,7 @@ use App\Entity\TripEntity; use App\Entity\TripStopEntity; use App\Event\DataUpdateEvent; use App\Model\Line as LineModel; +use App\Model\Location; use App\Service\DataUpdater; use App\Service\IdUtils; use Carbon\Carbon; @@ -148,7 +149,7 @@ class ZtmGdanskDataUpdateSubscriber implements EventSubscriberInterface 'id' => $this->ids->generate($provider, $stop['stopId']), 'name' => trim($stop['stopName'] ?? $stop['stopDesc']), 'variant' => trim($stop['zoneName'] == 'Gdańsk' ? $stop['stopCode'] : null), - 'latitude' => $stop['stopLat'], + 'latitude' => Location::fromArray($stop['stopLat']), 'longitude' => $stop['stopLon'], 'onDemand' => (bool)$stop['onDemand'], 'provider' => $provider, @@ -196,7 +197,8 @@ class ZtmGdanskDataUpdateSubscriber implements EventSubscriberInterface $this->ids->generate($provider, $stop['stopId']) ), 'track' => $entity, - 'order' => $stop['stopSequence'] + (int)($stop['stopId'] > 30000), // HACK! Gdynia has 0 based sequence + // HACK! Gdynia has 0 based sequence + 'order' => $stop['stopSequence'] + (int)($stop['stopId'] > 30000), ]); }); diff --git a/src/Service/EntityConverter.php b/src/Service/EntityConverter.php index 8a6c8e9..dddb066 100644 --- a/src/Service/EntityConverter.php +++ b/src/Service/EntityConverter.php @@ -3,7 +3,7 @@ namespace App\Service; use App\Entity\{Entity, LineEntity, OperatorEntity, StopEntity, TrackEntity, TripEntity, TripStopEntity}; -use App\Model\{Line, Operator, ScheduleStop, Stop, Track, Trip}; +use App\Model\{Line, Location, Operator, ScheduleStop, Stop, Track, Trip}; use App\Service\Proxy\ReferenceFactory; use Doctrine\ORM\PersistentCollection; use Doctrine\ORM\Proxy\Proxy; @@ -80,10 +80,10 @@ final class EntityConverter 'name' => $entity->getName(), 'variant' => $entity->getVariant(), 'description' => $entity->getDescription(), - 'location' => [ - $entity->getLatitude(), + 'location' => new Location( $entity->getLongitude(), - ], + $entity->getLatitude() + ), ]); break;