Exemplo n.º 1
0
 protected function doSerialize($var, Normalizers $handlers, array $state = array(), array $classes = array())
 {
     if (is_object($var)) {
         $class = get_class($var);
         $handler = $handlers->getHandler($class);
         if (null === $handler) {
             throw new \RuntimeException(sprintf('No serialization handler for class %s!', $class));
         }
         $hash = spl_object_hash($var);
         $classes[] = get_class($var);
         if (isset($state[$hash])) {
             throw new \RuntimeException('Nesting cycle: ' . implode(' -> ', $classes));
         }
         $state[$hash] = 1;
         return $this->doSerialize(call_user_func_array($handler, array($var)), $handlers, $state, $classes);
     }
     if (is_array($var)) {
         $return = array();
         foreach ($var as $key => $value) {
             $return[$key] = $this->doSerialize($value, $handlers, $state, $classes);
         }
         return $return;
     }
     return $var;
 }
Exemplo n.º 2
0
 private function serializeValue($var, Normalizers $normalizers, $doc, $parent, $key, array $state = array(), array $classes = array())
 {
     /** @var \DOMDocument|\DOMElement $doc */
     /** @var \DOMDocument|\DOMElement $parent */
     if (is_object($var)) {
         $handler = $normalizers->getHandler(get_class($var));
         $arr = $handler($var);
         $hash = spl_object_hash($var);
         $classes[] = get_class($var);
         if (isset($state[$hash])) {
             throw new \RuntimeException('Nesting cycle: ' . implode(' -> ', $classes));
         }
         $state[$hash] = 1;
         $this->doSerialize($arr, $normalizers, $doc, $parent, $normalizers->getRoot(get_class($var)), $state, $classes);
         return;
     }
     if (is_array($var)) {
         $item = $key ? $doc->createElement($key) : $parent;
         foreach ($var as $index => $value) {
             $this->doSerialize($value, $normalizers, $doc, $item, $index, $state, $classes);
         }
         !$key ?: $parent->appendChild($item);
         return;
     }
     $parent->appendChild($doc->createElement($key, $var));
 }