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\RootThing; |
41
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface; |
42
|
|
|
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Microdata element processor |
46
|
|
|
* |
47
|
|
|
* @package Jkphl\RdfaLiteMicrodata |
48
|
|
|
* @subpackage Jkphl\RdfaLiteMicrodata\Infrastructure |
49
|
|
|
*/ |
50
|
|
|
class MicrodataElementProcessor extends AbstractElementProcessor |
51
|
|
|
{ |
52
|
|
|
/** |
53
|
|
|
* Process a DOM element |
54
|
|
|
* |
55
|
|
|
* @param \DOMElement $element DOM element |
56
|
|
|
* @param ContextInterface $context Inherited Context |
57
|
|
|
* @return ContextInterface Local context for this element |
58
|
|
|
*/ |
59
|
2 |
|
public function processElement(\DOMElement $element, ContextInterface $context) |
60
|
|
|
{ |
61
|
|
|
// Create a property |
62
|
2 |
|
return $this->processProperty($element, $context); |
63
|
|
|
|
64
|
|
|
// TODO: itemref |
65
|
|
|
// TODO: Anonymous item type |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Create a property |
70
|
|
|
* |
71
|
|
|
* @param \DOMElement $element DOM element |
72
|
|
|
* @param ContextInterface $context Inherited Context |
73
|
|
|
* @return ContextInterface Local context for this element |
74
|
|
|
*/ |
75
|
2 |
|
protected function processProperty(\DOMElement $element, ContextInterface $context) |
76
|
|
|
{ |
77
|
2 |
|
if ($element->hasAttribute('itemprop') && !($context->getParentThing() instanceof RootThing)) { |
78
|
2 |
|
$properties = preg_split('/\s+/', $element->getAttribute('itemprop')); |
79
|
2 |
|
foreach ($properties as $index => $property) { |
80
|
2 |
|
$firstProperty = ($index ? 0 : self::PROPERTY_FIRST); |
81
|
2 |
|
$lastProperty = ($index == (count($properties) - 1)) ? self::PROPERTY_LAST : 0; |
82
|
2 |
|
$context = $this->processPropertyPrefixName( |
83
|
2 |
|
null, |
84
|
|
|
$property, |
85
|
|
|
$element, |
86
|
|
|
$context, |
87
|
2 |
|
$firstProperty | $lastProperty |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
return $context; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create a nested child |
97
|
|
|
* |
98
|
|
|
* @param \DOMElement $element DOM element |
99
|
|
|
* @param ContextInterface $context Context |
100
|
|
|
* @return ContextInterface Context for children |
101
|
|
|
*/ |
102
|
2 |
|
protected function processChild(\DOMElement $element, ContextInterface $context) |
103
|
|
|
{ |
104
|
2 |
|
if ($element->hasAttribute('itemtype') |
105
|
2 |
|
&& (empty($element->getAttribute('itemprop')) || $context->getParentThing() instanceof RootThing) |
106
|
|
|
) { |
107
|
2 |
|
$thing = $this->getThing( |
108
|
2 |
|
$element->getAttribute('itemtype'), |
109
|
2 |
|
trim($element->getAttribute('itemid')) ?: null, |
110
|
|
|
$context |
111
|
|
|
); |
112
|
|
|
|
113
|
|
|
// Add the new thing as a child to the current context |
114
|
|
|
// and set the thing as parent thing for nested iterations |
115
|
2 |
|
$context = $context->addChild($thing)->setParentThing($thing); |
116
|
|
|
} |
117
|
|
|
|
118
|
2 |
|
return $context; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return the resource ID |
123
|
|
|
* |
124
|
|
|
* @param \DOMElement $element DOM element |
125
|
|
|
* @return string|null Resource ID |
126
|
|
|
*/ |
127
|
2 |
|
protected function getResourceId(\DOMElement $element) |
128
|
|
|
{ |
129
|
2 |
|
return trim($element->getAttribute('itemid')) ?: null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return a property child value |
134
|
|
|
* |
135
|
|
|
* @param \DOMElement $element DOM element |
136
|
|
|
* @param ContextInterface $context Context |
137
|
|
|
* @return ThingInterface|null Property child value |
138
|
|
|
*/ |
139
|
2 |
|
protected function getPropertyChildValue(\DOMElement $element, ContextInterface $context) |
140
|
|
|
{ |
141
|
|
|
// If the property creates a new type: Return the element itself |
142
|
2 |
|
if ($element->hasAttribute('itemscope') && $element->hasAttribute('itemtype')) { |
143
|
1 |
|
return $this->getThing($element->getAttribute('itemtype'), null, $context); |
144
|
|
|
} |
145
|
|
|
|
146
|
2 |
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return a vocabulary by prefix with fallback to the default vocabulary |
151
|
|
|
* |
152
|
|
|
* @param string $prefix Vocabulary prefix |
153
|
|
|
* @param ContextInterface $context Context |
154
|
|
|
* @return VocabularyInterface Vocabulary |
155
|
|
|
*/ |
156
|
2 |
|
protected function getVocabulary($prefix, ContextInterface $context) |
157
|
|
|
{ |
158
|
2 |
|
$prefix = null; |
|
|
|
|
159
|
2 |
|
return $context->getDefaultVocabulary(); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Split a value into a vocabulary prefix and a name |
164
|
|
|
* |
165
|
|
|
* @param string $prefixName Prefixed name |
166
|
|
|
* @return array Prefix and name |
167
|
|
|
*/ |
168
|
2 |
|
protected function getPrefixName($prefixName) |
169
|
|
|
{ |
170
|
2 |
|
return [null, $prefixName]; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.