Add carbon data type to doctrine config

This commit is contained in:
Kacper Donat 2020-01-13 14:55:07 +01:00
parent e33bef7b21
commit e3ec5a5893
5 changed files with 55 additions and 5 deletions

View File

@ -7,6 +7,8 @@ doctrine:
url: '%env(resolve:DATABASE_URL)%' url: '%env(resolve:DATABASE_URL)%'
logging: true logging: true
profiling: true profiling: true
types:
datetime: App\Doctrine\CarbonDateTimeType
orm: orm:
auto_generate_proxy_classes: '%kernel.debug%' auto_generate_proxy_classes: '%kernel.debug%'

View File

@ -0,0 +1,47 @@
<?php
namespace App\Doctrine;
use Carbon\Carbon;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateTimeType;
class CarbonDateTimeType extends DateTimeType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if ($value instanceof \DateTimeInterface) {
$value = Carbon::instance($value);
}
if ($value instanceof Carbon) {
$value = $value->tz('UTC');
}
return parent::convertToDatabaseValue($value, $platform);
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (null === $value || $value instanceof Carbon) {
return $value;
}
$converted = Carbon::createFromFormat(
$platform->getDateTimeFormatString(),
$value,
'UTC'
);
if (!$converted) {
throw ConversionException::conversionFailedFormat(
$value,
$this->getName(),
$platform->getDateTimeFormatString()
);
}
return $converted;
}
}

View File

@ -20,7 +20,7 @@ class GenericScheduleRepository extends DatabaseRepository implements ScheduleRe
{ {
public function getDeparturesForStop( public function getDeparturesForStop(
Stop $stop, Stop $stop,
\DateTime $from, Carbon $from,
int $count = ScheduleRepository::DEFAULT_DEPARTURES_COUNT int $count = ScheduleRepository::DEFAULT_DEPARTURES_COUNT
): Collection { ): Collection {
$query = $this->em $query = $this->em

View File

@ -3,6 +3,7 @@
namespace App\Provider; namespace App\Provider;
use App\Model\Stop; use App\Model\Stop;
use Carbon\Carbon;
use Tightenco\Collect\Support\Collection; use Tightenco\Collect\Support\Collection;
interface ScheduleRepository interface ScheduleRepository
@ -11,7 +12,7 @@ interface ScheduleRepository
public function getDeparturesForStop( public function getDeparturesForStop(
Stop $stop, Stop $stop,
\DateTime $from, Carbon $from,
int $count = ScheduleRepository::DEFAULT_DEPARTURES_COUNT int $count = ScheduleRepository::DEFAULT_DEPARTURES_COUNT
): Collection; ): Collection;
} }

View File

@ -42,7 +42,7 @@ class ZtmGdanskDepartureRepository implements DepartureRepository
public function getForStop(Stop $stop): Collection public function getForStop(Stop $stop): Collection
{ {
$real = $this->getRealDepartures($stop); $real = $this->getRealDepartures($stop);
$now = Carbon::now(); $now = Carbon::now('UTC');
$first = $real->map(t\getter('scheduled'))->min() ?? $now; $first = $real->map(t\getter('scheduled'))->min() ?? $now;
$scheduled = $this->getScheduledDepartures($stop, $first < $now ? $now : $first); $scheduled = $this->getScheduledDepartures($stop, $first < $now ? $now : $first);
@ -60,7 +60,7 @@ class ZtmGdanskDepartureRepository implements DepartureRepository
$lines = $this->lines->getManyById($lines)->keyBy(t\property('id')); $lines = $this->lines->getManyById($lines)->keyBy(t\property('id'));
return collect($estimates)->map(function ($delay) use ($stop, $lines) { return collect($estimates)->map(function ($delay) use ($stop, $lines) {
$scheduled = (new Carbon($delay['theoreticalTime'], 'Europe/Warsaw')); $scheduled = (new Carbon($delay['theoreticalTime'], 'Europe/Warsaw'))->tz('UTC');
$estimated = (clone $scheduled)->addSeconds($delay['delayInSeconds']); $estimated = (clone $scheduled)->addSeconds($delay['delayInSeconds']);
return Departure::createFromArray([ return Departure::createFromArray([
@ -85,7 +85,7 @@ class ZtmGdanskDepartureRepository implements DepartureRepository
private function pair(Collection $schedule, Collection $real) private function pair(Collection $schedule, Collection $real)
{ {
$key = function (Departure $departure) { $key = function (Departure $departure) {
return sprintf("%s::%s", $departure->getScheduled()->format("H:i"), $departure->getLine()->getSymbol()); return sprintf("%s::%s", $departure->getLine()->getSymbol(), $departure->getScheduled()->format("H:i"));
}; };
$schedule = $schedule->keyBy($key)->all(); $schedule = $schedule->keyBy($key)->all();