Completed
Push — master ( c78c30...f35f13 )
by Joschi
02:42
created

MicrodataElementProcessor   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 2
dl 0
loc 98
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A processElement() 0 5 1
A processProperty() 0 8 3
B processChild() 0 18 5
A getResourceId() 0 4 2
A getPropertyValue() 0 10 3
A getVocabulary() 0 4 1
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2017 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Jkphl\RdfaLiteMicrodata\Infrastructure\Parser;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Context\ContextInterface;
40
use Jkphl\RdfaLiteMicrodata\Application\Parser\RootThing;
41
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
42
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
43
44
/**
45
 * Microdata element processor
46
 *
47
 * @package Jkphl\RdfaLiteMicrodata
48
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
49
 */
50
class MicrodataElementProcessor extends AbstractElementProcessor
51
{
52
    /**
53
     * Process a DOM element
54
     *
55
     * @param \DOMElement $element DOM element
56
     * @param ContextInterface $context Inherited Context
57
     * @return ContextInterface Local context for this element
58
     */
59 2
    public function processElement(\DOMElement $element, ContextInterface $context)
60
    {
61
        // Create a property
62 2
        return $this->processProperty($element, $context);
63
    }
64
65
    /**
66
     * Create a property
67
     *
68
     * @param \DOMElement $element DOM element
69
     * @param ContextInterface $context Inherited Context
70
     * @return ContextInterface Local context for this element
71
     */
72 2
    protected function processProperty(\DOMElement $element, ContextInterface $context)
73
    {
74 2
        if ($element->hasAttribute('itemprop') && !($context->getParentThing() instanceof RootThing)) {
75 2
            $context = $this->processPropertyPrefixName(null, $element->getAttribute('itemprop'), $element, $context);
76 1
        }
77
78 2
        return $context;
79
    }
80
81
    /**
82
     * Create a nested child
83
     *
84
     * @param \DOMElement $element DOM element
85
     * @param ContextInterface $context Context
86
     * @return ContextInterface Context for children
87
     */
88 2
    protected function processChild(\DOMElement $element, ContextInterface $context)
89
    {
90 2
        if ($element->hasAttribute('itemtype')
91 2
            && (empty($element->getAttribute('itemprop')) || $context->getParentThing() instanceof RootThing)
92 2
        ) {
93 2
            $thing = $this->getThing(
94 2
                $element->getAttribute('itemtype'),
95 2
                trim($element->getAttribute('itemid')) ?: null,
96
                $context
97 2
            );
98
99
            // Add the new thing as a child to the current context
100
            // and set the thing as parent thing for nested iterations
101 2
            $context = $context->addChild($thing)->setParentThing($thing);
102 2
        }
103
104 2
        return $context;
105
    }
106
107
    /**
108
     * Return the resource ID
109
     *
110
     * @param \DOMElement $element DOM element
111
     * @return string|null Resource ID
112
     */
113 2
    protected function getResourceId(\DOMElement $element)
114
    {
115 2
        return trim($element->getAttribute('itemid')) ?: null;
116
    }
117
118
    /**
119
     * Return a property value (type and tag name dependent)
120
     *
121
     * @param \DOMElement $element DOM element
122
     * @param ContextInterface $context Context
123
     * @return ThingInterface|string Property value
124
     */
125 2
    protected function getPropertyValue(\DOMElement $element, ContextInterface $context)
126
    {
127
        // If the property creates a new type: Return the element itself
128 2
        if ($element->hasAttribute('itemscope') && $element->hasAttribute('itemtype')) {
129 1
            return $this->getThing($element->getAttribute('itemtype'), null, $context);
130
        }
131
132
        // Return a string property value
133 2
        return $this->getPropertyStringValue($element);
134
    }
135
136
    /**
137
     * Return a vocabulary by prefix with fallback to the default vocabulary
138
     *
139
     * @param string $prefix Vocabulary prefix
140
     * @param ContextInterface $context Context
141
     * @return VocabularyInterface Vocabulary
142
     */
143 2
    protected function getVocabulary($prefix, ContextInterface $context)
144
    {
145 2
        return $context->getDefaultVocabulary();
146
    }
147
}
148