Created
January 12, 2020 11:53
-
-
Save sunviwo/44496cf0e719ef64677cd2dab7c3d94b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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