[Serializer] Inlined back the logic from isStructuredType and removed the method

This commit is contained in:
Jordi Boggiano 2011-05-08 18:14:44 +02:00
parent 3ecc9602e4
commit 4104c7b073
4 changed files with 12 additions and 24 deletions

View File

@ -25,9 +25,6 @@ class JsonEncoder extends AbstractEncoder implements DecoderInterface
*/ */
public function encode($data, $format) public function encode($data, $format)
{ {
if ($this->serializer->isStructuredType($data)) {
$data = $this->serializer->normalize($data, $format);
}
return json_encode($data); return json_encode($data);
} }

View File

@ -38,7 +38,7 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface
$this->dom = new \DOMDocument(); $this->dom = new \DOMDocument();
$this->format = $format; $this->format = $format;
if ($this->serializer->isStructuredType($data)) { if (null !== $data && !is_scalar($data)) {
$root = $this->dom->createElement($this->rootNodeName); $root = $this->dom->createElement($this->rootNodeName);
$this->dom->appendChild($root); $this->dom->appendChild($root);
$this->buildXml($root, $data); $this->buildXml($root, $data);
@ -248,16 +248,16 @@ class XmlEncoder extends AbstractEncoder implements DecoderInterface
} }
if (is_object($data)) { if (is_object($data)) {
$data = $this->serializer->normalizeObject($data, $this->format); $data = $this->serializer->normalizeObject($data, $this->format);
if (!$this->serializer->isStructuredType($data)) { if (null !== $data && !is_scalar($data)) {
// top level data object is normalized into a scalar return $this->buildXml($parentNode, $data);
if (!$parentNode->parentNode->parentNode) {
$root = $parentNode->parentNode;
$root->removeChild($parentNode);
return $this->appendNode($root, $data, $this->rootNodeName);
}
return $this->appendNode($parentNode, $data, 'data');
} }
return $this->buildXml($parentNode, $data); // top level data object was normalized into a scalar
if (!$parentNode->parentNode->parentNode) {
$root = $parentNode->parentNode;
$root->removeChild($parentNode);
return $this->appendNode($root, $data, $this->rootNodeName);
}
return $this->appendNode($parentNode, $data, 'data');
} }
throw new \UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true)); throw new \UnexpectedValueException('An unexpected value could not be serialized: '.var_export($data, true));
} }

View File

@ -52,7 +52,7 @@ class GetSetMethodNormalizer extends AbstractNormalizer
if (null === $propertyMap || isset($propertyMap[$attributeName])) { if (null === $propertyMap || isset($propertyMap[$attributeName])) {
$attributeValue = $method->invoke($object); $attributeValue = $method->invoke($object);
if ($this->serializer->isStructuredType($attributeValue)) { if (null !== $attributeValue && !is_scalar($attributeValue)) {
$attributeValue = $this->serializer->normalize($attributeValue, $format); $attributeValue = $this->serializer->normalize($attributeValue, $format);
} }

View File

@ -34,15 +34,6 @@ class Serializer implements SerializerInterface
protected $normalizerCache = array(); protected $normalizerCache = array();
protected $denormalizerCache = array(); protected $denormalizerCache = array();
/**
* @param mixed $value value to test
* @return Boolean whether the type is a structured type (array + objects)
*/
public function isStructuredType($value)
{
return null !== $value && !is_scalar($value);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -104,7 +95,7 @@ class Serializer implements SerializerInterface
*/ */
public function normalize($data, $format = null) public function normalize($data, $format = null)
{ {
if (!$this->isStructuredType($data)) { if (null === $value || is_scalar($value)) {
return $data; return $data;
} }
if ($data instanceof Traversable) { if ($data instanceof Traversable) {