Completed
Push — master ( c78c30...f35f13 )
by Joschi
02:42
created

RdfaLiteContext   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 6
dl 0
loc 168
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

6 Methods

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