RdfaLiteContext::getVocabulary()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 1
crap 2
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\Context;
38
39
use Jkphl\RdfaLiteMicrodata\Application\Exceptions\OutOfBoundsException;
40
use Jkphl\RdfaLiteMicrodata\Application\Exceptions\RuntimeException;
41
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\Vocabulary;
42
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyInterface;
43
use Jkphl\RdfaLiteMicrodata\Domain\Vocabulary\VocabularyService;
44
45
/**
46
 * RDFa Lite parsing context
47
 *
48
 * @package Jkphl\RdfaLiteMicrodata
49
 * @subpackage Jkphl\RdfaLiteMicrodata\Application
50
 */
51
class RdfaLiteContext extends AbstractContext
52
{
53
    /**
54
     * Default vocabularies and their prefixes
55
     *
56
     * @var array
57
     * @see https://www.w3.org/2011/rdfa-context/rdfa-1.1
58
     * @link https://www.w3.org/2013/json-ld-context/rdfa11
59
     */
60
    protected static $defaultVocabularies = [
61
        'cat' => 'http://www.w3.org/ns/dcat#',
62
        'qb' => 'http://purl.org/linked-data/cube#',
63
        'grddl' => 'http://www.w3.org/2003/g/data-view#',
64
        'ma' => 'http://www.w3.org/ns/ma-ont#',
65
        'owl' => 'http://www.w3.org/2002/07/owl#',
66
        'rdf' => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
67
        'rdfa' => 'http://www.w3.org/ns/rdfa#',
68
        'rdfs' => 'http://www.w3.org/2000/01/rdf-schema#',
69
        'rif' => 'http://www.w3.org/2007/rif#',
70
        'rr' => 'http://www.w3.org/ns/r2rml#',
71
        'skos' => 'http://www.w3.org/2004/02/skos/core#',
72
        'skosxl' => 'http://www.w3.org/2008/05/skos-xl#',
73
        'wdr' => 'http://www.w3.org/2007/05/powder#',
74
        'void' => 'http://rdfs.org/ns/void#',
75
        'wdrs' => 'http://www.w3.org/2007/05/powder-s#',
76
        'xhv' => 'http://www.w3.org/1999/xhtml/vocab#',
77
        'xml' => 'http://www.w3.org/XML/1998/namespace',
78
        'xsd' => 'http://www.w3.org/2001/XMLSchema#',
79
        'prov' => 'http://www.w3.org/ns/prov#',
80
        'sd' => 'http://www.w3.org/ns/sparql-service-description#',
81
        'org' => 'http://www.w3.org/ns/org#',
82
        'gldp' => 'http://www.w3.org/ns/people#',
83
        'cnt' => 'http://www.w3.org/2008/content#',
84
        'dcat' => 'http://www.w3.org/ns/dcat#',
85
        'earl' => 'http://www.w3.org/ns/earl#',
86
        'ht' => 'http://www.w3.org/2006/http#',
87
        'ptr' => 'http://www.w3.org/2009/pointers#',
88
        'cc' => 'http://creativecommons.org/ns#',
89
        'ctag' => 'http://commontag.org/ns#',
90
        'dc' => 'http://purl.org/dc/terms/',
91
        'dc11' => 'http://purl.org/dc/elements/1.1/',
92
        'dcterms' => 'http://purl.org/dc/terms/',
93
        'foaf' => 'http://xmlns.com/foaf/0.1/',
94
        'gr' => 'http://purl.org/goodrelations/v1#',
95
        'ical' => 'http://www.w3.org/2002/12/cal/icaltzd#',
96
        'og' => 'http://ogp.me/ns#',
97
        'rev' => 'http://purl.org/stuff/rev#',
98
        'sioc' => 'http://rdfs.org/sioc/ns#',
99
        'v' => 'http://rdf.data-vocabulary.org/#',
100
        'vcard' => 'http://www.w3.org/2006/vcard/ns#',
101
        'schema' => 'http://schema.org/',
102
        'describedby' => 'http://www.w3.org/2007/05/powder-s#describedby',
103
        'license' => 'http://www.w3.org/1999/xhtml/vocab#license',
104
        'role' => 'http://www.w3.org/1999/xhtml/vocab#role'
105
    ];
106
107
    /**
108
     * Registered vocabularies
109
     *
110
     * @var array
111
     */
112
    protected $vocabularies;
113
114
    /**
115
     * Context constructor
116
     */
117 23
    public function __construct()
118
    {
119 23
        parent::__construct();
120 23
        $this->vocabularies = self::$defaultVocabularies;
121 23
    }
122
123
    /**
124
     * Register a vocabulary and its prefix
125
     *
126
     * @param string $prefix Vocabulary prefix
127
     * @param string $uri Vocabulary URI
128
     * @return RdfaLiteContext New context
129
     *
130
     */
131 11
    public function registerVocabulary($prefix, $uri)
132
    {
133 11
        $prefix = self::validateVocabPrefix($prefix);
134 10
        $uri = (new VocabularyService())->validateVocabularyUri($uri);
135
136
        // Register the new URI
137 9
        if (empty($this->vocabularies[$prefix]) || ($this->vocabularies[$prefix] !== $uri)) {
138 9
            $context = clone $this;
139 9
            $context->vocabularies[$prefix] = $uri;
140 9
            return $context;
141
        }
142
143 1
        return $this;
144
    }
145
146
    /**
147
     * Validata a vocabulary prefix
148
     *
149
     * @param string $prefix Vocabulary prefix
150
     * @return string Valid vocabulary prefix
151
     * @throws RuntimeException If the vocabulary prefix is invalid
152
     */
153 13
    protected static function validateVocabPrefix($prefix)
154
    {
155 13
        $prefix = trim($prefix);
156
157
        // If the vocabulary prefix is invalid
158 13
        if (!strlen($prefix)) {
159 1
            throw new RuntimeException(
160 1
                sprintf(RuntimeException::INVALID_VOCABULARY_PREFIX_STR, $prefix),
161
                RuntimeException::INVALID_VOCABULARY_PREFIX
162 1
            );
163
        }
164
165 12
        return $prefix;
166
    }
167
168
    /**
169
     * Return a particular vocabulary
170
     *
171
     * @param string $prefix Vocabulary Prefix
172
     * @return VocabularyInterface Vocabulary
173
     * @throws OutOfBoundsException If the prefix has not been registered
174
     */
175 8
    public function getVocabulary($prefix)
176
    {
177 8
        $prefix = self::validateVocabPrefix($prefix);
178
179
        // If the prefix has not been registered
180 8
        if (empty($this->vocabularies[$prefix])) {
181 4
            throw new OutOfBoundsException(
182 4
                sprintf(OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX_STR, $prefix),
183
                OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX
184 4
            );
185
        }
186
187 4
        return new Vocabulary($this->vocabularies[$prefix]);
188
    }
189
190
    /**
191
     * Return whether a particular vocabulary prefix has been registered
192
     *
193
     * @param string $prefix Vocabulary prefix
194
     * @return bool Whether the prefix has been registered
195
     */
196 2
    public function hasVocabulary($prefix)
197
    {
198 2
        return !empty($this->vocabularies[self::validateVocabPrefix($prefix)]);
199
    }
200
201
    /**
202
     * Set the default vocabulary by URI
203
     *
204
     * @param VocabularyInterface $vocabulary Current default vocabulary
205
     * @return RdfaLiteContext Self reference
206
     */
207 11
    public function setDefaultVocabulary(VocabularyInterface $vocabulary)
208
    {
209
        // If the new default vocabulary differs from the current one
210 11
        if ($this->defaultVocabulary !== $vocabulary) {
211 11
            $context = clone $this;
212 11
            $context->defaultVocabulary = $vocabulary;
213 11
            return $context;
214
        }
215
216 1
        return $this;
217
    }
218
}
219