Skip to content

Commit

Permalink
add context to deserialize xml/json response
Browse files Browse the repository at this point in the history
 - remove namespace prefixes before deserialize
  • Loading branch information
rafrsr committed Jun 21, 2016
1 parent e62e1c1 commit e534aa3
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/ApiRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Rafrsr\GenericApi;

use JMS\Serializer\DeserializationContext;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerBuilder;
use Psr\Http\Message\RequestInterface;
Expand Down Expand Up @@ -303,25 +304,28 @@ public function withXMLBody($data, SerializationContext $context = null)
}

/**
* @param $class string|object $class class to unserialize the response, otherwise return a array
* @param $class string|object $class class to unserialize the response, otherwise return a array
* @param DeserializationContext $context context
*
* @return $this
*/
public function withJsonResponse($class = null)
public function withJsonResponse($class = null, DeserializationContext $context = null)
{
$this->responseParser = new JsonMessageParser($class);
$this->responseParser = new JsonMessageParser($class, $context);

return $this;
}

/**
* @param $class string|object $class class to unserialize the response, otherwise return a array
* @param string|object $class class to unserialize the response, otherwise return a array
* @param DeserializationContext $context context
* @param boolean $removeNamespacePrefixes remove all namespaces prefixes before deserialize
*
* @return $this
*/
public function withXmlResponse($class = null)
public function withXmlResponse($class = null, DeserializationContext $context = null, $removeNamespacePrefixes = false)
{
$this->responseParser = new XMLMessageParser($class);
$this->responseParser = new XMLMessageParser($class, $context, $removeNamespacePrefixes);

return $this;
}
Expand Down
14 changes: 11 additions & 3 deletions src/Serializer/AbstractSerializerMessageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Rafrsr\GenericApi\Serializer;

use JMS\Serializer\DeserializationContext;
use JMS\Serializer\SerializerBuilder;
use Psr\Http\Message\MessageInterface;

Expand All @@ -26,9 +27,15 @@ abstract class AbstractSerializerMessageParser implements MessageParserInterface
protected $class;

/**
* @param string|object $class class to create a valid response object
* @var DeserializationContext
*/
public function __construct($class = null)
protected $context;

/**
* @param string|object $class class to create a valid response object
* @param DeserializationContext $context context
*/
public function __construct($class = null, DeserializationContext $context = null)
{
if (is_object($class)) {
$class = get_class($class);
Expand All @@ -44,7 +51,8 @@ protected function deserialize(MessageInterface $message, $format)
if ($this->class) {
$content = $message->getBody()->getContents();

$parsedResponse = SerializerBuilder::create()->build()->deserialize($content, $this->class, $format);
$parsedResponse = SerializerBuilder::create()->build()
->deserialize($content, $this->class, $format, $this->context);

return $parsedResponse;
} else {
Expand Down
28 changes: 25 additions & 3 deletions src/Serializer/XMLMessageParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,45 @@

namespace Rafrsr\GenericApi\Serializer;

use JMS\Serializer\DeserializationContext;
use JMS\Serializer\SerializerBuilder;
use Psr\Http\Message\MessageInterface;

/**
* Class XMLMessageParser
*/
class XMLMessageParser extends AbstractSerializerMessageParser
{
protected $removeNamespacePrefixes;

/**
* @param string|object $class class to create a valid response object
* @param DeserializationContext $context context
* @param boolean $removeNamespacePrefixes remove all namespaces prefixes before deserialize
*/
public function __construct($class = null, DeserializationContext $context = null, $removeNamespacePrefixes = false)
{
parent::__construct($class, $context);
$this->removeNamespacePrefixes = $removeNamespacePrefixes;
}

/**
* @inheritdoc
*/
public function parse(MessageInterface $message)
{
if ($deserialized = $this->deserialize($message, 'xml')) {
return $deserialized;
$content = $message->getBody()->getContents();
if ($this->removeNamespacePrefixes) {
$content = preg_replace('/(<\/?)(\w+:)/', '$1', $content);
}

if ($this->class) {
$parsedResponse = SerializerBuilder::create()->build()
->deserialize($content, $this->class, 'xml', $this->context);
} else {
return simplexml_load_string($message->getBody()->getContents());
$parsedResponse = simplexml_load_string($content);
}

return $parsedResponse;
}
}

0 comments on commit e534aa3

Please sign in to comment.