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