is_scalar(null) !== true

This commit is contained in:
Lukas Kahwe Smith 2011-01-30 10:10:27 +01:00 committed by Fabien Potencier
parent 2889e91c27
commit 46d900682f
4 changed files with 14 additions and 9 deletions

View File

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

View File

@ -37,12 +37,12 @@ class XmlEncoder extends AbstractEncoder
$this->dom = new \DOMDocument(); $this->dom = new \DOMDocument();
$this->format = $format; $this->format = $format;
if (is_scalar($data)) { if ($this->serializer->isStructuredType($data)) {
$this->appendNode($this->dom, $data, 'response');
} else {
$root = $this->dom->createElement('response'); $root = $this->dom->createElement('response');
$this->dom->appendChild($root); $this->dom->appendChild($root);
$this->buildXml($root, $data); $this->buildXml($root, $data);
} else {
$this->appendNode($this->dom, $data, 'response');
} }
return $this->dom->saveXML(); return $this->dom->saveXML();
} }
@ -115,7 +115,7 @@ class XmlEncoder extends AbstractEncoder
} }
if (is_object($data)) { if (is_object($data)) {
$data = $this->serializer->normalizeObject($data, $this->format); $data = $this->serializer->normalizeObject($data, $this->format);
if (is_scalar($data)) { if (!$this->serializer->isStructuredType($data)) {
// top level data object is normalized into a scalar // top level data object is normalized into a scalar
if (!$parentNode->parentNode->parentNode) { if (!$parentNode->parentNode->parentNode) {
$root = $parentNode->parentNode; $root = $parentNode->parentNode;
@ -250,4 +250,4 @@ class XmlEncoder extends AbstractEncoder
{ {
return $name && strpos($name, ' ') === false && preg_match('|^\w+$|', $name); return $name && strpos($name, ' ') === false && preg_match('|^\w+$|', $name);
} }
} }

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 (!is_scalar($attributeValue)) { if ($this->serializer->isStructuredType($attributeValue)) {
$attributeValue = $this->serializer->normalize($attributeValue, $format); $attributeValue = $this->serializer->normalize($attributeValue, $format);
} }
@ -142,4 +142,4 @@ class GetSetMethodNormalizer extends AbstractNormalizer
0 === $method->getNumberOfRequiredParameters() 0 === $method->getNumberOfRequiredParameters()
); );
} }
} }

View File

@ -32,6 +32,11 @@ class Serializer implements SerializerInterface
protected $encoders = array(); protected $encoders = array();
protected $normalizerCache = array(); protected $normalizerCache = array();
public function isStructuredType($val)
{
return null !== $val && !is_scalar($val);
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -90,7 +95,7 @@ class Serializer implements SerializerInterface
{ {
if (is_array($data)) { if (is_array($data)) {
foreach ($data as $key => $val) { foreach ($data as $key => $val) {
$data[$key] = is_scalar($val) ? $val : $this->normalize($val, $format); $data[$key] = $this->isStructuredType($val) ? $this->normalize($val, $format) : $val;
} }
return $data; return $data;
} }