Completed
Push — master ( 5284fc...9f77ec )
by Henri
03:01
created

ConceptMappingPropertyValue::getVocabName()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 8.8571
cc 5
eloc 10
nc 4
nop 0
1
<?php
2
3
/**
4
 * Class for handling concept property values.
5
 */
6
class ConceptMappingPropertyValue extends VocabularyDataObject
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
    /** property type */
9
    private $type;
10
    private $clang;
11
    private $labelcache;
12
13
    public function __construct($model, $vocab, $resource, $prop, $clang = '')
14
    {
15
        parent::__construct($model, $vocab, $resource);
16
        $this->type = $prop;
17
        $this->clang = $clang;
18
        $this->labelcache = array();
19
    }
20
21
    public function __toString()
22
    {
23
        $label = $this->getLabel();
24
        $notation = $this->getNotation();
25
        return $notation . $label;
26
    }
27
28
    public function getType()
29
    {
30
        return $this->type;
31
    }
32
33
    public function getLabel($lang = '')
34
    {
35
        if (isset($this->labelcache[$lang])) {
36
            return $this->labelcache[$lang];
37
        }
38
39
        $label = $this->queryLabel($lang);
40
        $this->labelcache[$lang] = $label;
41
        return $label;
42
    }
43
44
    private function queryLabel($lang)
45
    {
46
        if ($this->clang) {
47
            $lang = $this->clang;
48
        }
49
50
        $exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri());
51
52
        if ($this->resource->label($lang) !== null) { // current language
53
            return $this->resource->label($lang);
54
        } elseif ($this->resource->label() !== null) { // any language
55
            return $this->resource->label();
56
        } elseif ($this->resource->getLiteral('rdf:value', $lang) !== null) { // current language
57
            return $this->resource->getLiteral('rdf:value', $lang);
58
        } elseif ($this->resource->getLiteral('rdf:value') !== null) { // any language
59
            return $this->resource->getLiteral('rdf:value');
60
        }
61
62
        // if the resource is from a another vocabulary known by the skosmos instance
63
        if ($exvocab) {
64
            $label = $this->getExternalLabel($exvocab, $this->getUri(), $exvocab->getConfig()->getDefaultLanguage());
65
            if ($label) {
66
                return $label;
67
            }
68
69
        }
70
71
        // using URI as label if nothing else has been found.
72
        $label = $this->resource->shorten() ? $this->resource->shorten() : $this->resource->getUri();
73
        return $label;
74
    }
75
76
    public function getUri()
77
    {
78
        return $this->resource->getUri();
79
    }
80
81
    public function getExVocab()
82
    {
83
        $exvocab = $this->model->guessVocabularyFromURI($this->getUri());
84
        return $exvocab;
85
    }
86
87
    public function getVocab()
88
    {
89
        return $this->vocab;
90
    }
91
92
    public function getVocabName()
93
    {
94
        $exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri());
95
        if ($exvocab) {
96
            return $exvocab->getTitle();
97
        }
98
        // @codeCoverageIgnoreStart
99
        $scheme = $this->resource->get('skos:inScheme');
100
        if ($scheme) {
101
            $schemeResource = $this->model->getResourceFromUri($scheme->getUri());
102
            if ($schemeResource && $schemeResource->label()) {
103
                return $schemeResource->label()->getValue();
104
            }
105
        }
106
        // got a label for the concept, but not the scheme - use the host name as scheme label
107
        return parse_url($this->resource->getUri(), PHP_URL_HOST);
108
        // @codeCoverageIgnoreEnd
109
    }
110
111
    public function getNotation()
112
    {
113
        if ($this->resource->get('skos:notation')) {
114
            return $this->resource->get('skos:notation')->getValue();
115
        }
116
    }
117
118
}
119