Skip to content

Instantly share code, notes, and snippets.

@abdullahpazarbasi
Created January 27, 2020 07:36
Show Gist options
  • Save abdullahpazarbasi/12f4dbee566fe30ad8a7721e3cd10367 to your computer and use it in GitHub Desktop.
Save abdullahpazarbasi/12f4dbee566fe30ad8a7721e3cd10367 to your computer and use it in GitHub Desktop.
JMS Serializer Interface Handler
<?php
namespace App\Infrastructure\Serializer\Handlers;
use JMS\Serializer\Exception\NotAcceptableException;
use JMS\Serializer\Exception\ObjectConstructionException;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use function get_class;
use function gettype;
use InvalidArgumentException;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
use RuntimeException;
/**
* Class InterfaceHandler
*/
class InterfaceHandler implements SubscribingHandlerInterface
{
/**
* @inheritDoc
*/
public static function getSubscribingMethods()
{
return [
[
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => 'Interface',
'method' => 'serializeInterface',
],
[
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'format' => 'xml',
'type' => 'Interface',
'method' => 'serializeInterface',
],
[
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => 'Interface',
'method' => 'deserializeInterface',
],
[
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
'format' => 'xml',
'type' => 'Interface',
'method' => 'deserializeInterface',
],
];
}
/**
* @param SerializationVisitorInterface $visitor
* @param mixed $object
* @param array $type
* @param SerializationContext $context
* @return mixed
*/
public function serializeInterface(
SerializationVisitorInterface $visitor,
$object,
array $type,
SerializationContext $context
) {
if (!isset($type['params'][0])) {
throw new InvalidArgumentException("Interface<...> parameter must be given");
}
$fcin = (string)$type['params'][0];
if (!interface_exists($fcin)) {
throw new RuntimeException(sprintf("Interface '%s' does not exist", $fcin));
}
$path = implode('.', array_merge(['*'], $context->getCurrentPath()));
if (!is_object($object)) {
throw new ObjectConstructionException(sprintf(
"%1\$s must be an object, it was '%2\$s'",
$path,
gettype($object)
));
}
if (!($object instanceof $fcin)) {
throw new NotAcceptableException(sprintf(
"%1\$s must implement '%2\$s'",
$path,
$fcin
));
}
$fqcn = get_class($object);
$context->stopVisiting($object);
$result = $context->getNavigator()->accept($object, ['name' => $fqcn]);
$context->startVisiting($object);
return $result;
}
/**
* @param DeserializationVisitorInterface $visitor
* @param mixed $object
* @param array $type
* @param DeserializationContext $context
* @return object
*/
public function deserializeInterface(
DeserializationVisitorInterface $visitor,
$object,
array $type,
DeserializationContext $context
): object {
if (!isset($type['params'][0])) {
throw new InvalidArgumentException("Interface<...> parameter must be given");
}
$fcin = (string)$type['params'][0];
if (!interface_exists($fcin)) {
throw new RuntimeException(sprintf("Interface '%s' does not exist", $fcin));
}
$path = implode('.', array_merge(['*'], $context->getCurrentPath()));
if (!is_object($object)) {
throw new ObjectConstructionException(sprintf(
"%1\$s must be an object, it was '%2\$s'",
$path,
gettype($object)
));
}
if (!($object instanceof $fcin)) {
throw new NotAcceptableException(sprintf(
"%1\$s must implement '%2\$s'",
$path,
$fcin
));
}
return $object;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment