diff --git a/src/SitemapParser.php b/src/SitemapParser.php index 4b2b9ff..5ca1906 100644 --- a/src/SitemapParser.php +++ b/src/SitemapParser.php @@ -389,16 +389,26 @@ protected function isSitemapURL($url) protected function objectToArray($object) { if (is_object($object) || is_array($object)) { - $ret = (array)$object; + if (is_object($object) && $object instanceof SimpleXMLElement and count($object->getNamespaces()) != 0 ) { + if (count($object->attributes()) != 0) { + $ret = []; + foreach($object->attributes() as $attribute) { + $ret[$attribute->getName()] = $attribute->__toString(); + } + } else { + $ret = (array)$object; + } + } else { + $ret = (array)$object; + } foreach($ret as &$item) { $item = $this->objectToArray($item); } return $ret; - } else { - return $object; } + return $object; } /** @@ -415,20 +425,15 @@ protected function parseJson($type, \SimpleXMLElement $json) } $nameSpaces = $json->getDocNamespaces(); - + $notEmptyNamespaceNames = array_filter(array_keys($nameSpaces)); if (!empty($nameSpaces)) { foreach ($json->$type as $node) { - $tags = ["namespaces" => []]; + $tags = ["namespaces" => array_combine($notEmptyNamespaceNames, array_fill(0,count($notEmptyNamespaceNames),[]))]; foreach ($nameSpaces as $nameSpace => $value) { if ($nameSpace != "") { - $tags["namespaces"] = array_merge( - $tags["namespaces"], - [ - $nameSpace => $this->objectToArray( - $node->children($nameSpace, true) - ) - ] - ); + foreach($node->children($nameSpace, true) as $child) { + $tags["namespaces"][$nameSpace][] = [$child->getName() => $this->objectToArray($child)]; + } } else { $tags = array_merge($tags, (array)$node); } diff --git a/tests/URLSetTest.php b/tests/URLSetTest.php index 272ddc3..d85be5a 100644 --- a/tests/URLSetTest.php +++ b/tests/URLSetTest.php @@ -16,10 +16,10 @@ class URLSetTest extends TestCase public function testURLSet($url, $body, $result) { $parser = new SitemapParser('SitemapParser'); - $this->assertInstanceOf('vipnytt\SitemapParser', $parser); + self::assertInstanceOf('vipnytt\SitemapParser', $parser); $parser->parse($url, $body); - $this->assertEquals([], $parser->getSitemaps()); - $this->assertEquals($result, $parser->getURLs()); + self::assertEquals([], $parser->getSitemaps()); + self::assertEquals($result, $parser->getURLs()); } /** @@ -34,12 +34,14 @@ public function generateDataForTest() << - + http://www.example.com/ 2005-01-01 monthly 0.8 + + http://www.example.com/catalog?item=12&desc=vacation_hawaii @@ -62,41 +64,66 @@ public function generateDataForTest() XMLSITEMAP , - $result = [ + [ 'http://www.example.com/' => [ 'loc' => 'http://www.example.com/', 'lastmod' => '2005-01-01', 'changefreq' => 'monthly', 'priority' => '0.8', - 'namespaces'=> [], + 'namespaces'=> [ + 'xhtml' => [ + [ + 'link' => [ + 'rel' => 'alternate', + 'hreflang' => 'en-US', + 'href' => 'http://www.example.com/?land=en&country=US' + ] + ], + [ + 'link' => [ + 'rel' => 'alternate', + 'hreflang' => 'en-GB', + 'href' => 'http://www.example.com/?land=en&country=GB' + ] + ], + ], + ], ], 'http://www.example.com/catalog?item=12&desc=vacation_hawaii' => [ 'loc' => 'http://www.example.com/catalog?item=12&desc=vacation_hawaii', 'changefreq' => 'weekly', 'lastmod' => null, 'priority' => null, - 'namespaces'=> [], + 'namespaces'=> [ + 'xhtml' => [], + ], ], 'http://www.example.com/catalog?item=73&desc=vacation_new_zealand' => [ 'loc' => 'http://www.example.com/catalog?item=73&desc=vacation_new_zealand', 'lastmod' => '2004-12-23', 'changefreq' => 'weekly', 'priority' => null, - 'namespaces'=> [], + 'namespaces'=> [ + 'xhtml' => [], + ], ], 'http://www.example.com/catalog?item=74&desc=vacation_newfoundland' => [ 'loc' => 'http://www.example.com/catalog?item=74&desc=vacation_newfoundland', 'lastmod' => '2004-12-23T18:00:15+00:00', 'priority' => '0.3', 'changefreq' => null, - 'namespaces'=> [], + 'namespaces'=> [ + 'xhtml' => [], + ], ], 'http://www.example.com/catalog?item=83&desc=vacation_usa' => [ 'loc' => 'http://www.example.com/catalog?item=83&desc=vacation_usa', 'lastmod' => '2004-11-23', 'changefreq' => null, 'priority' => null, - 'namespaces'=> [], + 'namespaces'=> [ + 'xhtml' => [], + ], ], ] ]