Completed
Push — master ( a57c7d...d1bd01 )
by Joschi
02:32
created

AbstractElementProcessor::getPropertyStringValue()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
crap 3
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 static $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 11
    public function setHtml($html)
102
    {
103 11
        $this->html = boolval($html);
104 11
        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 14
    public function processElementChildren(\DOMElement $element, ContextInterface $context)
115
    {
116
        // Process a child
117 14
        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 the resource ID
131
     *
132
     * @param \DOMElement $element DOM element
133
     * @return string|null Resource ID
134
     */
135
    abstract protected function getResourceId(\DOMElement $element);
136
137
    /**
138
     * Return a thing by typeof value
139
     *
140
     * @param string|null $typeof Thing type
141
     * @param string|null $resourceId Resource ID
142
     * @param ContextInterface $context Context
143
     * @return Thing Thing
144
     * @throws RuntimeException If the default vocabulary is empty
145
     */
146 14
    protected function getThing($typeof, $resourceId, ContextInterface $context)
147
    {
148
        /** @var TypeInterface[] $types */
149 14
        $types = [];
150 14
        if (strlen($typeof)) {
151 13
            foreach (preg_split('/\s+/', $typeof) as $prefixedType) {
152 13
                $types[] = $this->getType($prefixedType, $context);
153 7
            }
154 7
        }
155
156 8
        return new Thing($types, $resourceId);
157
    }
158
159
    /**
160
     * Instanciate a type
161
     *
162
     * @param string $prefixedType Prefixed type
163
     * @param ContextInterface $context Context
164
     * @return TypeInterface Type
165
     */
166 13
    protected function getType($prefixedType, ContextInterface $context)
167
    {
168 13
        list($prefix, $typeName) = $this->getPrefixName($prefixedType);
169 13
        $vocabulary = $this->getVocabulary($prefix, $context);
170 10
        if ($vocabulary instanceof VocabularyInterface) {
171 7
            return new Type($typeName, $vocabulary);
172
        }
173
174
        // If the default vocabulary is empty
175 3
        throw new RuntimeException(
176 3
            RuntimeException::EMPTY_DEFAULT_VOCABULARY_STR,
177
            RuntimeException::EMPTY_DEFAULT_VOCABULARY
178 3
        );
179
    }
180
181
    /**
182
     * Split a value into a vocabulary prefix and a name
183
     *
184
     * @param string $prefixName Prefixed name
185
     * @return array Prefix and name
186
     */
187
    abstract protected function getPrefixName($prefixName);
188
189
    /**
190
     * Return a vocabulary by prefix with fallback to the default vocabulary
191
     *
192
     * @param string $prefix Vocabulary prefix
193
     * @param ContextInterface $context Context
194
     * @return VocabularyInterface Vocabulary
195
     */
196
    abstract protected function getVocabulary($prefix, ContextInterface $context);
197
}
198