Skip to content

Instantly share code, notes, and snippets.

@sunviwo
Created January 12, 2020 11:53
Show Gist options
  • Save sunviwo/44496cf0e719ef64677cd2dab7c3d94b to your computer and use it in GitHub Desktop.
Save sunviwo/44496cf0e719ef64677cd2dab7c3d94b to your computer and use it in GitHub Desktop.
<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Property;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class PropertyWriteSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['propertyWritten', EventPriorities::PRE_WRITE],
];
}
public function propertyWritten(GetResponseForControllerResultEvent $event)
{
$property = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$property instanceof Property ||
!in_array($method, [Request::METHOD_POST, Request::METHOD_PUT])) {
return;
}
$property->setPoint(
sprintf(
'POINT(%f %f)',
(string)$property->getLongitude(),
(string)$property->getLatitude()
)
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment