Created
February 26, 2016 06:58
-
-
Save svolobuev/ecd931f27982ef6f6ad8 to your computer and use it in GitHub Desktop.
JMS Serializer Subscriber example
This file contains hidden or 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
services: | |
serializer.subscriber.news: | |
class: AppBundle\Serializer\Subscriber\NewsSubscriber | |
arguments: [@vich_uploader.storage, @liip_imagine.cache.manager, @request_stack] | |
tags: | |
- { name: jms_serializer.event_subscriber } |
This file contains hidden or 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 AppBundle\Serializer\Subscriber; | |
use AppBundle\Entity\News; | |
use JMS\Serializer\EventDispatcher\EventSubscriberInterface; | |
use JMS\Serializer\EventDispatcher\ObjectEvent; | |
use Liip\ImagineBundle\Imagine\Cache\CacheManager; | |
use Symfony\Component\HttpFoundation\RequestStack; | |
use Vich\UploaderBundle\Storage\StorageInterface; | |
class NewsSubscriber implements EventSubscriberInterface | |
{ | |
/** | |
* @var CacheManager | |
*/ | |
private $imagineCacheManager; | |
/** | |
* @var StorageInterface | |
*/ | |
private $storage; | |
/** | |
* @var RequestStack | |
*/ | |
private $requestStack; | |
/** | |
* @param StorageInterface $storage | |
* @param CacheManager $imagineCacheManager | |
*/ | |
public function __construct(StorageInterface $storage, CacheManager $imagineCacheManager, RequestStack $requestStack) | |
{ | |
$this->imagineCacheManager = $imagineCacheManager; | |
$this->storage = $storage; | |
$this->requestStack = $requestStack; | |
} | |
public static function getSubscribedEvents() | |
{ | |
return [ | |
[ | |
'event' => 'serializer.post_serialize', | |
'method' => 'onPostSerialize', | |
'class' => News::class, | |
], | |
]; | |
} | |
public function onPostSerialize(ObjectEvent $event) | |
{ | |
/** @var News $object */ | |
$object = $event->getObject(); | |
$uri = $this->storage->resolveUri($object, 'imageFile'); | |
$request = $this->requestStack->getCurrentRequest(); | |
$thumbUrl = null; | |
$url = null; | |
if ($uri) { | |
$thumbUrl = $this->imagineCacheManager->getBrowserPath($uri, 'news_image_api'); | |
$url = $this->imagineCacheManager->getBrowserPath($uri, 'news_image'); | |
} | |
/** @var \JMS\Serializer\JsonSerializationVisitor $visitor */ | |
$visitor = $event->getVisitor(); | |
$visitor->addData('image_thumb', $thumbUrl); | |
$visitor->addData('image', $url); | |
} | |
} |
$visitor->addData('image_thumb', $thumbUrl);
is an old version of JMS, now use
$visitor->visitProperty(new StaticPropertyMetadata('', 'image_thumb', null), $thumbUrl);
Thanks.
I'm agree with @NikitaKharkov => "no examples in documentation" -> no easy to use JMS Serializer !
To complete @yfrommelt, for JMS serializer 3.x (3.4 for me) :
Into \JMS\Serializer\JsonSerializationVisitor.php class (vendor\JMS\src folder), one method exists called "setData".
However, this method is deprecated, and with this comment :
@deprecated Use `::visitProperty(new StaticPropertyMetadata('', 'name', 'value'), 'value')` instead
*
* Allows you to replace existing data on the current object element.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you very much! It's important because there are no examples in documentation!