Ejemplo n.º 1
0
 public function decodeInto(\DOMNode $node, Type $type, $variabile)
 {
     if ($type instanceof AbstractComplexType) {
         if ($type instanceof SimpleContent && $variabile instanceof \stdClass) {
             // hack per i complex type simple content
             $newVariabile = new \stdClass();
             $newVariabile->_ = $variabile;
             $variabile = $newVariabile;
         } elseif ($type instanceof SimpleContent && $type->getBase()) {
             $this->setBaseValue($variabile, $this->convertSimpleXmlPhp($node, $type->getBase()));
         }
         foreach ($type->getAttributes() as $attribute) {
             if ($attribute->getQualification() == "qualified") {
                 $attributeNode = $node->getAttributeNodeNS($attribute->getNs(), $attribute->getName());
             } else {
                 $attributeNode = $node->getAttributeNode($attribute->getName());
             }
             if ($attributeNode) {
                 self::setValueTo($variabile, $attribute->getName(), $this->convertSimpleXmlPhp($attributeNode, $attribute->getType()));
             } elseif ($attribute->isRequred()) {
                 throw new \Exception("Noon trovo l'attributo obbligatorio {$attribute} su {$type}");
             }
         }
         if ($type instanceof ComplexType) {
             $childs = array();
             foreach ($node->childNodes as $child) {
                 $childs[$child->namespaceURI][$child->localName][] = $child;
             }
             foreach ($type->getElements() as $element) {
                 $elementType = $element->getType();
                 $ns = $element->getQualification() == "qualified" ? $element->getNs() : "";
                 $nm = $element->getName();
                 if (isset($childs[$ns][$nm])) {
                     if ($element->getMax() > 1) {
                         foreach ($childs[$ns][$nm] as $elementNode) {
                             self::addValueTo($variabile, $this->convertSimpleXmlPhp($elementNode, $elementType));
                         }
                     } else {
                         $elementNode = array_shift($childs[$ns][$nm]);
                         $value = $this->convertSimpleXmlPhp($elementNode, $elementType);
                         if ($value instanceof \stdClass && is_object($variabile) && !$variabile instanceof \stdClass) {
                             throw new \Exception("Non trovo nessuna conversione valida per tag per il tipo {{$elementType->getNs()}}#{$elementType->getName()}");
                         }
                         self::setValueTo($variabile, $element->getName(), $value);
                     }
                 } elseif ($element->getMin() > 0) {
                     throw new \Exception("Non trovo nessun tag per l'elemento di tipo {{$ns}}#{$nm}");
                 }
             }
             if ($type->getBase()) {
                 $this->decodeInto($node, $type->getBase(), $variabile);
             }
         }
     } elseif ($type instanceof SimpleType) {
         if (is_object($variabile) && $type->getBase()) {
             $this->setBaseValue($variabile, $this->convertSimpleXmlPhp($node, $type->getBase()));
         }
     }
 }
Ejemplo n.º 2
0
 public function encode($variable, \DOMNode $node, Type $type)
 {
     if ($type instanceof AbstractComplexType) {
         foreach ($type->getAttributes() as $attribute) {
             $val = self::getValueFrom($variable, $attribute->getName());
             if ($val !== null) {
                 if ($attribute->getQualification() == "qualified") {
                     $attributeNode = $node->ownerDocument->ocreateAttributeNS($attribute->getNs(), $node->getPrefixFor($attribute->getNs()) . ":" . $attribute->getName());
                     $node->setAttributeNodeNs($attributeNode);
                 } else {
                     $attributeNode = $node->ownerDocument->createAttribute($attribute->getName());
                     $node->setAttributeNode($attributeNode);
                 }
                 $this->convertSimplePhpXml($val, $attributeNode, $attribute->getType());
             } elseif ($attribute->isRequred()) {
                 throw new \Exception("Type {$type}, attributo " . $attribute . " non deve essere vuoto");
             }
         }
         if ($type->getBase()) {
             $this->encode($variable, $node, $type->getBase());
         }
         if ($type instanceof ComplexType) {
             foreach ($type->getElements() as $element) {
                 $elementQualified = $element->getQualification() == "qualified";
                 $newType = $element->getType();
                 if ($element->getMax() > 1 && ($val = self::tryGetValueFrom($variable, $element->getName())) && (is_array($val) || $val instanceof \Traversable)) {
                     foreach ($val as $nval) {
                         if ($elementQualified) {
                             $newNode = $node->addPrefixedChild($element->getNs(), $element->getName());
                         } else {
                             $newNode = $node->addChild($element->getName());
                         }
                         $this->encode($nval, $newNode, $newType);
                     }
                 } elseif ($element->getMax() > 1 && (is_array($variable) || $variable instanceof \Traversable)) {
                     foreach ($variable as $nval) {
                         if ($elementQualified) {
                             $newNode = $node->addPrefixedChild($element->getNs(), $element->getName());
                         } else {
                             $newNode = $node->addChild($element->getName());
                         }
                         $this->encode($nval, $newNode, $newType);
                     }
                 } else {
                     $val = self::getValueFrom($variable, $element->getName());
                     if ($val !== null || $element->isNillable()) {
                         if ($elementQualified) {
                             $newNode = $node->addPrefixedChild($element->getNs(), $element->getName());
                         } else {
                             $newNode = $node->addChild($element->getName());
                         }
                         if ($val === null) {
                             $newNode->setAttributeNS(self::NS_XSI, $newNode->getPrefixFor(self::NS_XSI) . ":nil", "true");
                         } else {
                             $this->encode($val, $newNode, $newType);
                         }
                     } elseif ($element->getMin() > 0) {
                         throw new \Exception($element . " non deve essere vuoto");
                     }
                 }
             }
         }
     }
     if ($type instanceof SimpleType) {
         $this->convertSimplePhpXml($variable, $node, $type);
     }
 }