Completed
Push — master ( f17f8e...3aa6f6 )
by Joschi
02:50
created

Context::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
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 $defaultVocabs = [
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 $vocabs;
112
113
    /**
114
     * Context constructor
115
     */
116 5
    public function __construct()
117
    {
118 5
        $this->vocabs = self::$defaultVocabs;
119 5
    }
120
121
    /**
122
     * Register a vocabulary and its prefix
123
     *
124
     * @param string $prefix Vocabulary prefix
125
     * @param string $uri Vocabulary URI
126
     * @return Context New context
127
     *
128
     */
129 3
    public function registerVocabulary($prefix, $uri)
130
    {
131 3
        $prefix = self::validateVocabPrefix($prefix);
132 2
        $uri = Vocabulary::validateVocabUri($uri);
133
134
        // Register the new URI
135 1
        if (empty($this->vocabs[$prefix]) || ($this->vocabs[$prefix] !== $uri)) {
136 1
            $context = clone $this;
137 1
            $context->vocabs[$prefix] = $uri;
138 1
            return $context;
139
        }
140
141 1
        return $this;
142
    }
143
144
    /**
145
     * Validata a vocabulary prefix
146
     *
147
     * @param string $prefix Vocabulary prefix
148
     * @return string Valid vocabulary prefix
149
     * @throws RuntimeException If the vocabulary prefix is invalid
150
     */
151 5
    protected static function validateVocabPrefix($prefix)
152
    {
153 5
        $prefix = trim($prefix);
154
155
        // If the vocabulary prefix is invalid
156 5
        if (!strlen($prefix)) {
157 1
            throw new RuntimeException(
158 1
                sprintf(RuntimeException::INVALID_VOCABULARY_PREFIX_STR, $prefix),
159
                RuntimeException::INVALID_VOCABULARY_PREFIX
160 1
            );
161
        }
162
163 4
        return $prefix;
164
    }
165
166
    /**
167
     * Return a particular vocabulary
168
     *
169
     * @param string $prefix Vocabulary Prefix
170
     * @return VocabularyInterface Vocabulary
171
     * @throws OutOfBoundsException If the prefix has not been registered
172
     */
173 3
    public function getVocabulary($prefix)
174
    {
175 3
        $prefix = self::validateVocabPrefix($prefix);
176
177
        // If the prefix has not been registered
178 3
        if (empty($this->vocabs[$prefix])) {
179 1
            throw new OutOfBoundsException(
180 1
                sprintf(OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX_STR, $prefix),
181
                OutOfBoundsException::UNKNOWN_VOCABULARY_PREFIX
182 1
            );
183
        }
184
185 2
        return new Vocabulary($this->vocabs[$prefix]);
186
    }
187
188
    /**
189
     * Return whether a particular vocabulary prefix has been registered
190
     *
191
     * @param string $prefix Vocabulary prefix
192
     * @return bool Whether the prefix has been registered
193
     */
194 2
    public function hasVocabulary($prefix)
195
    {
196 2
        return !empty($this->vocabs[self::validateVocabPrefix($prefix)]);
197
    }
198
}
199