Completed
Push — master ( 3f4f64...36b546 )
by Henri
03:58
created

VocabularyConfig::getShowStatistics()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * VocabularyConfig provides access to the vocabulary configuration defined in vocabularies.ttl.
5
 */
6
class VocabularyConfig extends DataObject
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    public function __construct($resource) 
9
    {
10
      $this->resource = $resource;
11
    }
12
13
    /**
14
     * Returns a boolean value based on a literal value from the vocabularies.ttl configuration.
15
     * @param string $property the property to query
16
     * @param boolean $default the default value if the value is not set in configuration
17
     */
18
    private function getBoolean($property, $default = false)
19
    {
20
        $val = $this->resource->getLiteral($property);
21
        if ($val) {
22
            return filter_var($val->getValue(), FILTER_VALIDATE_BOOLEAN);
23
        }
24
25
        return $default;
26
    }
27
    
28
    /**
29
     * Returns a boolean value based on a literal value from the vocabularies.ttl configuration.
30
     * @param string $property the property to query
31
     * @param string $lang preferred language for the literal,
32
     */
33
    private function getLiteral($property, $lang=null)
34
    {
35
        if (!isset($lang)) {;
36
            $lang = $this->getEnvLang();
37
        }
38
39
        $literal = $this->resource->getLiteral($property, $lang);
40
        if ($literal) {
41
            return $literal->getValue();
42
        }
43
44
        // not found with selected language, try any language
45
        $literal = $this->resource->getLiteral($property);
46
        if ($literal)
47
          return $literal->getValue();
48
    }
49
50
    /**
51
     * Get the default language of this vocabulary
52
     * @return string default language, e.g. 'en'
53
     */
54
55
    public function getDefaultLanguage()
56
    {
57
        $deflang = $this->resource->getLiteral('skosmos:defaultLanguage');
58
        if ($deflang) {
59
            return $deflang->getValue();
60
        }
61
62
        $langs = $this->getLanguages();
63
        $deflang = reset($langs); // picking the first one from the list with reset since the keys are not numeric
64
        if (sizeof($langs) > 1) {
65
            trigger_error("Default language for vocabulary '" . $this->getShortName() . "' unknown, choosing '$deflang'.", E_USER_WARNING);
66
        }
67
68
        return $deflang;
69
    }
70
71
    /**
72
     * Wether the alphabetical index is small enough to be shown all at once.
73
     * @return boolean true if all concepts can be shown at once.
74
     */
75
    public function getAlphabeticalFull()
76
    {
77
        return $this->getBoolean('skosmos:fullAlphabeticalIndex');
78
    }
79
80
    /**
81
     * Returns a short name for a vocabulary if configured. If that has not been set
82
     * using vocabId as a fallback.
83
     * @return string
84
     */
85
    public function getShortName()
86
    {
87
        $shortname = $this->getLiteral('skosmos:shortName');
88
        if ($shortname) 
89
          return $shortname;
90
91
        // if no shortname exists fall back to the id
92
        return $this->getId();
93
    }
94
95
    /**
96
     * Get the vocabulary feedback e-mail address and return it.
97
     *
98
     * @return string e-mail address or null if not defined.
99
     */
100
    public function getFeedbackRecipient()
101
    {
102
        $email = $this->resource->get('skosmos:feedbackRecipient');
103
        return isset($email) ? $email->getValue() : null;
104
    }
105
106
    /**
107
     * Returns the human readable vocabulary title.
108
     * @return string the title of the vocabulary
109
     */
110
    public function getTitle($lang = null)
111
    {
112
        return $this->getLiteral('dc:title', $lang);
113
    }
114
115
    /**
116
     * Returns a boolean value set in the vocabularies.ttl config.
117
     * @return boolean
118
     */
119
    public function sortByNotation()
120
    {
121
        return $this->getBoolean('skosmos:sortByNotation');
122
    }
123
    
124
    /**
125
     * Returns a boolean value set in the vocabularies.ttl config.
126
     * @return boolean
127
     */
128
    public function showChangeList()
129
    {
130
        return $this->getBoolean('skosmos:showChangeList');
131
    }
132
133
    /**
134
     * get the URLs from which the vocabulary data can be downloaded
135
     * @return array Array with MIME type as key, URL as value
136
     */
137
    public function getDataURLs()
138
    {
139
        $ret = array();
140
        $urls = $this->resource->allResources("void:dataDump");
141
        foreach ($urls as $url) {
142
            // first try dc:format and dc11:format
143
            $mimetypelit = $url->getLiteral('dc:format');
144
            if ($mimetypelit === null) {
145
                $mimetypelit = $url->getLiteral('dc11:format');
146
            }
147
148
            // if still not found, guess MIME type using file extension
149
            if ($mimetypelit !== null) {
150
                $mimetype = $mimetypelit->getValue();
151
            } else {
152
                $format = EasyRdf_Format::guessFormat(null, $url->getURI());
153
                if ($format === null) {
154
                    trigger_error("Could not guess format for <$url>.", E_USER_WARNING);
155
                    continue;
156
                }
157
                $mimetypes = array_keys($format->getMimeTypes());
158
                $mimetype = $mimetypes[0];
159
            }
160
            $ret[$mimetype] = $url->getURI();
161
        }
162
        return $ret;
163
    }
164
165
    /**
166
     * Returns the class URI used for concept groups in this vocabulary,
167
     * or null if not set.
168
     * @return string group class URI or null
169
     */
170
171
    public function getGroupClassURI()
172
    {
173
        $val = $this->resource->getResource("skosmos:groupClass");
174
        if ($val) {
175
            return $val->getURI();
176
        }
177
178
        return null;
179
    }
180
181
    /**
182
     * Returns the class URI used for thesaurus arrays in this vocabulary,
183
     * or null if not set.
184
     * @return string array class URI or null
185
     */
186
187
    public function getArrayClassURI()
188
    {
189
        $val = $this->resource->getResource("skosmos:arrayClass");
190
        if ($val) {
191
            return $val->getURI();
192
        }
193
194
        return null;
195
    }
196
197
    /**
198
     * Returns custom properties displayed on the search page if configured.
199
     * @return string array class URI or null
200
     */
201
202
    public function getAdditionalSearchProperties()
203
    {
204
        $resources = $this->resource->allResources("skosmos:showPropertyInSearch");
205
        $ret = array();
206 View Code Duplication
        foreach ($resources as $res) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
207
            $prop = $res->getURI();
208
            if (EasyRdf_Namespace::shorten($prop) !== null) // shortening property labels if possible
209
            {
210
                $prop = EasyRdf_Namespace::shorten($prop);
211
            }
212
213
            $ret[] = $prop;
214
        }
215
        return $ret;
216
    }
217
218
    /**
219
     * Queries whether the property should be shown with all the label language variations.
220
     * @param string $property
221
     * @return boolean
222
     */
223
    public function hasMultiLingualProperty($property)
224
    {
225
        $resources = $this->resource->allResources("skosmos:hasMultiLingualProperty");
226 View Code Duplication
        foreach ($resources as $res) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
227
            $prop = $res->getURI();
228
            if (EasyRdf_Namespace::shorten($prop) !== null) // shortening property labels if possible
229
            {
230
                $prop = EasyRdf_Namespace::shorten($prop);
231
            }
232
233
            if ($prop === $property) {
234
                return true;
235
            }
236
237
        }
238
        return false;
239
    }
240
241
    /**
242
     * Returns a boolean value set in the vocabularies.ttl config.
243
     * @return boolean
244
     */
245
    public function getShowHierarchy()
246
    {
247
        return $this->getBoolean('skosmos:showTopConcepts');
248
    }
249
250
    /**
251
     * Returns a boolean value set in the vocabularies.ttl config.
252
     * @return boolean
253
     */
254
    public function showConceptSchemesInHierarchy()
255
    {
256
        return $this->getBoolean('skosmos:conceptSchemesInHierarchy');
257
    }
258
259
    /**
260
     * Returns a boolean value set in the vocabularies.ttl config.
261
     * @return boolean defaults to true if fetching hasn't been explicitly denied.
262
     */
263
    public function getExternalResourcesLoading()
264
    {
265
        return $this->getBoolean('skosmos:loadExternalResources', true);
266
    }
267
268
    /**
269
     * Returns a boolean value set in the vocabularies.ttl config.
270
     * @return boolean
271
     */
272
    public function getShowLangCodes()
273
    {
274
        return $this->getBoolean('skosmos:explicitLanguageTags');
275
    }
276
277
    /**
278
     * Returns a boolean value set in the vocabularies.ttl config.
279
     * @return array array of concept class URIs (can be empty)
280
     */
281
    public function getIndexClasses()
282
    {
283
        $resources = $this->resource->allResources("skosmos:indexShowClass");
284
        $ret = array();
285
        foreach ($resources as $res) {
286
            $ret[] = $res->getURI();
287
        }
288
        return $ret;
289
    }
290
291
    /**
292
     * Get the languages supported by this vocabulary
293
     * @return array languages supported by this vocabulary (as language tag strings)
294
     */
295
    public function getLanguages()
296
    {
297
        $langs = $this->resource->allLiterals('skosmos:language');
298
        $ret = array();
299
        foreach ($langs as $lang) {
300
            $langlit = Punic\Language::getName($lang->getValue(), $this->getEnvLang());
301
            $ret[$langlit] = $lang->getValue();
302
        }
303
        ksort($ret);
304
305
        return $ret;
306
    }
307
308
    /**
309
     * Returns the vocabulary default sidebar view.
310
     * @return string name of the view
311
     */
312
    public function getDefaultSidebarView()
313
    {
314
        $defview = $this->resource->getLiteral('skosmos:defaultSidebarView');
315
        if ($defview) {
316
            $value = $defview->getValue();
317
            if ($value === 'groups' || $value === 'hierarchy') {
318
                return $value;
319
            }
320
321
        }
322
        return 'alphabetical'; // if not defined displaying the alphabetical index
323
    }
324
325
    /**
326
     * Extracts the vocabulary id string from the baseuri of the vocabulary.
327
     * @return string identifier eg. 'mesh'.
328
     */
329
    public function getId()
330
    {
331
        $uriparts = explode("#", $this->resource->getURI());
332
        if (count($uriparts) != 1)
333
        // hash namespace
334
        {
335
            return $uriparts[1];
336
        }
337
338
        // slash namespace
339
        $uriparts = explode("/", $this->resource->getURI());
340
341
        return $uriparts[count($uriparts) - 1];
342
    }
343
344
    public function getShowStatistics() {
345
        return $this->getBoolean('skosmos:showStatistics', true);
346
    }
347
}
348