1
|
|
|
<?php |
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
|
|
|
protected $urlAttributeName = 'url'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
5 |
|
public function getUrlAttributeName() |
29
|
|
|
{ |
30
|
5 |
|
return $this->urlAttributeName; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
* @return $this |
36
|
|
|
*/ |
37
|
8 |
|
public function setUrlAttributeName($name) |
38
|
|
|
{ |
39
|
8 |
|
$this->urlAttributeName = $name; |
40
|
|
|
|
41
|
8 |
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param NodeInterface $node |
46
|
|
|
* @param \DOMElement $element |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
4 |
|
public function setProperty(NodeInterface $node, \DOMElement $element) |
50
|
|
|
{ |
51
|
4 |
|
if ($node instanceof ItemInterface) { |
52
|
4 |
|
$media = $node->newMedia(); |
53
|
4 |
|
$media->setType($this->getAttributeValue($element, 'type')) |
54
|
4 |
|
->setUrl($this->getAttributeValue($element, $this->getUrlAttributeName())) |
55
|
4 |
|
->setLength($this->getAttributeValue($element, 'length')); |
56
|
|
|
|
57
|
4 |
|
$node->addMedia($media); |
58
|
|
|
} |
59
|
|
|
|
60
|
4 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param \DomDocument $document |
65
|
|
|
* @param MediaInterface $media |
66
|
|
|
* @return \DomElement |
67
|
|
|
*/ |
68
|
1 |
|
public function createMediaElement(\DomDocument $document, MediaInterface $media) |
69
|
|
|
{ |
70
|
1 |
|
$element = $document->createElement($this->getNodeName()); |
71
|
1 |
|
$element->setAttribute($this->getUrlAttributeName(), $media->getUrl()); |
72
|
1 |
|
$element->setAttribute('type', $media->getType()); |
73
|
1 |
|
$element->setAttribute('length', $media->getLength()); |
74
|
|
|
|
75
|
1 |
|
return $element; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritDoc |
80
|
|
|
*/ |
81
|
2 |
|
protected function hasValue(NodeInterface $node) : bool |
82
|
|
|
{ |
83
|
2 |
|
return $node instanceof ItemInterface && !! $node->getMedias(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritDoc |
88
|
|
|
*/ |
89
|
2 |
|
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
90
|
|
|
{ |
91
|
2 |
|
foreach ($node->getMedias() as $media) { |
|
|
|
|
92
|
1 |
|
$rootElement->appendChild($this->createMediaElement($document, $media)); |
93
|
|
|
} |
94
|
2 |
|
} |
95
|
|
|
} |
96
|
|
|
|
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: