Last active
May 31, 2018 07:38
-
-
Save jrencz/6070574 to your computer and use it in GitHub Desktop.
Generating LiipImagineBundle media URIs for FOSRestBundle Api using JMSSerializerBundle
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
liip_imagine: | |
filter_sets: | |
thumbnail_name: | |
quality: 95 | |
filters: | |
thumbnail: { size: [120, 90], mode: inset } |
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 Acme\AcmeDemoBundle\Entity; | |
use JMS\Serializer\Annotation as Serializer; | |
/** | |
* Entity | |
* | |
* @Serializer\ExclusionPolicy("all") | |
*/ | |
class Entity | |
{ | |
/** | |
* @var string|null | |
* | |
* | |
* @Serializer\Expose | |
* @Serializer\Accessor(getter="getBackgroundFilenameForSerialization") | |
* @Serializer\Type("Filename") | |
* @Serializer\SerializedName("background_uri") | |
*/ | |
private $backgroundFilename; | |
/** | |
* @return Media | |
*/ | |
public function getBackgroundFilenameForSerialization() | |
{ | |
return new Filename($this->backgroundFilename); | |
} | |
} |
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 Acme\AcmeDemoBundle\Entity; | |
/** | |
* Class Filename | |
* | |
* JMSSerializer Handler requires type to be custom class (it cannot be a primitive) | |
* | |
*/ | |
class Filename { | |
/** | |
* @var string | |
*/ | |
public $filename; | |
public function __construct($filename) | |
{ | |
$this->setFilename($filename); | |
} | |
/** | |
* @param string $name | |
* @return Filename | |
*/ | |
public function setFilename($name) | |
{ | |
$this->name = $name; | |
return $this; | |
} | |
/** | |
* @return string filename | |
*/ | |
public function getFilename() | |
{ | |
return $this->filename; | |
} | |
/** | |
* @return string filename | |
*/ | |
public function __toString() | |
{ | |
return $this->filename; | |
} | |
} |
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 Acme\AcmeDemoBundle\Handler; | |
use Acme\AcmeDemoBundle\Entity\Filename; | |
use JMS\Serializer\GraphNavigator; | |
use JMS\Serializer\Handler\SubscribingHandlerInterface; | |
use JMS\Serializer\VisitorInterface; | |
use Liip\ImagineBundle\Imagine\Cache\CacheManager; | |
class FilenameHandler implements SubscribingHandlerInterface | |
{ | |
function __construct(CacheManager $manager) | |
{ | |
$this->manager = $manager; | |
} | |
public static function getSubscribingMethods() | |
{ | |
return array( | |
array( | |
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, | |
'format' => 'json', | |
'type' => 'Filename', | |
'method' => 'serializeFilenameToJson', | |
), | |
); | |
} | |
public function serializeFilenameToJson(VisitorInterface $visitor, Filename $filename, array $type) | |
{ | |
return $this->manager->getBrowserPath($filename, 'thumbnail_name', true); | |
} | |
} |
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
acme.serializer.filename_handler: | |
class: Acme\AcmeDemoBundle\Handler\FilenameHandler | |
arguments: | |
- "@liip_imagine.cache.manager" | |
tags: | |
- { name: 'jms_serializer.handler', type: Filename, format: json} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment