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 |
|
if ($element->nodeName == "media:group" or $element->nodeName == "media:content") |
54
|
|
|
{ |
55
|
1 |
|
$media->setTitle($this->getChildValue($element, 'title', static::MRSS_NAMESPACE)); |
56
|
1 |
|
$media->setDescription($this->getChildValue($element, 'description', static::MRSS_NAMESPACE)); |
57
|
1 |
|
$thumbnails = $element->getElementsByTagNameNS(static::MRSS_NAMESPACE, "thumbnail"); |
58
|
1 |
|
if ($thumbnails->length > 0) { |
59
|
1 |
|
$media->setThumbnail($this->getAttributeValue($thumbnails->item(0), "url")); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
if ($element->nodeName == "media:content") { |
63
|
|
|
$media->setUrl($this->getAttributeValue($element, "url")); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
if ($element->nodeName == "media:group") { |
67
|
1 |
|
$contents = $element->getElementsByTagNameNS(static::MRSS_NAMESPACE, "content"); |
68
|
1 |
|
if ($contents->length > 0) { |
69
|
1 |
|
$media->setUrl($this->getAttributeValue($contents->item(0), "url")); |
|
|
|
|
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
else |
74
|
|
|
{ |
75
|
|
|
$media |
76
|
4 |
|
->setType($this->getAttributeValue($element, 'type')) |
77
|
4 |
|
->setUrl($this->getAttributeValue($element, $this->getUrlAttributeName())) |
78
|
4 |
|
->setLength($this->getAttributeValue($element, 'length')); |
79
|
|
|
|
80
|
|
|
} |
81
|
5 |
|
$node->addMedia($media); |
82
|
|
|
} |
83
|
5 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \DomDocument $document |
87
|
|
|
* @param MediaInterface $media |
88
|
|
|
* @return \DomElement |
89
|
|
|
*/ |
90
|
1 |
|
public function createMediaElement(\DomDocument $document, MediaInterface $media) : \DOMElement |
91
|
|
|
{ |
92
|
1 |
|
$element = $document->createElement($this->getNodeName()); |
93
|
1 |
|
$element->setAttribute($this->getUrlAttributeName(), $media->getUrl()); |
94
|
1 |
|
$element->setAttribute('type', $media->getType() ?? ''); |
95
|
1 |
|
$element->setAttribute('length', $media->getLength() ?? ''); |
96
|
|
|
|
97
|
1 |
|
return $element; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @inheritDoc |
102
|
|
|
*/ |
103
|
5 |
|
protected function hasValue(NodeInterface $node) : bool |
104
|
|
|
{ |
105
|
5 |
|
return $node instanceof ItemInterface && !! $node->getMedias(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @inheritDoc |
110
|
|
|
*/ |
111
|
5 |
|
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
112
|
|
|
{ |
113
|
5 |
|
foreach ($node->getMedias() as $media) { |
|
|
|
|
114
|
1 |
|
$rootElement->appendChild($this->createMediaElement($document, $media)); |
115
|
|
|
} |
116
|
5 |
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.