123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- /*
- * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- namespace JMS\SerializerBundle\Serializer;
- use JMS\SerializerBundle\Exception\XmlErrorException;
- use JMS\SerializerBundle\Exception\RuntimeException;
- use JMS\SerializerBundle\Metadata\PropertyMetadata;
- use JMS\SerializerBundle\Metadata\ClassMetadata;
- use JMS\SerializerBundle\Serializer\Construction\ObjectConstructorInterface;
- use JMS\SerializerBundle\Serializer\Naming\PropertyNamingStrategyInterface;
- class XmlDeserializationVisitor extends AbstractDeserializationVisitor
- {
- private $objectConstructor;
- private $objectStack;
- private $metadataStack;
- private $currentObject;
- private $currentMetadata;
- private $result;
- private $navigator;
- private $disableExternalEntities;
- public function __construct(PropertyNamingStrategyInterface $namingStrategy, array $customHandlers, ObjectConstructorInterface $objectConstructor, $disableExternalEntities = true)
- {
- parent::__construct($namingStrategy, $customHandlers);
- $this->objectConstructor = $objectConstructor;
- $this->disableExternalEntities = $disableExternalEntities;
- }
- public function setNavigator(GraphNavigator $navigator)
- {
- $this->navigator = $navigator;
- $this->objectStack = new \SplStack;
- $this->metadataStack = new \SplStack;
- $this->result = null;
- }
- public function getNavigator()
- {
- return $this->navigator;
- }
- public function prepare($data)
- {
- $previous = libxml_use_internal_errors(true);
- $previousEntityLoaderState = libxml_disable_entity_loader($this->disableExternalEntities);
- $doc = simplexml_load_string($data);
- libxml_use_internal_errors($previous);
- libxml_disable_entity_loader($previousEntityLoaderState);
- if (false === $doc) {
- throw new XmlErrorException(libxml_get_last_error());
- }
- return $doc;
- }
- public function visitString($data, $type)
- {
- $data = (string) $data;
- if (null === $this->result) {
- $this->result = $data;
- }
- return $data;
- }
- public function visitBoolean($data, $type)
- {
- $data = (string) $data;
- if ('true' === $data) {
- $data = true;
- } else if ('false' === $data) {
- $data = false;
- } else {
- throw new RuntimeException(sprintf('Could not convert data to boolean. Expected "true", or "false", but got %s.', json_encode($data)));
- }
- if (null === $this->result) {
- $this->result = $data;
- }
- return $data;
- }
- public function visitInteger($data, $type)
- {
- $data = (integer) $data;
- if (null === $this->result) {
- $this->result = $data;
- }
- return $data;
- }
- public function visitDouble($data, $type)
- {
- $data = (double) $data;
- if (null === $this->result) {
- $this->result = $data;
- }
- return $data;
- }
- public function visitArray($data, $type)
- {
- $entryName = null !== $this->currentMetadata && $this->currentMetadata->xmlEntryName ? $this->currentMetadata->xmlEntryName : 'entry';
- if (!isset($data->$entryName)) {
- if (null === $this->result) {
- return $this->result = array();
- }
- return array();
- }
- if ('array' === $type) {
- throw new RuntimeException(sprintf('You must specify either a list type, or a key and entry type for type array.'));
- }
- if (false === $pos = strpos($type, ',', 6)) {
- $listType = substr($type, 6, -1);
- $result = array();
- if (null === $this->result) {
- $this->result = &$result;
- }
- foreach ($data->$entryName as $v) {
- $result[] = $this->navigator->accept($v, $listType, $this);
- }
- return $result;
- }
- if (null === $this->currentMetadata) {
- throw new RuntimeException('Maps are not supported on top-level without metadata.');
- }
- $keyType = trim(substr($type, 6, $pos - 6));
- $entryType = trim(substr($type, $pos+1, -1));
- $result = array();
- if (null === $this->result) {
- $this->result = &$result;
- }
- foreach ($data->$entryName as $v) {
- if (!isset($v[$this->currentMetadata->xmlKeyAttribute])) {
- throw new RuntimeException(sprintf('The key attribute "%s" must be set for each entry of the map.', $this->currentMetadata->xmlKeyAttribute));
- }
- $k = $this->navigator->accept($v[$this->currentMetadata->xmlKeyAttribute], $keyType, $this);
- $result[$k] = $this->navigator->accept($v, $entryType, $this);
- }
- return $result;
- }
- public function visitTraversable($data, $type)
- {
- throw new RuntimeException('Traversable is not supported for Deserialization.');
- }
- public function startVisitingObject(ClassMetadata $metadata, $data, $type)
- {
- $this->setCurrentObject($this->objectConstructor->construct($this, $metadata, $data, $type));
- if (null === $this->result) {
- $this->result = $this->currentObject;
- }
- }
- public function visitProperty(PropertyMetadata $metadata, $data)
- {
- $name = $this->namingStrategy->translateName($metadata);
- if (!$metadata->type) {
- throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->reflection->getDeclaringClass()->getName(), $metadata->name));
- }
- if ($metadata->xmlAttribute) {
- if (isset($data[$name])) {
- $v = $this->navigator->accept($data[$name], $metadata->type, $this);
- $metadata->reflection->setValue($this->currentObject, $v);
- }
- return;
- }
- if ($metadata->xmlValue) {
- $v = $this->navigator->accept($data, $metadata->type, $this);
- $metadata->reflection->setValue($this->currentObject, $v);
- return;
- }
- if ($metadata->xmlCollection) {
- $enclosingElem = $data;
- if (!$metadata->xmlCollectionInline && isset($data->$name)) {
- $enclosingElem = $data->$name;
- }
- $this->setCurrentMetadata($metadata);
- $v = $this->navigator->accept($enclosingElem, $metadata->type, $this);
- $this->revertCurrentMetadata();
- $metadata->reflection->setValue($this->currentObject, $v);
- return;
- }
- if (!isset($data->$name)) {
- return;
- }
- $v = $this->navigator->accept($data->$name, $metadata->type, $this);
- if (null === $metadata->setter) {
- $metadata->reflection->setValue($this->currentObject, $v);
- return;
- }
- $this->currentObject->{$metadata->setter}($v);
- }
- public function endVisitingObject(ClassMetadata $metadata, $data, $type)
- {
- $rs = $this->currentObject;
- $this->revertCurrentObject();
- return $rs;
- }
- public function visitPropertyUsingCustomHandler(PropertyMetadata $metadata, $object)
- {
- // TODO
- return false;
- }
- public function setCurrentObject($object)
- {
- $this->objectStack->push($this->currentObject);
- $this->currentObject = $object;
- }
- public function getCurrentObject()
- {
- return $this->currentObject;
- }
- public function revertCurrentObject()
- {
- return $this->currentObject = $this->objectStack->pop();
- }
- public function setCurrentMetadata(PropertyMetadata $metadata)
- {
- $this->metadataStack->push($this->currentMetadata);
- $this->currentMetadata = $metadata;
- }
- public function getCurrentMetadata()
- {
- return $this->currentMetadata;
- }
- public function revertCurrentMetadata()
- {
- return $this->currentMetadata = $this->metadataStack->pop();
- }
- public function getResult()
- {
- return $this->result;
- }
- }
|