Completed
Push — master ( 2f5cae...701262 )
by Joschi
03:21
created

Context::getParentThing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * rdfa-lite-microdata
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\RdfaLiteMicrodata
8
 * @subpackage Jkphl\RdfaLiteMicrodata\Application
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\Application\Parser;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Exceptions\OutOfBoundsException;
40
use Jkphl\RdfaLiteMicrodata\Application\Exceptions\RuntimeException;
41
use Jkphl\RdfaLiteMicrodata\Domain\Thing\Thing;
42
use Jkphl\RdfaLiteMicrodata\Domain\Thing\ThingInterface;
43
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\Vocabulary;
44
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
45
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyService;
46
47
/**
48
 * Parsing context
49
 *
50
 * @package Jkphl\RdfaLiteMicrodata
51
 * @subpackage Jkphl\RdfaLiteMicrodata\Application
52
 */
53
class Context
54
{
55
    /**
56
     * Default vocabularies and their prefixes
57
     *
58
     * @var array
59
     * @see https://www.w3.org/2011/rdfa-context/rdfa-1.1
60
     * @link https://www.w3.org/2013/json-ld-context/rdfa11
61
     */
62
    protected static $defaultVocabularies = [
63
        'cat' => 'http://www.w3.org/ns/dcat#',
64
        'qb' => 'http://purl.org/linked-data/cube#',
65
        'grddl' => 'http://www.w3.org/2003/g/data-view#',
66
        'ma' => 'http://www.w3.org/ns/ma-ont#',
67
        'owl' => 'http://www.w3.org/2002/07/owl#',
68
        'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
69
        'rdfa' => 'http://www.w3.org/ns/rdfa#',
70
        'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
71
        'rif' => 'http://www.w3.org/2007/rif#',
72
        'rr' => 'http://www.w3.org/ns/r2rml#',
73
        'skos' => 'http://www.w3.org/2004/02/skos/core#',
74
        'skosxl' => 'http://www.w3.org/2008/05/skos-xl#',
75
        'wdr' => 'http://www.w3.org/2007/05/powder#',
76
        'void' => 'http://rdfs.org/ns/void#',
77
        'wdrs' => 'http://www.w3.org/2007/05/powder-s#',
78
        'xhv' => 'http://www.w3.org/1999/xhtml/vocab#',
79
        'xml' => 'http://www.w3.org/XML/1998/namespace',
80
        'xsd' => 'http://www.w3.org/2001/XMLSchema#',
81
        'prov' => 'http://www.w3.org/ns/prov#',
82
        'sd' => 'http://www.w3.org/ns/sparql-service-description#',
83
        'org' => 'http://www.w3.org/ns/org#',
84
        'gldp' => 'http://www.w3.org/ns/people#',
85
        'cnt' => 'http://www.w3.org/2008/content#',
86
        'dcat' => 'http://www.w3.org/ns/dcat#',
87
        'earl' => 'http://www.w3.org/ns/earl#',
88
        'ht' => 'http://www.w3.org/2006/http#',
89
        'ptr' => 'http://www.w3.org/2009/pointers#',
90
        'cc' => 'http://creativecommons.org/ns#',
91
        'ctag' => 'http://commontag.org/ns#',
92
        'dc' => 'http://purl.org/dc/terms/',
93
        'dc11' => 'http://purl.org/dc/elements/1.1/',
94
        'dcterms' => 'http://purl.org/dc/terms/',
95
        'foaf' => 'http://xmlns.com/foaf/0.1/',
96
        'gr' => 'http://purl.org/goodrelations/v1#',
97
        'ical' => 'http://www.w3.org/2002/12/cal/icaltzd#',
98
        'og' => 'http://ogp.me/ns#',
99
        'rev' => 'http://purl.org/stuff/rev#',
100
        'sioc' => 'http://rdfs.org/sioc/ns#',
101
        'v' => 'http://rdf.data-vocabulary.org/#',
102
        'vcard' => 'http://www.w3.org/2006/vcard/ns#',
103
        'schema' => 'http://schema.org/',
104
        'describedby' => 'http://www.w3.org/2007/05/powder-s#describedby',
105
        'license' => 'http://www.w3.org/1999/xhtml/vocab#license',
106
        'role' => 'http://www.w3.org/1999/xhtml/vocab#role'
107
    ];
108
109
    /**
110
     * Registered vocabularies
111
     *
112
     * @var array
113
     */
114
    protected $vocabularies;
115
116
    /**
117
     * Current default vocabulary
118
     *
119
     * @var VocabularyInterface
120
     */
121
    protected $defaultVocabulary = null;
122
123
    /**
124
     * Parent thing
125
     *
126
     * @var ThingInterface
127
     */
128
    protected $parentThing;
129
130
    /**
131
     * Context constructor
132
     */
133 17
    public function __construct()
134
    {
135 17
        $this->parentThing = new Thing('Null', new NullVocabulary());
136 17
        $this->vocabularies = self::$defaultVocabularies;
137 17
    }
138
139
    /**
140
     * Return the registered child things
141
     *
142
     * @return ThingInterface[] Child things
143
     */
144 5
    public function getChildren()
145
    {
146 5
        return $this->parentThing->getChildren();
147
    }
148
149
    /**
150
     * Register a vocabulary and its prefix
151
     *
152
     * @param string $prefix Vocabulary prefix
153
     * @param string $uri Vocabulary URI
154
     * @return Context New context
155
     *
156
     */
157 9
    public function registerVocabulary($prefix, $uri)
158
    {
159 9
        $prefix = self::validateVocabPrefix($prefix);
160 8
        $uri = (new VocabularyService())->validateVocabularyUri($uri);
161
162
        // Register the new URI
163 7
        if (empty($this->vocabularies[$prefix]) || ($this->vocabularies[$prefix] !== $uri)) {
164 7
            $context = clone $this;
165 7
            $context->vocabularies[$prefix] = $uri;
166 7
            return $context;
167
        }
168
169 1
        return $this;
170
    }
171
172
    /**
173
     * Validata a vocabulary prefix
174
     *
175
     * @param string $prefix Vocabulary prefix
176
     * @return string Valid vocabulary prefix
177
     * @throws RuntimeException If the vocabulary prefix is invalid
178
     */
179 11
    protected static function validateVocabPrefix($prefix)
180
    {
181 11
        $prefix = trim($prefix);
182
183
        // If the vocabulary prefix is invalid
184 11
        if (!strlen($prefix)) {
185 1
            throw new RuntimeException(
186 1
                sprintf(RuntimeException::INVALID_VOCABULARY_PREFIX_STR, $prefix),
187
                RuntimeException::INVALID_VOCABULARY_PREFIX
188 1
            );
189
        }
190
191 10
        return $prefix;
192
    }
193
194
    /**
195
     * Return a particular vocabulary
196
     *
197
     * @param string $prefix Vocabulary Prefix
198
     * @return VocabularyInterface Vocabulary
199
     * @throws OutOfBoundsException If the prefix has not been registered
200
     */
201 7
    public function getVocabulary($prefix)
202
    {
203 7
        $prefix = self::validateVocabPrefix($prefix);
204
205
        // If the prefix has not been registered
206 7
        if (empty($this->vocabularies[$prefix])) {
207 3
            throw new OutOfBoundsException(
208 3
                sprintf(OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX_STR, $prefix),
209
                OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX
210 3
            );
211
        }
212
213 4
        return new Vocabulary($this->vocabularies[$prefix]);
214
    }
215
216
    /**
217
     * Return whether a particular vocabulary prefix has been registered
218
     *
219
     * @param string $prefix Vocabulary prefix
220
     * @return bool Whether the prefix has been registered
221
     */
222 2
    public function hasVocabulary($prefix)
223
    {
224 2
        return !empty($this->vocabularies[self::validateVocabPrefix($prefix)]);
225
    }
226
227
    /**
228
     * Return the current default vocabulary
229
     *
230
     * @return VocabularyInterface Current default vocabulary
231
     */
232 7
    public function getDefaultVocabulary()
233
    {
234 7
        return $this->defaultVocabulary;
235
    }
236
237
    /**
238
     * Set the default vocabulary by URI
239
     *
240
     * @param VocabularyInterface $vocabulary Current default vocabulary
241
     * @return Context Self reference
242
     */
243 7
    public function setDefaultVocabulary(VocabularyInterface $vocabulary)
244
    {
245
        // If the new default vocabulary differs from the current one
246 7
        if ($this->defaultVocabulary !== $vocabulary) {
247 7
            $context = clone $this;
248 7
            $context->defaultVocabulary = $vocabulary;
249 7
            return $context;
250
        }
251
252 1
        return $this;
253
    }
254
255
    /**
256
     * Get the current parent thing
257
     *
258
     * @return ThingInterface|null Parent thing
259
     */
260 5
    public function getParentThing()
261
    {
262 5
        return $this->parentThing;
263
    }
264
265
    /**
266
     * Set the parent thing
267
     *
268
     * @param ThingInterface $parentThing Parent thing
269
     * @return Context
270
     */
271 6
    public function setParentThing(ThingInterface $parentThing)
272
    {
273
        // If the new parent thing differs from the current one
274 6
        if ($this->parentThing !== $parentThing) {
275 6
            $context = clone $this;
276 6
            $context->parentThing = $parentThing;
277 6
            return $context;
278
        }
279
280 1
        return $this;
281
    }
282
283
    /**
284
     * Add a child thing
285
     *
286
     * @param ThingInterface $thing Child thing
287
     * @return Context Self reference
288
     */
289 5
    public function addChild(ThingInterface $thing)
290
    {
291 5
        $this->parentThing->addChild($thing);
292 5
        return $this;
293
    }
294
}
295