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\RuleAbstract; |
18
|
|
|
|
19
|
|
|
class Media extends RuleAbstract |
20
|
|
|
{ |
21
|
|
|
const NODE_NAME = 'enclosure'; |
22
|
|
|
|
23
|
|
|
const MRSS_NAMESPACE = "http://search.yahoo.com/mrss/"; |
24
|
|
|
|
25
|
|
|
protected $urlAttributeName = 'url'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
5 |
|
public function getUrlAttributeName() : string |
31
|
|
|
{ |
32
|
5 |
|
return $this->urlAttributeName; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $name |
37
|
|
|
*/ |
38
|
12 |
|
public function setUrlAttributeName(string $name) : void |
39
|
|
|
{ |
40
|
12 |
|
$this->urlAttributeName = $name; |
41
|
12 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param NodeInterface $node |
45
|
|
|
* @param \DOMElement $element |
46
|
|
|
*/ |
47
|
5 |
|
public function setProperty(NodeInterface $node, \DOMElement $element) : void |
48
|
|
|
{ |
49
|
5 |
|
if ($node instanceof ItemInterface) { |
50
|
5 |
|
$media = $node->newMedia(); |
51
|
5 |
|
$media->setNodeName($element->nodeName); |
52
|
|
|
|
53
|
5 |
|
switch ($element->nodeName) { |
54
|
5 |
|
case 'media:group': |
55
|
1 |
|
$this->initMedia($media, $element); |
56
|
1 |
|
$media->setUrl($this->getChildAttributeValue($element, 'content', 'url', static::MRSS_NAMESPACE)); |
57
|
1 |
|
break; |
58
|
4 |
|
case 'media:content': |
59
|
|
|
$this->initMedia($media, $element); |
60
|
|
|
$media->setUrl($this->getAttributeValue($element, "url")); |
61
|
|
|
break; |
62
|
|
|
default: |
63
|
|
|
$media |
64
|
4 |
|
->setType($this->getAttributeValue($element, 'type')) |
65
|
4 |
|
->setUrl($this->getAttributeValue($element, $this->getUrlAttributeName())) |
66
|
4 |
|
->setLength($this->getAttributeValue($element, 'length')); |
67
|
4 |
|
break; |
68
|
|
|
} |
69
|
5 |
|
$node->addMedia($media); |
70
|
|
|
} |
71
|
5 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param \DomDocument $document |
75
|
|
|
* @param MediaInterface $media |
76
|
|
|
* @return \DomElement |
77
|
|
|
*/ |
78
|
1 |
|
public function createMediaElement(\DomDocument $document, MediaInterface $media) : \DOMElement |
79
|
|
|
{ |
80
|
1 |
|
$element = $document->createElement($this->getNodeName()); |
81
|
1 |
|
$element->setAttribute($this->getUrlAttributeName(), $media->getUrl()); |
82
|
1 |
|
$element->setAttribute('type', $media->getType() ?? ''); |
83
|
1 |
|
$element->setAttribute('length', $media->getLength() ?? ''); |
84
|
|
|
|
85
|
1 |
|
return $element; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param \MediaInterface $media |
90
|
|
|
* @param \DomElement $element |
91
|
|
|
*/ |
92
|
1 |
|
protected function initMedia(MediaInterface $media, \DOMElement $element): void |
93
|
|
|
{ |
94
|
1 |
|
$media->setTitle($this->getChildValue($element, 'title', static::MRSS_NAMESPACE)); |
95
|
1 |
|
$media->setDescription($this->getChildValue($element, 'description', static::MRSS_NAMESPACE)); |
96
|
1 |
|
$media->setThumbnail($this->getChildAttributeValue($element, 'thumbnail', 'url', static::MRSS_NAMESPACE)); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @inheritDoc |
101
|
|
|
*/ |
102
|
5 |
|
protected function hasValue(NodeInterface $node) : bool |
103
|
|
|
{ |
104
|
5 |
|
return $node instanceof ItemInterface && !! $node->getMedias(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
5 |
|
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
111
|
|
|
{ |
112
|
5 |
|
foreach ($node->getMedias() as $media) { |
|
|
|
|
113
|
1 |
|
$rootElement->appendChild($this->createMediaElement($document, $media)); |
114
|
|
|
} |
115
|
5 |
|
} |
116
|
|
|
} |
117
|
|
|
|
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: