Create model for Location

This commit is contained in:
Kacper Donat 2020-01-08 16:10:09 +01:00
parent eed36786ec
commit 5fb80224ce
6 changed files with 96 additions and 61 deletions

View File

@ -1,11 +1,11 @@
<span class="line__symbol flex" :class="{ [`line--${line.type}`]: true, 'line--night': line.night, 'line--fast': line.fast }">
<span class="flex align-items-stretch">
<span class="icon">
<fa :icon="['fac', line.type]" fixed-width></fa>
<fa :icon="['fac', line.type]" fixed-width/>
</span>
<span class="badge badge-dark flex">
<fa :icon="['fas', 'walking']" fixed-width v-if="line.fast"></fa>
<fa :icon="['fal', 'moon']" fixed-width v-if="line.night"></fa>
<fa :icon="['fas', 'walking']" fixed-width v-if="line.fast"/>
<fa :icon="['fal', 'moon']" fixed-width v-if="line.night"/>
{{ line.symbol }}
</span>
</span>

View File

@ -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)));
}
}
}

55
src/Model/Location.php Normal file
View File

@ -0,0 +1,55 @@
<?php
namespace App\Model;
use JMS\Serializer\Annotation as Serializer;
use Swagger\Annotations as SWG;
class Location
{
/**
* Locations longitude.
* @Serializer\Type("float")
* @Serializer\SerializedName("lng")
*/
private $longitude;
/**
* Locations latitude.
* @Serializer\Type("float")
* @Serializer\SerializedName("lat")
* @SWG\Property()
*/
private $latitude;
public function __construct(float $longitude = 0.0, float $latitude = 0.0)
{
$this->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);
}
}

View File

@ -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<string>")
* @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;
}
}
public function getLocation()
{
return $this->location;
}
public function setLocation(?Location $location): void
{
$this->location = $location;
}
}

View File

@ -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),
]);
});

View File

@ -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;