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, $this->getChildAttributeValue($element, 'content', 'url', static::MRSS_NAMESPACE), $node); |
69
|
1 |
|
break; |
70
|
4 |
|
case 'media:content': |
71
|
|
|
$this->initMedia($media, $element); |
72
|
|
|
$this->setUrl($media, $this->getAttributeValue($element, "url"), $node); |
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, $this->getAttributeValue($element, $this->getUrlAttributeName()), $node); |
79
|
4 |
|
break; |
80
|
|
|
} |
81
|
5 |
|
$node->addMedia($media); |
82
|
|
|
} |
83
|
5 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param MediaInterface $media |
87
|
|
|
* @param string $url |
88
|
|
|
* @param NodeInterface $node |
89
|
|
|
*/ |
90
|
5 |
|
protected function setUrl(MediaInterface $media, string $url, NodeInterface $node): void |
91
|
|
|
{ |
92
|
5 |
|
$media->setUrl($this->urlGenerator->getAbsolutePath($url, $node->getHost())); |
93
|
5 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param \DomDocument $document |
97
|
|
|
* @param MediaInterface $media |
98
|
|
|
* @return \DomElement |
99
|
|
|
*/ |
100
|
1 |
|
public function createMediaElement(\DomDocument $document, MediaInterface $media) : \DOMElement |
101
|
|
|
{ |
102
|
1 |
|
$element = $document->createElement($this->getNodeName()); |
103
|
1 |
|
$element->setAttribute($this->getUrlAttributeName(), $media->getUrl()); |
104
|
1 |
|
$element->setAttribute('type', $media->getType() ?? ''); |
105
|
1 |
|
$element->setAttribute('length', $media->getLength() ?? ''); |
106
|
|
|
|
107
|
1 |
|
return $element; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param \MediaInterface $media |
112
|
|
|
* @param \DomElement $element |
113
|
|
|
*/ |
114
|
1 |
|
protected function initMedia(MediaInterface $media, \DOMElement $element): void |
115
|
|
|
{ |
116
|
1 |
|
$media->setTitle($this->getChildValue($element, 'title', static::MRSS_NAMESPACE)); |
117
|
1 |
|
$media->setDescription($this->getChildValue($element, 'description', static::MRSS_NAMESPACE)); |
118
|
1 |
|
$media->setThumbnail($this->getChildAttributeValue($element, 'thumbnail', 'url', static::MRSS_NAMESPACE)); |
119
|
1 |
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @inheritDoc |
123
|
|
|
*/ |
124
|
5 |
|
protected function hasValue(NodeInterface $node) : bool |
125
|
|
|
{ |
126
|
5 |
|
return $node instanceof ItemInterface && !! $node->getMedias(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @inheritDoc |
131
|
|
|
*/ |
132
|
5 |
|
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
133
|
|
|
{ |
134
|
5 |
|
foreach ($node->getMedias() as $media) { |
|
|
|
|
135
|
1 |
|
$rootElement->appendChild($this->createMediaElement($document, $media)); |
136
|
|
|
} |
137
|
5 |
|
} |
138
|
|
|
} |
139
|
|
|
|
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: