Completed
Push — master ( c78c30...f35f13 )
by Joschi
02:42
created

RdfaLiteElementProcessor::getPropertyStringValue()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 7
cts 7
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 7
nc 3
nop 1
crap 4
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\Context\RdfaLiteContext;
41
use Jkphl\RdfaLiteMicrodata\Application\Parser\RootThing;
42
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
43
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\Vocabulary;
44
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
45
46
/**
47
 * RDFa Lite 1.1 element processor
48
 *
49
 * @package Jkphl\RdfaLiteMicrodata
50
 * @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure
51
 */
52
class RdfaLiteElementProcessor extends AbstractElementProcessor
53
{
54
    /**
55
     * Process a DOM element
56
     *
57
     * @param \DOMElement $element DOM element
58
     * @param ContextInterface $context Inherited Context
59
     * @return ContextInterface Local context for this element
60
     */
61 11
    public function processElement(\DOMElement $element, ContextInterface $context)
62
    {
63
        // Process default vocabulary registrations
64 11
        $context = $this->processVocab($element, $context);
65
66
        // Register vocabulary prefixes
67 11
        $context = $this->processPrefix($element, $context);
68
69
        // Create a property
70 11
        return $this->processProperty($element, $context);
71
    }
72
73
    /**
74
     * Process changes of the default vocabulary
75
     *
76
     * @param \DOMElement $element DOM element
77
     * @param ContextInterface $context Inherited Context
78
     * @return ContextInterface Local context for this element
79
     */
80 11
    protected function processVocab(\DOMElement $element, ContextInterface $context)
81
    {
82 11
        if ($element->hasAttribute('vocab') && ($context instanceof RdfaLiteContext)) {
83 8
            $defaultVocabulary = new Vocabulary($element->getAttribute('vocab'));
84 8
            $context = $context->setDefaultVocabulary($defaultVocabulary);
85 8
        }
86
87 11
        return $context;
88
    }
89
90
    /**
91
     * Process vocabulary prefixes
92
     *
93
     * @param \DOMElement $element DOM element
94
     * @param ContextInterface $context Inherited Context
95
     * @return ContextInterface Local context for this element
96
     */
97 11
    protected function processPrefix(\DOMElement $element, ContextInterface $context)
98
    {
99 11
        if ($element->hasAttribute('prefix') && ($context instanceof RdfaLiteContext)) {
100 8
            $prefixes = preg_split('/\s+/', $element->getAttribute('prefix'));
101 8
            while (count($prefixes)) {
102 8
                $prefix = rtrim(array_shift($prefixes), ':');
103 8
                $uri = array_shift($prefixes);
104 8
                $context = $context->registerVocabulary($prefix, $uri);
105 8
            }
106 8
        }
107
108 11
        return $context;
109
    }
110
111
    /**
112
     * Create a property
113
     *
114
     * @param \DOMElement $element DOM element
115
     * @param ContextInterface $context Inherited Context
116
     * @return ContextInterface Local context for this element
117
     */
118 11
    protected function processProperty(\DOMElement $element, ContextInterface $context)
119
    {
120 11
        if ($element->hasAttribute('property') && !($context->getParentThing() instanceof RootThing)) {
121 5
            list($prefix, $name) = $this->splitProperty($element->getAttribute('property'));
122 5
            $context = $this->processPropertyPrefixName($prefix, $name, $element, $context);
123 5
        }
124
125 11
        return $context;
126
    }
127
128
    /**
129
     * Create a nested child
130
     *
131
     * @param \DOMElement $element DOM element
132
     * @param ContextInterface $context Context
133
     * @return ContextInterface Context for children
134
     */
135 11
    protected function processChild(\DOMElement $element, ContextInterface $context)
136
    {
137 11
        if ($element->hasAttribute('typeof')
138 11
            && (empty($element->getAttribute('property')) || $context->getParentThing() instanceof RootThing)
139 11
        ) {
140 11
            $thing = $this->getThing(
141 11
                $element->getAttribute('typeof'),
142 11
                trim($element->getAttribute('resource')) ?: null,
143
                $context
144 11
            );
145
146
            // Add the new thing as a child to the current context
147
            // and set the thing as parent thing for nested iterations
148 5
            $context = $context->addChild($thing)->setParentThing($thing);
149 5
        }
150
151 11
        return $context;
152
    }
153
154
    /**
155
     * Return a property value (type and tag name dependent)
156
     *
157
     * @param \DOMElement $element DOM element
158
     * @param ContextInterface $context Context
159
     * @return ThingInterface|string Property value
160
     */
161 5
    protected function getPropertyValue(\DOMElement $element, ContextInterface $context)
162
    {
163
        // If the property creates a new type: Return the element itself
164 5
        if ($element->hasAttribute('typeof')) {
165 2
            return $this->getThing(
166 2
                $element->getAttribute('typeof'),
167 2
                trim($element->getAttribute('resource')) ?: null,
168
                $context
169 2
            );
170
        }
171
172
        // Return a string property value
173 5
        return $this->getPropertyStringValue($element);
174
    }
175
176
    /**
177
     * Return the resource ID
178
     *
179
     * @param \DOMElement $element DOM element
180
     * @return string|null Resource ID
181
     */
182 5
    protected function getResourceId(\DOMElement $element)
183
    {
184 5
        return trim($element->getAttribute('resource')) ?: null;
185
    }
186
187
    /**
188
     * Return a vocabulary by prefix with fallback to the default vocabulary
189
     *
190
     * @param string $prefix Vocabulary prefix
191
     * @param ContextInterface $context Context
192
     * @return VocabularyInterface Vocabulary
193
     */
194 11
    protected function getVocabulary($prefix, ContextInterface $context)
195
    {
196 11
        return (empty($prefix) || !($context instanceof RdfaLiteContext)) ?
197 11
            $context->getDefaultVocabulary() : $context->getVocabulary($prefix);
198
    }
199
}
200