예제 #1
0
 public function deserialize(VisitorInterface $visitor, $data, $type, &$handled)
 {
     if (!class_exists($type) || !in_array('JMS\\SerializerBundle\\Serializer\\Handler\\DeserializationHandlerInterface', class_implements($type))) {
         return;
     }
     $metadata = $this->metadataFactory->getMetadataForClass($type);
     $visitor->startVisitingObject($metadata, $data, $type);
     $instance = $visitor->getResult();
     $instance->deserialize($visitor, $data, $type, $handled);
     return $instance;
 }
예제 #2
0
 public function accept($data, $type, VisitorInterface $visitor)
 {
     // determine type if not given
     if (null === $type) {
         if (null === $data) {
             return null;
         }
         $type = gettype($data);
         if ('object' === $type) {
             $type = get_class($data);
         }
     }
     if ('string' === $type) {
         return $visitor->visitString($data, $type);
     } else {
         if ('integer' === $type) {
             return $visitor->visitInteger($data, $type);
         } else {
             if ('boolean' === $type) {
                 return $visitor->visitBoolean($data, $type);
             } else {
                 if ('double' === $type) {
                     return $visitor->visitDouble($data, $type);
                 } else {
                     if ('array' === $type || 'a' === $type[0] && 0 === strpos($type, 'array<')) {
                         return $visitor->visitArray($data, $type);
                     } else {
                         if ('resource' === $type) {
                             $path = array();
                             foreach ($this->visiting as $obj) {
                                 $path[] = get_class($obj);
                             }
                             $msg = 'Resources are not supported in serialized data.';
                             if ($path) {
                                 $msg .= ' Path: ' . implode(' -> ', $path);
                             }
                             throw new \RuntimeException($msg);
                         } else {
                             if (self::DIRECTION_SERIALIZATION === $this->direction && null !== $data) {
                                 if ($this->visiting->contains($data)) {
                                     return null;
                                 }
                                 $this->visiting->attach($data);
                             }
                             // try custom handler
                             $handled = false;
                             $rs = $visitor->visitUsingCustomHandler($data, $type, $handled);
                             if ($handled) {
                                 if (self::DIRECTION_SERIALIZATION === $this->direction) {
                                     $this->visiting->detach($data);
                                 }
                                 return $rs;
                             }
                             $metadata = $this->metadataFactory->getMetadataForClass($type);
                             if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipClass($metadata)) {
                                 if (self::DIRECTION_SERIALIZATION === $this->direction) {
                                     $this->visiting->detach($data);
                                 }
                                 return null;
                             }
                             // pre-serialization callbacks
                             if (self::DIRECTION_SERIALIZATION === $this->direction) {
                                 foreach ($metadata->preSerializeMethods as $method) {
                                     $method->invoke($data);
                                 }
                             }
                             // check if traversable
                             if (self::DIRECTION_SERIALIZATION === $this->direction && $data instanceof \Traversable) {
                                 $rs = $visitor->visitTraversable($data, $type);
                                 $this->afterVisitingObject($metadata, $data, self::DIRECTION_SERIALIZATION === $this->direction);
                                 return $rs;
                             }
                             $visitor->startVisitingObject($metadata, $data, $type);
                             foreach ($metadata->propertyMetadata as $propertyMetadata) {
                                 if (null !== $this->exclusionStrategy && $this->exclusionStrategy->shouldSkipProperty($propertyMetadata)) {
                                     continue;
                                 }
                                 if (self::DIRECTION_DESERIALIZATION === $this->direction && $propertyMetadata->readOnly) {
                                     continue;
                                 }
                                 // try custom handler
                                 if (!$visitor->visitPropertyUsingCustomHandler($propertyMetadata, $data)) {
                                     $visitor->visitProperty($propertyMetadata, $data);
                                 }
                             }
                             $rs = $visitor->endVisitingObject($metadata, $data, $type);
                             $this->afterVisitingObject($metadata, self::DIRECTION_SERIALIZATION === $this->direction ? $data : $rs);
                             return $rs;
                         }
                     }
                 }
             }
         }
     }
 }