Completed
Push — master ( 3788c5...9f114c )
by Osma
02:06
created

ConceptMappingPropertyValue::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 5
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class for handling concept property values.
5
 */
6
class ConceptMappingPropertyValue extends VocabularyDataObject
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
51
        $label = $this->getResourceLabel($this->resource, $lang);
52
        if ($label) {
53
            return $label;
54
        }
55
56
        // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping
57
        $exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId());
58
59
        // if the resource is from another vocabulary known by the skosmos instance
60
        if ($exvocab) {
61
            $label = $this->getExternalLabel($exvocab, $this->getUri(), $lang) ? $this->getExternalLabel($exvocab, $this->getUri(), $lang) : $this->getExternalLabel($exvocab, $this->getUri(), $exvocab->getConfig()->getDefaultLanguage());
62
            if ($label) {
63
                return $label;
64
            }
65
        }
66
67
        // using URI as label if nothing else has been found.
68
        $label = $this->resource->shorten() ? $this->resource->shorten() : $this->resource->getUri();
69
        return $label;
70
    }
71
72
    private function getResourceLabel($res, $lang = '') {
73
74
        if ($this->clang) {
75
            $lang = $this->clang;
76
        }
77
78
        if ($res->label($lang) !== null) { // current language
79
            return $res->label($lang);
80
        } elseif ($res->label() !== null) { // any language
81
            return $res->label();
82
        } elseif ($res->getLiteral('rdf:value', $lang) !== null) { // current language
83
            return $res->getLiteral('rdf:value', $lang);
84
        } elseif ($res->getLiteral('rdf:value') !== null) { // any language
85
            return $res->getLiteral('rdf:value');
86
        }
87
        return null;
88
    }
89
90
    public function getUri()
91
    {
92
        return $this->resource->getUri();
93
    }
94
95
    public function getExVocab()
96
    {
97
        $exvocab = $this->model->guessVocabularyFromURI($this->getUri(), $this->vocab->getId());
98
        return $exvocab;
99
    }
100
101
    public function getVocab()
102
    {
103
        return $this->vocab;
104
    }
105
106
    public function getVocabName($lang = '')
107
    {
108
109
        if ($this->clang) {
110
            $lang = $this->clang;
111
        }
112
113
        // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping
114
        $exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId());
115
        if ($exvocab) {
116
            return $exvocab->getTitle($lang);
117
        }
118
119
        // @codeCoverageIgnoreStart
120
        $scheme = $this->resource->get('skos:inScheme');
121
        if ($scheme) {
122
            $schemeResource = $this->model->getResourceFromUri($scheme->getUri());
123
            if ($schemeResource) {
124
                $schemaName = $this->getResourceLabel($schemeResource);
125
                if ($schemaName) {
126
                    //var_dump($schemaName);
127
                    return $schemaName;
128
                }
129
            }
130
        }
131
        // got a label for the concept, but not the scheme - use the host name as scheme label
132
        return parse_url($this->resource->getUri(), PHP_URL_HOST);
133
        // @codeCoverageIgnoreEnd
134
    }
135
136
    public function isExternal() {
137
        $propertyUris = $this->resource->propertyUris();
138
        return empty($propertyUris);
139
    }
140
141
    public function getNotation()
142
    {
143
        if ($this->resource->get('skos:notation')) {
144
            return $this->resource->get('skos:notation')->getValue();
145
        }
146
147
        $exvocab = $this->getExvocab();
148
149
        // if the resource is from a another vocabulary known by the skosmos instance
150
        if ($exvocab) {
151
            return $this->getExternalNotation($exvocab, $this->getUri());
152
        }
153
        return null;
154
    }
155
156
}
157
158