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
|
5 |
|
public function getUrlAttributeName() : string |
29
|
|
|
{ |
30
|
5 |
|
return $this->urlAttributeName; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $name |
35
|
|
|
*/ |
36
|
9 |
|
public function setUrlAttributeName(string $name) : void |
37
|
|
|
{ |
38
|
9 |
|
$this->urlAttributeName = $name; |
39
|
9 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param NodeInterface $node |
43
|
|
|
* @param \DOMElement $element |
44
|
|
|
*/ |
45
|
4 |
|
public function setProperty(NodeInterface $node, \DOMElement $element) : void |
46
|
|
|
{ |
47
|
4 |
|
if ($node instanceof ItemInterface) { |
48
|
4 |
|
$media = $node->newMedia(); |
49
|
4 |
|
$media->setType($this->getAttributeValue($element, 'type')) |
50
|
4 |
|
->setUrl($this->getAttributeValue($element, $this->getUrlAttributeName())) |
51
|
4 |
|
->setLength($this->getAttributeValue($element, 'length')); |
52
|
|
|
|
53
|
4 |
|
$node->addMedia($media); |
54
|
|
|
} |
55
|
4 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param \DomDocument $document |
59
|
|
|
* @param MediaInterface $media |
60
|
|
|
* @return \DomElement |
61
|
|
|
*/ |
62
|
1 |
View Code Duplication |
public function createMediaElement(\DomDocument $document, MediaInterface $media) : \DOMElement |
|
|
|
|
63
|
|
|
{ |
64
|
1 |
|
$element = $document->createElement($this->getNodeName()); |
65
|
1 |
|
$element->setAttribute($this->getUrlAttributeName(), $media->getUrl()); |
66
|
1 |
|
$element->setAttribute('type', $media->getType() ?? ''); |
67
|
1 |
|
$element->setAttribute('length', $media->getLength() ?? ''); |
68
|
|
|
|
69
|
1 |
|
return $element; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
75
|
2 |
|
protected function hasValue(NodeInterface $node) : bool |
76
|
|
|
{ |
77
|
2 |
|
return $node instanceof ItemInterface && !! $node->getMedias(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
*/ |
83
|
2 |
|
protected function addElement(\DomDocument $document, \DOMElement $rootElement, NodeInterface $node) : void |
84
|
|
|
{ |
85
|
2 |
|
foreach ($node->getMedias() as $media) { |
|
|
|
|
86
|
1 |
|
$rootElement->appendChild($this->createMediaElement($document, $media)); |
87
|
|
|
} |
88
|
2 |
|
} |
89
|
|
|
} |
90
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.