1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thepixeldeveloper\Sitemap; |
4
|
|
|
|
5
|
|
|
use XMLWriter; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Urlset |
9
|
|
|
* |
10
|
|
|
* @package Thepixeldeveloper\Sitemap |
11
|
|
|
*/ |
12
|
|
|
class Urlset implements OutputInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Array of URL objects. |
16
|
|
|
* |
17
|
|
|
* @var OutputInterface[] |
18
|
|
|
*/ |
19
|
|
|
protected $urls = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Sub-elements that have been appended to the collection attributes. |
23
|
|
|
* |
24
|
|
|
* @var AppendAttributeInterface[] |
25
|
|
|
*/ |
26
|
|
|
protected $appendedSubelements = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Add a new URL object. |
30
|
|
|
* |
31
|
|
|
* @param OutputInterface $url |
32
|
|
|
* |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function addUrl(OutputInterface $url) |
36
|
|
|
{ |
37
|
|
|
$this->urls[] = $url; |
38
|
|
|
|
39
|
|
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function generateXML(XMLWriter $XMLWriter) |
46
|
|
|
{ |
47
|
|
|
$XMLWriter->startElement('urlset'); |
48
|
|
|
|
49
|
|
|
$XMLWriter->writeAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); |
50
|
|
|
|
51
|
|
|
$XMLWriter->writeAttribute('xsi:schemaLocation', |
52
|
|
|
'http://www.sitemaps.org/schemas/sitemap/0.9 ' . |
53
|
|
|
'http://www.sitemaps.org/schemas/sitemap/0.9/siteindex.xsd'); |
54
|
|
|
|
55
|
|
|
$XMLWriter->writeAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); |
56
|
|
|
|
57
|
|
|
foreach ($this->getUrls() as $url) { |
58
|
|
|
foreach ($url->getSubelementsThatAppend() as $subelement) { |
|
|
|
|
59
|
|
|
$this->appendSubelementAttribute($XMLWriter, $subelement); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
foreach ($this->getUrls() as $url) { |
64
|
|
|
$url->generateXML($XMLWriter); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$XMLWriter->endElement(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get array of URL objects. |
72
|
|
|
* |
73
|
|
|
* @return OutputInterface[] |
74
|
|
|
*/ |
75
|
|
|
public function getUrls() |
76
|
|
|
{ |
77
|
|
|
return $this->urls; |
78
|
|
|
} |
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
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: