Completed
Push — master ( 342540...c82ee6 )
by Joschi
02:27
created

MicrodataElementProcessor::processItemReferences()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
ccs 15
cts 15
cp 1
rs 8.7624
cc 5
eloc 11
nc 3
nop 3
crap 5
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\DOMIterator;
41
use Jkphl\RdfaLiteMicrodata\Application\Parser\RootThing;
42
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
43
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
44
45
/**
46
 * Microdata element processor
47
 *
48
 * @package Jkphl\RdfaLiteMicrodata
49
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
50
 */
51
class MicrodataElementProcessor extends AbstractElementProcessor
52
{
53
    /**
54
     * Process a DOM element
55
     *
56
     * @param \DOMElement $element DOM element
57
     * @param ContextInterface $context Inherited Context
58
     * @return ContextInterface Local context for this element
59
     */
60 3
    public function processElement(\DOMElement $element, ContextInterface $context)
61
    {
62
        // Create a property
63 3
        $propertyContext = $this->processProperty($element, $context);
64
65
        // Process the element in case it's an anonymous thing with no children
66 3
        if (!$element->childNodes->length) {
67 3
            $this->processChild($element, $context);
68 3
        }
69
70 3
        return $propertyContext;
71
    }
72
73
    /**
74
     * Create a property
75
     *
76
     * @param \DOMElement $element DOM element
77
     * @param ContextInterface $context Inherited Context
78
     * @return ContextInterface Local context for this element
79
     */
80 3
    protected function processProperty(\DOMElement $element, ContextInterface $context)
81
    {
82 3
        if ($element->hasAttribute('itemprop') && !($context->getParentThing() instanceof RootThing)) {
83 3
            $itemprops = trim($element->getAttribute('itemprop'));
84 3
            $itemprops = strlen($itemprops) ? preg_split('/\s+/', $itemprops) : [];
85 3
            foreach ($itemprops as $index => $itemprop) {
86 3
                $context = $this->processPropertyPrefixName(
87 3
                    null,
88 3
                    $itemprop,
89 3
                    $element,
90 3
                    $context,
91 3
                    $index == (count($itemprops) - 1)
92 3
                );
93 2
            }
94 2
        }
95
96 3
        return $context;
97
    }
98
99
    /**
100
     * Create a nested child
101
     *
102
     * @param \DOMElement $element DOM element
103
     * @param ContextInterface $context Context
104
     * @return ContextInterface Context for children
105
     */
106 3
    protected function processChild(\DOMElement $element, ContextInterface $context)
107
    {
108 3
        if ($element->hasAttribute('itemscope') && empty($element->getAttribute('itemprop'))) {
109 3
            $thing = $this->getThing(
110 3
                trim($element->getAttribute('itemtype')) ?: null,
111 3
                trim($element->getAttribute('itemid')) ?: null,
112
                $context
113 3
            );
114
115
            // Process item references
116 3
            $this->processItemReferences($element, $context, $thing);
117
118
            // Add the new thing as a child to the current context
119
            // and set the thing as parent thing for nested iterations
120 3
            $context = $context->addChild($thing)->setParentThing($thing);
121 3
        }
122
123 3
        return $context;
124
    }
125
126
    /**
127
     * Process item references
128
     *
129
     * @param \DOMElement $element DOM element
130
     * @param ContextInterface $context Context
131
     * @param ThingInterface $thing Thing
132
     * @return ThingInterface Thing
133
     */
134 3
    protected function processItemReferences(\DOMElement $element, ContextInterface $context, ThingInterface $thing)
135
    {
136
        // If the element has item references
137 3
        if ($element->hasAttribute('itemref')) {
138 1
            $itemrefElements = $this->getItemReferenceElements($element);
139 1
            if (count($itemrefElements)) {
140 1
                $iterator = new DOMIterator(
141 1
                    $itemrefElements,
142 1
                    $context->setParentThing($thing),
143 1
                    new MicrodataElementProcessor()
144 1
                );
145
146
                // Iterate through all $node
147 1
                foreach ($iterator->getRecursiveIterator() as $node) {
148 1
                    $node || true;
149 1
                }
150 1
            }
151 1
        }
152
153 3
        return $thing;
154
    }
155
156
    /**
157
     * Find all reference elements
158
     *
159
     * @param \DOMElement $element DOM element
160
     * @return \DOMElement[] Reference elements
161
     */
162 1
    protected function getItemReferenceElements(\DOMElement $element)
163
    {
164 1
        $itemrefElements = [];
165 1
        $itemrefs = trim($element->getAttribute('itemref'));
166 1
        $itemrefs = strlen($itemrefs) ? preg_split('/\s+/', $itemrefs) : [];
167 1
        foreach ($itemrefs as $itemref) {
168 1
            $itemrefElement = $element->ownerDocument->getElementById($itemref);
169 1
            if ($itemrefElement instanceof \DOMElement) {
170 1
                $itemrefElements[] = $itemrefElement;
171 1
            }
172 1
        }
173 1
        return $itemrefElements;
174
    }
175
176
    /**
177
     * Return the resource ID
178
     *
179
     * @param \DOMElement $element DOM element
180
     * @return string|null Resource ID
181
     */
182 3
    protected function getResourceId(\DOMElement $element)
183
    {
184 3
        return trim($element->getAttribute('itemid')) ?: null;
185
    }
186
187
    /**
188
     * Return a property child value
189
     *
190
     * @param \DOMElement $element DOM element
191
     * @param ContextInterface $context Context
192
     * @return ThingInterface|null Property child value
193
     */
194 3
    protected function getPropertyChildValue(\DOMElement $element, ContextInterface $context)
195
    {
196
        // If the property creates a new type: Return the element itself
197 3
        if ($element->hasAttribute('itemscope')) {
198 2
            $thing = $this->getThing($element->getAttribute('itemtype'), null, $context);
199 2
            return $this->processItemReferences($element, $context, $thing);
200
        }
201
202 3
        return null;
203
    }
204
205
    /**
206
     * Return a vocabulary by prefix with fallback to the default vocabulary
207
     *
208
     * @param string $prefix Vocabulary prefix
209
     * @param ContextInterface $context Context
210
     * @return VocabularyInterface Vocabulary
211
     */
212 3
    protected function getVocabulary($prefix, ContextInterface $context)
213
    {
214
        /** @var VocabularyInterface $vocabulary */
215 3
        $vocabulary = $prefix ?: $context->getDefaultVocabulary();
216 3
        return $vocabulary;
217
    }
218
219
    /**
220
     * Split a value into a vocabulary prefix and a name
221
     *
222
     * @param string $prefixName Prefixed name
223
     * @return array Prefix and name
224
     */
225 2
    protected function getPrefixName($prefixName)
226
    {
227 2
        return [null, $prefixName];
228
    }
229
}
230