Skip to content
This repository was archived by the owner on Dec 20, 2025. It is now read-only.

Commit bfb14b6

Browse files
lushcThePixelDeveloper
authored andcommitted
Write any remaining attributes to the urlset element before writing sub-elements (#34)
* Write any remaining attributes to the urlset element before writing sub-elements. Fixes #33. * Fix tests
1 parent 5a1d8dd commit bfb14b6

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed

spec/UrlSpec.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
7-
use Thepixeldeveloper\Sitemap\Subelements\Image;
8-
use XMLWriter;
97

108
class UrlSpec extends ObjectBehavior
119
{
@@ -38,19 +36,4 @@ function it_should_have_a_priority()
3836
{
3937
$this->getPriority()->shouldReturn(null);
4038
}
41-
42-
function it_should_only_append_attributes_once_for_each_subelement_type(XMLWriter $xmlWriter, Image $image)
43-
{
44-
$xmlWriter->startElement('url')->shouldBeCalled();
45-
$xmlWriter->writeElement('loc', 'http://www.example.com/')->shouldBeCalled();
46-
$xmlWriter->endElement()->shouldBeCalled();
47-
48-
$this->addSubElement($image);
49-
$this->addSubElement($image);
50-
51-
$image->appendAttributeToCollectionXML($xmlWriter)->shouldBeCalled();
52-
$image->generateXML($xmlWriter)->shouldBeCalled();
53-
54-
$this->generateXML($xmlWriter);
55-
}
5639
}

spec/UrlsetSpec.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use PhpSpec\ObjectBehavior;
66
use Prophecy\Argument;
77
use Thepixeldeveloper\Sitemap\Url;
8+
use Thepixeldeveloper\Sitemap\Subelements\Image;
9+
use Thepixeldeveloper\Sitemap\Subelements\Video;
10+
use XMLWriter;
811

912
class UrlsetSpec extends ObjectBehavior
1013
{
@@ -24,4 +27,26 @@ function it_should_return_the_urls_added(Url $url)
2427

2528
$this->getUrls()->shouldReturn([$url]);
2629
}
30+
31+
function it_should_only_append_attributes_once_for_each_subelement_type(XMLWriter $xmlWriter, Url $url, Image $image, Video $video)
32+
{
33+
$xmlWriter->startElement('urlset')->shouldBeCalled();
34+
$xmlWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance')->shouldBeCalled();
35+
$xmlWriter->writeAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 ' . 'http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd')->shouldBeCalled();
36+
$xmlWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9')->shouldBeCalled();
37+
38+
$url->getSubelementsThatAppend()->willReturn([$image, $video]);
39+
$this->appendSubelementAttribute($xmlWriter, $image)->shouldReturn(true);
40+
$this->appendSubelementAttribute($xmlWriter, $image)->shouldReturn(false);
41+
$this->appendSubelementAttribute($xmlWriter, $video)->shouldReturn(true);
42+
$this->appendSubelementAttribute($xmlWriter, $video)->shouldReturn(false);
43+
44+
$image->appendAttributeToCollectionXML($xmlWriter)->shouldBeCalled();
45+
$video->appendAttributeToCollectionXML($xmlWriter)->shouldBeCalled();
46+
$url->generateXML($xmlWriter)->shouldBeCalled();
47+
$xmlWriter->endElement()->shouldBeCalled();
48+
49+
$this->addUrl($url);
50+
$this->generateXML($xmlWriter);
51+
}
2752
}

src/Url.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,6 @@ public function __construct($loc)
6868
*/
6969
public function generateXML(XMLWriter $XMLWriter)
7070
{
71-
foreach ($this->getSubelementsThatAppend() as $subelement) {
72-
$subelement->appendAttributeToCollectionXML($XMLWriter);
73-
}
74-
7571
$XMLWriter->startElement('url');
7672
$XMLWriter->writeElement('loc', $this->getLoc());
7773

src/Urlset.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,13 @@ class Urlset implements OutputInterface
1818
*/
1919
protected $urls = [];
2020

21+
/**
22+
* Sub-elements that have been appended to the collection attributes.
23+
*
24+
* @var AppendAttributeInterface[]
25+
*/
26+
protected $appendedSubelements = [];
27+
2128
/**
2229
* Add a new URL object.
2330
*
@@ -47,6 +54,12 @@ public function generateXML(XMLWriter $XMLWriter)
4754

4855
$XMLWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9');
4956

57+
foreach ($this->getUrls() as $url) {
58+
foreach ($url->getSubelementsThatAppend() as $subelement) {
59+
$this->appendSubelementAttribute($XMLWriter, $subelement);
60+
}
61+
}
62+
5063
foreach ($this->getUrls() as $url) {
5164
$url->generateXML($XMLWriter);
5265
}
@@ -63,4 +76,24 @@ public function getUrls()
6376
{
6477
return $this->urls;
6578
}
79+
80+
/**
81+
* Appends the sub-element to the collection attributes if it has yet to be visited.
82+
*
83+
* @param XMLWriter $XMLWriter
84+
* @param OutputInterface $subelement
85+
*
86+
* @return boolean
87+
*/
88+
public function appendSubelementAttribute(XMLWriter $XMLWriter, OutputInterface $subelement)
89+
{
90+
if (array_key_exists(get_class($subelement), $this->appendedSubelements)) {
91+
return false;
92+
}
93+
94+
$subelement->appendAttributeToCollectionXML($XMLWriter);
95+
$this->appendedSubelements[get_class($subelement)] = $subelement;
96+
97+
return true;
98+
}
6699
}

0 commit comments

Comments
 (0)