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\Node\CategoryInterface; |
14
|
|
|
use FeedIo\Feed\NodeInterface; |
15
|
|
|
use FeedIo\RuleAbstract; |
16
|
|
|
|
17
|
|
|
class Category extends RuleAbstract |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
const NODE_NAME = 'category'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param NodeInterface $node |
24
|
|
|
* @param \DOMElement $element |
25
|
|
|
* @return mixed |
26
|
|
|
*/ |
27
|
1 |
|
public function setProperty(NodeInterface $node, \DOMElement $element) |
28
|
|
|
{ |
29
|
1 |
|
$category = $node->newCategory(); |
30
|
1 |
|
$category->setScheme($this->getAttributeValue($element, 'domain')) |
31
|
1 |
|
->setLabel($element->nodeValue) |
32
|
1 |
|
->setTerm($element->nodeValue); |
33
|
1 |
|
$node->addCategory($category); |
34
|
|
|
|
35
|
1 |
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* creates the accurate DomElement content according to the $item's property |
40
|
|
|
* |
41
|
|
|
* @param \DomDocument $document |
42
|
|
|
* @param NodeInterface $node |
43
|
|
|
* @return \DomElement|null |
44
|
|
|
*/ |
45
|
|
|
public function createElement(\DomDocument $document, NodeInterface $node) |
46
|
|
|
{ |
47
|
|
|
if ( ! is_null($node->getCategories()) ) { |
48
|
|
|
foreach( $node->getCategories() as $category ) { |
49
|
|
|
return $this->createCategoryElement($document, $category); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param \DomDocument $document |
58
|
|
|
* @param CategoryInterface $category |
59
|
|
|
* @return \DomElement |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function createCategoryElement(\DomDocument $document, CategoryInterface $category) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$element = $document->createElement( |
64
|
|
|
$this->getNodeName(), |
65
|
|
|
is_null($category->getTerm()) ? $category->getLabel():$category->getTerm() |
66
|
|
|
); |
67
|
|
|
$element->setAttribute('domain', $category->getScheme()); |
68
|
|
|
|
69
|
|
|
return $element; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.