public function testGetAssocArrayKeyAttributeNotMatched()
 {
     $xpath = '/root/assoc[@attr="value"]/four';
     $this->nodePathMatcher->expects($this->at(0))->method('match')->with('/root/assoc/one', $xpath)->will($this->returnValue(false));
     $this->nodePathMatcher->expects($this->at(1))->method('match')->with('/root/assoc/two', $xpath)->will($this->returnValue(false));
     $this->nodePathMatcher->expects($this->at(2))->method('match')->with('/root/assoc/three', $xpath)->will($this->returnValue(false));
     $this->assertNull($this->object->getAssocArrayKeyAttribute($xpath));
 }
Example #2
0
 /**
  * Convert dom node tree to array in general case or to string in a case of a text node
  *
  * Example:
  * <node attr="val">
  *     <subnode>val2<subnode>
  * </node>
  *
  * is converted to
  *
  * array(
  *     'node' => array(
  *         'attr' => 'wal',
  *         'subnode' => 'val2'
  *     )
  * )
  *
  * @param \DOMNode $source
  * @param string $basePath
  * @return string|array
  * @throws \UnexpectedValueException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 public function convert(\DOMNode $source, $basePath = '')
 {
     $value = array();
     /** @var \DOMNode $node */
     foreach ($source->childNodes as $node) {
         if ($node->nodeType == XML_ELEMENT_NODE) {
             $nodeName = $node->nodeName;
             $nodePath = $basePath . '/' . $nodeName;
             $arrayKeyAttribute = $this->arrayNodeConfig->getAssocArrayKeyAttribute($nodePath);
             $isNumericArrayNode = $this->arrayNodeConfig->isNumericArray($nodePath);
             $isArrayNode = $isNumericArrayNode || $arrayKeyAttribute;
             if (isset($value[$nodeName]) && !$isArrayNode) {
                 throw new \UnexpectedValueException("Node path '{$nodePath}' is not unique, but it has not been marked as array.");
             }
             $nodeData = $this->convert($node, $nodePath);
             if ($isArrayNode) {
                 if ($isNumericArrayNode) {
                     $value[$nodeName][] = $nodeData;
                 } else {
                     if (isset($nodeData[$arrayKeyAttribute])) {
                         $arrayKeyValue = $nodeData[$arrayKeyAttribute];
                         $value[$nodeName][$arrayKeyValue] = $nodeData;
                     } else {
                         throw new \UnexpectedValueException("Array is expected to contain value for key '{$arrayKeyAttribute}'.");
                     }
                 }
             } else {
                 $value[$nodeName] = $nodeData;
             }
         } elseif ($node->nodeType == XML_CDATA_SECTION_NODE || $node->nodeType == XML_TEXT_NODE && trim($node->nodeValue) != '') {
             $value = $node->nodeValue;
             break;
         }
     }
     $result = $this->getNodeAttributes($source);
     if (is_array($value)) {
         $result = array_merge($result, $value);
         if (!$result) {
             $result = '';
         }
     } else {
         if ($result) {
             $result['value'] = $value;
         } else {
             $result = $value;
         }
     }
     return $result;
 }