Skip to content

Instantly share code, notes, and snippets.

@koemeet
Created November 21, 2014 20:33
Show Gist options
  • Save koemeet/ebfe0f07fe7b1df93b54 to your computer and use it in GitHub Desktop.
Save koemeet/ebfe0f07fe7b1df93b54 to your computer and use it in GitHub Desktop.
JMS Serializer & willdurand Hateoas JSON-API serializer
<?php
/**
* This file is part of the Mango package.
*
* (c) Mango
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Mango\Bundle\ApiBundle\Serializer;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\PersistentCollection;
use Hateoas\Model\Embedded;
use Hateoas\Model\Link;
use Hateoas\Serializer\JsonSerializerInterface;
use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\SerializationContext;
use Mango\Bundle\ApiBundle\Resolver\ClassTypeKeyResolver;
use Mango\Component\Core\Model\Workspace;
/**
* Class JsonApiSerializer
* @package Mango\Bundle\ApiBundle\Serializer
* @author Steffen Brem <[email protected]>
*/
class JsonApiSerializer implements JsonSerializerInterface
{
protected $linked = array();
/**
* @param Link[] $links
* @param JsonSerializationVisitor $visitor
* @param SerializationContext $context
*/
public function serializeLinks(array $links, JsonSerializationVisitor $visitor, SerializationContext $context)
{
$serializedLinks = array();
foreach ($links as $link) {
$rel = $link->getRel();
$serializedLinks[$rel] = array(
'href' => $link->getHref()
);
}
$visitor->addData('links', $serializedLinks);
}
/**
* @param Embedded[] $embeddeds
* @param JsonSerializationVisitor $visitor
* @param SerializationContext $context
*/
public function serializeEmbeddeds(array $embeddeds, JsonSerializationVisitor $visitor, SerializationContext $context)
{
foreach ($embeddeds as $embedded) {
$data = $embedded->getData();
if ($this->isArray($data)) {
$ids = array();
foreach ($data as $object) {
$rel = $this->getRelKey($object);
$serializedObject = $context->accept($object);
$this->addLinkedObject($rel, $serializedObject);
$ids[] = $this->getObjectId($serializedObject);
}
$value = $ids;
} else {
$rel = $this->getRelKey($data);
$serializedObject = $context->accept($data);
$this->addLinkedObject($rel, $serializedObject);
$value = $this->getObjectId($serializedObject);
}
$visitor->addData($embedded->getRel(), $value);
}
$linked = $this->getLinked();
if (!empty($linked)) {
$root = (array)$visitor->getRoot();
$root['linked'] = $this->getLinked();
$visitor->setRoot($root);
}
}
/**
* @param $data
* @return bool
*/
protected function isArray($data)
{
return is_array($data) || $data instanceof \Traversable;
}
/**
* @return array
*/
protected function getLinked()
{
return $this->linked;
}
/**
* @param $serializedObject
* @return mixed
*/
protected function getObjectId($serializedObject)
{
return $serializedObject['id'];
}
/**
* @param $rel
* @param $serializedObject
*/
protected function addLinkedObject($rel, $serializedObject)
{
if (!$serializedObject) {
return;
}
if (!isset($this->linked[$rel])) {
$this->linked[$rel] = array();
}
if (!in_array($serializedObject, $this->linked[$rel])) {
$this->linked[$rel][] = $serializedObject;
}
}
/**
* @param $object
* @return string
*/
protected function getRelKey($object)
{
$resolver = new ClassTypeKeyResolver();
$typeKey = $resolver->resolve($object, true);
return $typeKey;
}
}
@silverqx
Copy link

Sry the abuse report was a click mistake. :-[ And I can't revert it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment