|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the feed-io package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Alexandre Debril <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace FeedIo\Rule; |
|
12
|
|
|
|
|
13
|
|
|
use FeedIo\Feed\Item; |
|
14
|
|
|
use FeedIo\Feed\Item\MediaInterface; |
|
15
|
|
|
use FeedIo\Feed\ItemInterface; |
|
16
|
|
|
use FeedIo\Feed\NodeInterface; |
|
17
|
|
|
use FeedIo\Parser\UrlGenerator; |
|
18
|
|
|
use FeedIo\RuleAbstract; |
|
19
|
|
|
|
|
20
|
|
|
class Media extends RuleAbstract |
|
21
|
|
|
{ |
|
22
|
|
|
const NODE_NAME = 'enclosure'; |
|
23
|
|
|
|
|
24
|
|
|
const MRSS_NAMESPACE = "http://search.yahoo.com/mrss/"; |
|
25
|
|
|
|
|
26
|
|
|
protected $urlAttributeName = 'url'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var UrlGenerator |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $urlGenerator; |
|
32
|
|
|
|
|
33
|
24 |
|
public function __construct(string $nodeName = null) |
|
34
|
|
|
{ |
|
35
|
24 |
|
$this->urlGenerator = new UrlGenerator(); |
|
36
|
24 |
|
parent::__construct($nodeName); |
|
37
|
24 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
5 |
|
public function getUrlAttributeName() : string |
|
43
|
|
|
{ |
|
44
|
5 |
|
return $this->urlAttributeName; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $name |
|
49
|
|
|
*/ |
|
50
|
12 |
|
public function setUrlAttributeName(string $name) : void |
|
51
|
|
|
{ |
|
52
|
12 |
|
$this->urlAttributeName = $name; |
|
53
|
12 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param NodeInterface $node |
|
57
|
|
|
* @param \DOMElement $element |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public function setProperty(NodeInterface $node, \DOMElement $element) : void |
|
60
|
|
|
{ |
|
61
|
5 |
|
if ($node instanceof ItemInterface) { |
|
62
|
5 |
|
$media = $node->newMedia(); |
|
63
|
5 |
|
$media->setNodeName($element->nodeName); |
|
64
|
|
|
|
|
65
|
5 |
|
switch ($element->nodeName) { |
|
66
|
5 |
|
case 'media:group': |
|
67
|
1 |
|
$this->initMedia($media, $element); |
|
68
|
1 |
|
$this->setUrl($media, $node, $this->getChildAttributeValue($element, 'content', 'url', static::MRSS_NAMESPACE)); |
|
69
|
1 |
|
break; |
|
70
|
4 |
|
case 'media:content': |
|
71
|
|
|
$this->initMedia($media, $element); |
|
72
|
|
|
$this->setUrl($media, $node, $this->getAttributeValue($element, "url")); |
|
73
|
|
|
break; |
|
74
|
|
|
default: |
|
75
|
|
|
$media |
|
76
|
4 |
|
->setType($this->getAttributeValue($element, 'type')) |
|
77
|
4 |
|
->setLength($this->getAttributeValue($element, 'length')); |
|
78
|
4 |
|
$this->setUrl($media, $node, $this->getAttributeValue($element, $this->getUrlAttributeName())); |
|
79
|
4 |
|
break; |
|
80
|
|
|
} |
|
81
|
5 |
|
$node->addMedia($media); |
|
82
|
|
|
} |
|
83
|
5 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param MediaInterface $media |
|
87
|
|
|
* @param NodeInterface $node |
|
88
|
|
|
* @param string|null $url |
|
89
|
|
|
*/ |
|
90
|
5 |
|
protected function setUrl(MediaInterface $media, NodeInterface $node, string $url = null): void |
|
91
|
|
|
{ |
|
92
|
5 |
|
if (! is_null($url)) { |
|
93
|
5 |
|
$media->setUrl( |
|
94
|
5 |
|
$this->urlGenerator->getAbsolutePath($url, $node->getHost()) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
5 |
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param \DomDocument $document |
|
101
|
|
|
* @param MediaInterface $media |
|
102
|
|
|
* @return \DomElement |
|
103
|
|
|
*/ |
|
104
|
1 |
|
public function createMediaElement(\DomDocument $document, MediaInterface $media) : \DOMElement |
|
105
|
|
|
{ |
|
106
|
1 |
|
$element = $document->createElement($this->getNodeName()); |
|
107
|
1 |
|
$element->setAttribute($this->getUrlAttributeName(), $media->getUrl()); |
|
108
|
1 |
|
$element->setAttribute('type', $media->getType() ?? ''); |
|
109
|
1 |
|
$element->setAttribute('length', $media->getLength() ?? ''); |
|
110
|
|
|
|
|
111
|
1 |
|
return $element; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param MediaInterface $media |
|
116
|
|
|
* @param \DomElement $element |
|
117
|
|
|
*/ |
|
118
|
1 |
|
protected function initMedia(MediaInterface $media, \DOMElement $element): void |
|
119
|
|
|
{ |
|
120
|
1 |
|
$media->setTitle($this->getChildValue($element, 'title', static::MRSS_NAMESPACE)); |
|
121
|
1 |
|
$media->setDescription($this->getChildValue($element, 'description', static::MRSS_NAMESPACE)); |
|
122
|
1 |
|
$media->setThumbnail($this->getChildAttributeValue($element, 'thumbnail', 'url', static::MRSS_NAMESPACE)); |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @inheritDoc |
|
127
|
|
|
*/ |
|
128
|
5 |
|
protected function hasValue(NodeInterface $node) : bool |
|
129
|
|
|
{ |
|
130
|
5 |
|
return $node instanceof ItemInterface && !! $node->getMedias(); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @inheritDoc |
|
135
|
|
|
*/ |
|
136
|
5 |
|
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
|
137
|
|
|
{ |
|
138
|
5 |
|
foreach ($node->getMedias() as $media) { |
|
|
|
|
|
|
139
|
1 |
|
$rootElement->appendChild($this->createMediaElement($document, $media)); |
|
140
|
|
|
} |
|
141
|
5 |
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
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: