Completed
Push — master ( ef4b76...8c4081 )
by Joschi
02:36
created

AbstractElementProcessor::getPropertyStringValue()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 5.025

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 9
cts 10
cp 0.9
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 8
nc 4
nop 1
crap 5.025
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\Contract\ElementProcessorInterface;
41
use Jkphl\RdfaLiteMicrodata\Domain\Property\Property;
42
use Jkphl\RdfaLiteMicrodata\Domain\Thing\Thing;
43
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
44
use Jkphl\RdfaLiteMicrodata\Domain\Type\Type;
45
use Jkphl\RdfaLiteMicrodata\Domain\Type\TypeInterface;
46
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
47
use Jkphl\RdfaLiteMicrodata\Infrastructure\Exceptions\RuntimeException;
48
49
/**
50
 * Abstract element processor
51
 *
52
 * @package Jkphl\RdfaLiteMicrodata
53
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
54
 */
55
abstract class AbstractElementProcessor implements ElementProcessorInterface
56
{
57
    /**
58
     * Tag name / attribute map
59
     *
60
     * @var array
61
     */
62
    protected static $tagNameAttributes = [
63
        'META' => 'content',
64
        'AUDIO' => 'src',
65
        'EMBED' => 'src',
66
        'IFRAME' => 'src',
67
        'IMG' => 'src',
68
        'SOURCE' => 'src',
69
        'TRACK' => 'src',
70
        'VIDEO' => 'src',
71
        'A' => 'href',
72
        'AREA' => 'href',
73
        'LINK' => 'href',
74
        'OBJECT' => 'data',
75
        'DATA' => 'value',
76
        'METER' => 'value',
77
        'TIME' => 'datetime'
78
    ];
79
    /**
80
     * HTML mode
81
     *
82
     * @var boolean
83
     */
84
    protected $html;
85
86
    /**
87
     * Element processor constructor
88
     *
89
     * @param bool $html HTML mode
90
     */
91 13
    public function __construct($html = false)
92
    {
93 13
        $this->html = boolval($html);
94 13
    }
95
96
    /**
97
     * Process a DOM element's child
98
     *
99
     * @param \DOMElement $element DOM element
100
     * @param ContextInterface $context Context
101
     * @return ContextInterface Context for children
102
     */
103 13
    public function processElementChildren(\DOMElement $element, ContextInterface $context)
104
    {
105
        // Process a child
106 13
        return $this->processChild($element, $context);
107
    }
108
109
    /**
110
     * Create a nested child
111
     *
112
     * @param \DOMElement $element DOM element
113
     * @param ContextInterface $context Context
114
     * @return ContextInterface Context for children
115
     */
116
    abstract protected function processChild(\DOMElement $element, ContextInterface $context);
117
118
    /**
119
     * Create a property
120
     *
121
     * @param \DOMElement $element DOM element
122
     * @param ContextInterface $context Inherited Context
123
     * @return ContextInterface Local context for this element
124
     */
125
    abstract protected function processProperty(\DOMElement $element, ContextInterface $context);
126
127
    /**
128
     * Split a property into prefix and name
129
     *
130
     * @param string $property Prefixed property
131
     * @return array Prefix and name
132
     */
133 5
    protected function splitProperty($property)
134
    {
135 5
        $property = explode(':', $property);
136 5
        $name = strval(array_pop($property));
137 5
        $prefix = strval(array_pop($property));
138 5
        return [$prefix, $name];
139
    }
140
141
    /**
142
     * Create a property by prefix and name
143
     *
144
     * @param string $prefix Property prefix
145
     * @param string $name Property name
146
     * @param \DOMElement $element DOM element
147
     * @param ContextInterface $context Inherited Context
148
     * @return ContextInterface Local context for this element
149
     */
150 7
    protected function processPropertyPrefixName($prefix, $name, \DOMElement $element, ContextInterface $context)
151
    {
152 7
        $vocabulary = $this->getVocabulary($prefix, $context);
153 7
        if ($vocabulary instanceof VocabularyInterface) {
154 7
            $context = $this->addProperty($element, $context, $name, $vocabulary);
155 6
        }
156
157 6
        return $context;
158
    }
159
160
    /**
161
     * Return a vocabulary by prefix with fallback to the default vocabulary
162
     *
163
     * @param string $prefix Vocabulary prefix
164
     * @param ContextInterface $context Context
165
     * @return VocabularyInterface Vocabulary
166
     */
167
    abstract protected function getVocabulary($prefix, ContextInterface $context);
168
169
    /**
170
     * Add a single property
171
     *
172
     * @param \DOMElement $element DOM element
173
     * @param ContextInterface $context Inherited Context
174
     * @param string $name Property name
175
     * @param VocabularyInterface $vocabulary Property vocabulary
176
     * @return ContextInterface Local context for this element
177
     */
178 7
    protected function addProperty(
179
        \DOMElement $element,
180
        ContextInterface $context,
181
        $name,
182
        VocabularyInterface $vocabulary
183
    ) {
184 7
        $resourceId = $this->getResourceId($element);
185
186
        // Get the property value
187 7
        $propertyValue = $this->getPropertyValue($element, $context);
188 7
        $property = new Property($name, $vocabulary, $propertyValue, $resourceId);
189
190
        // Add the property to the current parent thing
191 6
        $context->getParentThing()->addProperty($property);
192
193
        // If the property value is a thing
194 6
        if ($propertyValue instanceof ThingInterface) {
195
            // Set the thing as parent thing for nested iterations
196 3
            $context = $context->setParentThing($propertyValue);
197 3
        }
198
199 6
        return $context;
200
    }
201
202
    /**
203
     * Return the resource ID
204
     *
205
     * @param \DOMElement $element DOM element
206
     * @return string|null Resource ID
207
     */
208
    abstract protected function getResourceId(\DOMElement $element);
209
210
    /**
211
     * Return a property value (type and tag name dependent)
212
     *
213
     * @param \DOMElement $element DOM element
214
     * @param ContextInterface $context Context
215
     * @return ThingInterface|string Property value
216
     */
217
    abstract protected function getPropertyValue(\DOMElement $element, ContextInterface $context);
218
219
    /**
220
     * Return a thing by typeof value
221
     *
222
     * @param string $typeof Thing type
223
     * @param string $resourceId Resource ID
224
     * @param ContextInterface $context Context
225
     * @return Thing Thing
226
     * @throws RuntimeException If the default vocabulary is empty
227
     */
228 13
    protected function getThing($typeof, $resourceId, ContextInterface $context)
229
    {
230
        /** @var TypeInterface[] $types */
231 13
        $types = [];
232 13
        foreach (preg_split('/\s+/', $typeof) as $prefixedType) {
233 13
            $prefixedType = explode(':', $prefixedType);
234 13
            $typeName = array_pop($prefixedType);
235 13
            $prefix = array_pop($prefixedType);
236
237 13
            $vocabulary = $this->getVocabulary($prefix, $context);
238 10
            if ($vocabulary instanceof VocabularyInterface) {
239 7
                $types[] = new Type($typeName, $vocabulary);
240 7
                continue;
241
            }
242
243
            // If the default vocabulary is empty
244 3
            if (empty($prefix)) {
245 3
                throw new RuntimeException(
246 3
                    RuntimeException::EMPTY_DEFAULT_VOCABULARY_STR,
247
                    RuntimeException::EMPTY_DEFAULT_VOCABULARY
248 3
                );
249
            }
250 7
        }
251
252 7
        return new Thing($types, $resourceId);
253
    }
254
255
    /**
256
     * Return a property value (type and tag name dependent)
257
     *
258
     * @param \DOMElement $element DOM element
259
     * @return ThingInterface|string Property value
260
     */
261 7
    protected function getPropertyStringValue(\DOMElement $element)
262
    {
263
        // If HTML mode is active
264 7
        if ($this->html) {
265 6
            $tagName = strtoupper($element->tagName);
266
267
            // Map to an attribute (if applicable)
268 6
            if (array_key_exists($tagName, self::$tagNameAttributes)) {
269 5
                $value = strval($element->getAttribute(self::$tagNameAttributes[$tagName]));
270 5
                if (($tagName != 'TIME') || !empty($value)) {
271 5
                    return $value;
272
                }
273
            }
274 6
        }
275
276
        // Return the text content
277 7
        return $element->textContent;
278
    }
279
}
280