From 506f888f52668e291364f4b7033fa199bb307b0a Mon Sep 17 00:00:00 2001 From: Vladimir Savenkov Date: Mon, 19 Nov 2012 14:33:22 +0600 Subject: [PATCH] Compatability with new JMS\PropertyMetadata interface JMS\SerializerBundle\Metadata\PropertyMetadata interface for type has changed. Here is a quickfix for it. Two versions of JMS Serializer support --- Serializer/JMSSerializerConverter.php | 60 ++++++++++++++++++++------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/Serializer/JMSSerializerConverter.php b/Serializer/JMSSerializerConverter.php index 1f85aac..2fd2650 100644 --- a/Serializer/JMSSerializerConverter.php +++ b/Serializer/JMSSerializerConverter.php @@ -70,28 +70,58 @@ public function __construct(MetadataFactoryInterface $factory) private function getType($type, $recursive = false) { - if (isset($this->typeMap[$type])) { - return $this->typeMap[$type]; - } else if (strpos($type, "array<") === 0) { - if ( ! $recursive) { - return 'collection'; + if (is_array($type)){ //JMSSerializer version > 0.9 + + if (isset($this->typeMap[$type['name']])) { + return $this->typeMap[$type['name']]; + } else if ($type['name'] === 'ArrayCollection' || $type['name'] === 'array' && !empty($type['params'])) { + if (!$recursive) { + return 'collection'; + } + + if (count($type['params'] === 1)) { + $listType = $type['params'][0]; + } else { + $keyType = $type['params'][0]; + $listType = $type['params'][1]; + } + + return $this->getType($listType); + } else if (class_exists($type['name'])) { + + $parts = explode("\\", $type['name']); + return "new ".end($parts)."Type()"; } - if (false === $pos = strpos($type, ',', 6)) { - $listType = substr($type, 6, -1); - } else { - $keyType = trim(substr($type, 6, $pos - 6)); - $listType = trim(substr($type, $pos+1, -1)); + return "text"; + + }else{ // JMSSerializer version <= 0.9 + + if (isset($this->typeMap[$type])) { + return $this->typeMap[$type]; + } else if (strpos($type, "array<") === 0) { + if ( ! $recursive) { + return 'collection'; + } + + if (false === $pos = strpos($type, ',', 6)) { + $listType = substr($type, 6, -1); + } else { + $keyType = trim(substr($type, 6, $pos - 6)); + $listType = trim(substr($type, $pos+1, -1)); + } + + return $this->getType($listType); + } else if (class_exists($type)) { + + $parts = explode("\\", $type); + return "new " . end($parts) . "Type()"; } - return $this->getType($listType); - } else if (class_exists($type)) { + return "text"; - $parts = explode("\\", $type); - return "new " . end($parts) . "Type()"; } - return "text"; } public function generateFormPhpCode($className)