Skip to content

Commit

Permalink
Improve handling of additional namespaced objects (#23)
Browse files Browse the repository at this point in the history
Updated the `objectToArray` method to handle SimpleXMLElement objects in a fine-grained way. More specifically, the function now iterates through and properly unpacks the attributes of such objects - like alternative links or languages.

Co-authored-by: Grzegorz Drozd <[email protected]>
  • Loading branch information
GrzegorzDrozd and Grzegorz Drozd committed Nov 27, 2023
1 parent 481016d commit 59f8852
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 23 deletions.
31 changes: 18 additions & 13 deletions src/SitemapParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}
Expand Down
47 changes: 37 additions & 10 deletions tests/URLSetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

/**
Expand All @@ -34,12 +34,14 @@ public function generateDataForTest()

<<<XMLSITEMAP
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url>
<loc>http://www.example.com/</loc>
<lastmod>2005-01-01</lastmod>
<changefreq>monthly</changefreq>
<priority>0.8</priority>
<xhtml:link rel="alternate" hreflang="en-US" href="http://www.example.com/?land=en&amp;country=US" />
<xhtml:link rel="alternate" hreflang="en-GB" href="http://www.example.com/?land=en&amp;country=GB" />
</url>
<url>
<loc>http://www.example.com/catalog?item=12&amp;desc=vacation_hawaii</loc>
Expand All @@ -62,41 +64,66 @@ public function generateDataForTest()
</urlset>
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' => [],
],
],
]
]
Expand Down

0 comments on commit 59f8852

Please sign in to comment.