AbstractElementProcessor::getType()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
crap 2
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\Thing\Thing;
42
use Jkphl\RdfaLiteMicrodata\Domain\Type\Type;
43
use Jkphl\RdfaLiteMicrodata\Domain\Type\TypeInterface;
44
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
45
use Jkphl\RdfaLiteMicrodata\Infrastructure\Exceptions\RuntimeException;
46
47
/**
48
 * Abstract element processor
49
 *
50
 * @package Jkphl\RdfaLiteMicrodata
51
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
52
 */
53
abstract class AbstractElementProcessor implements ElementProcessorInterface
54
{
55
    /**
56
     * Use the property processor methods
57
     */
58
    use PropertyProcessorTrait;
59
60
    /**
61
     * Tag name / attribute map
62
     *
63
     * @var array
64
     */
65
    protected static $tagNameAttributes = [
66
        'META' => 'content',
67
        'AUDIO' => 'src',
68
        'EMBED' => 'src',
69
        'IFRAME' => 'src',
70
        'IMG' => 'src',
71
        'SOURCE' => 'src',
72
        'TRACK' => 'src',
73
        'VIDEO' => 'src',
74
        'A' => 'href',
75
        'AREA' => 'href',
76
        'LINK' => 'href',
77
        'OBJECT' => 'data',
78
        'DATA' => 'value',
79
        'METER' => 'value',
80
        'TIME' => 'datetime'
81
    ];
82
    /**
83
     * Property cache
84
     *
85
     * @var array
86
     */
87
    protected $propertyCache = [];
88
    /**
89
     * HTML mode
90
     *
91
     * @var boolean
92
     */
93
    protected $html;
94
95
    /**
96
     * Enable / disable HTML element value resolution
97
     *
98
     * @param bool $html Enable HTML element value resolution
99
     * @return ElementProcessorInterface Self reference
100
     */
101 14
    public function setHtml($html)
102
    {
103 14
        $this->html = boolval($html);
104 14
        return $this;
105
    }
106
107
    /**
108
     * Process a DOM element's child
109
     *
110
     * @param \DOMElement $element DOM element
111
     * @param ContextInterface $context Context
112
     * @return ContextInterface Context for children
113
     */
114 17
    public function processElementChildren(\DOMElement $element, ContextInterface $context)
115
    {
116
        // Process a child
117 17
        return $this->processChild($element, $context);
118
    }
119
120
    /**
121
     * Create a nested child
122
     *
123
     * @param \DOMElement $element DOM element
124
     * @param ContextInterface $context Context
125
     * @return ContextInterface Context for children
126
     */
127
    abstract protected function processChild(\DOMElement $element, ContextInterface $context);
128
129
    /**
130
     * Return a thing by typeof value
131
     *
132
     * @param string|null $typeof Thing type
133
     * @param string|null $resourceId Resource ID
134
     * @param ContextInterface $context Context
135
     * @return Thing Thing
136
     * @throws RuntimeException If the default vocabulary is empty
137
     */
138 17
    protected function getThing($typeof, $resourceId, ContextInterface $context)
139
    {
140
        /** @var TypeInterface[] $types */
141 17
        $types = [];
142 17
        if (strlen($typeof)) {
143 16
            foreach (preg_split('/\s+/', $typeof) as $prefixedType) {
144 16
                $types[] = $this->getType($prefixedType, $context);
145 10
            }
146 10
        }
147
148 11
        return new Thing($types, $resourceId);
149
    }
150
151
    /**
152
     * Instantiate a type
153
     *
154
     * @param string $prefixedType Prefixed type
155
     * @param ContextInterface $context Context
156
     * @return TypeInterface Type
157
     */
158 16
    protected function getType($prefixedType, ContextInterface $context)
159
    {
160 16
        list($prefix, $typeName) = $this->getPrefixName($prefixedType);
161 16
        $vocabulary = $this->getVocabulary($prefix, $context);
162 13
        if ($vocabulary instanceof VocabularyInterface) {
163 10
            return new Type($typeName, $vocabulary);
164
        }
165
166
        // If the default vocabulary is empty
167 3
        throw new RuntimeException(
168 3
            RuntimeException::EMPTY_DEFAULT_VOCABULARY_STR,
169
            RuntimeException::EMPTY_DEFAULT_VOCABULARY
170 3
        );
171
    }
172
173
    /**
174
     * Split a value into a vocabulary prefix and a name
175
     *
176
     * @param string $prefixName Prefixed name
177
     * @return array Prefix and name
178
     */
179
    abstract protected function getPrefixName($prefixName);
180
}
181