Completed
Push — master ( 8ccf5d...da3279 )
by Joschi
03:02
created

Context::setDefaultVocabulary()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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