Completed
Push — master ( a08768...b494ea )
by Joschi
04:40
created

MicrodataElementProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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