ConceptMappingPropertyValue::getResourceLabel()   A
last analyzed

Complexity

Conditions 6
Paths 10

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 17
rs 9.2222
c 0
b 0
f 0
cc 6
nc 10
nop 2
1
<?php
2
3
use EasyRdf\Resource;
4
5
/**
6
 * Class for handling concept property values.
7
 */
8
class ConceptMappingPropertyValue extends VocabularyDataObject
9
{
10
    /** property type */
11
    private $type;
12
    private $source;
13
    private $clang;
14
    private $labelcache;
15
16
    /**
17
     * ConceptMappingPropertyValue constructor.
18
     *
19
     * @param Model $model
20
     * @param Vocabulary $vocab  Target vocabulary
21
     * @param Resource $target   Target concept resource
22
     * @param Resource $source   Source concept resource
23
     * @param string $prop       Mapping property
24
     * @param ?string $clang     Preferred label language (nullable)
25
     */
26
    public function __construct(Model $model, Vocabulary $vocab, Resource $target, Resource $source, string $prop, $clang = '')
27
    {
28
        parent::__construct($model, $vocab, $target);
29
        $this->source = $source;
30
        $this->type = $prop;
31
        $this->clang = $clang;
32
        $this->labelcache = array();
33
    }
34
35
    public function __toString()
36
    {
37
        $label = $this->getLabel();
38
        $notation = $this->getNotation();
39
        return ltrim($notation . ' ') . $label;
40
    }
41
42
    public function getType()
43
    {
44
        return $this->type;
45
    }
46
47
    public function getLabel($lang = '', $allowExternal = true)
48
    {
49
        $key = $lang . "/" . ($allowExternal ? "true" : "false");
50
        if (isset($this->labelcache[$key])) {
51
            return $this->labelcache[$key];
52
        }
53
54
        $label = $this->queryLabel($lang, $allowExternal);
55
        $this->labelcache[$key] = $label;
56
        return $label;
57
    }
58
59
    public function getSortKey()
60
    {
61
        return strtolower($this->getVocabName() . ": " . $this->getLabel());
62
    }
63
64
    private function queryLabel($lang = '', $allowExternal = true)
65
    {
66
        if ($this->clang) {
67
            $lang = $this->clang;
68
        }
69
70
71
        $label = $this->getResourceLabel($this->resource, $lang);
72
        if ($label) {
73
            return $label;
74
        }
75
76
        // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping
77
        $exvocab = $allowExternal ? $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId()) : null;
78
79
        // if the resource is from another vocabulary known by the skosmos instance
80
        if ($exvocab) {
81
            $label = $this->getExternalLabel($exvocab, $this->getUri(), $lang) ? $this->getExternalLabel($exvocab, $this->getUri(), $lang) : $this->getExternalLabel($exvocab, $this->getUri(), $exvocab->getConfig()->getDefaultLanguage());
82
            if ($label) {
0 ignored issues
show
introduced by
$label is of type EasyRdf\Literal, thus it always evaluated to true.
Loading history...
83
                return $label;
84
            }
85
        }
86
87
        // using URI as label if nothing else has been found.
88
        return $this->resource->shorten() ? $this->resource->shorten() : $this->resource->getUri();
89
    }
90
91
    private function getResourceLabel($res, $lang = '')
92
    {
93
94
        if ($this->clang) {
95
            $lang = $this->clang;
96
        }
97
98
        if ($res->label($lang) !== null) { // current language
99
            return $res->label($lang);
100
        } elseif ($res->label() !== null) { // any language
101
            return $res->label();
102
        } elseif ($res->getLiteral('rdf:value', $lang) !== null) { // current language
103
            return $res->getLiteral('rdf:value', $lang);
104
        } elseif ($res->getLiteral('rdf:value') !== null) { // any language
105
            return $res->getLiteral('rdf:value');
106
        }
107
        return null;
108
    }
109
110
    public function getUri()
111
    {
112
        return $this->resource->getUri();
113
    }
114
115
    public function getExVocab()
116
    {
117
        return $this->model->guessVocabularyFromURI($this->getUri(), $this->vocab->getId());
118
    }
119
120
    public function getVocab()
121
    {
122
        return $this->vocab;
123
    }
124
125
    public function getVocabName($lang = '')
126
    {
127
128
        if ($this->clang) {
129
            $lang = $this->clang;
130
        }
131
132
        // if multiple vocabularies are found, the following method will return in priority the current vocabulary of the mapping
133
        $exvocab = $this->model->guessVocabularyFromURI($this->resource->getUri(), $this->vocab->getId());
134
        if ($exvocab) {
0 ignored issues
show
introduced by
$exvocab is of type Vocabulary, thus it always evaluated to true.
Loading history...
135
            return $exvocab->getTitle($lang);
136
        }
137
138
        // @codeCoverageIgnoreStart
139
        $scheme = $this->resource->get('skos:inScheme');
140
        if ($scheme) {
141
            $schemeResource = $this->model->getResourceFromUri($scheme->getUri());
142
            if ($schemeResource) {
143
                $schemaName = $this->getResourceLabel($schemeResource);
144
                if ($schemaName) {
145
                    return $schemaName;
146
                }
147
            }
148
        }
149
        // got a label for the concept, but not the scheme - use the host name as scheme label
150
        return parse_url($this->resource->getUri(), PHP_URL_HOST);
151
        // @codeCoverageIgnoreEnd
152
    }
153
154
    public function getNotation()
155
    {
156
        if ($this->resource->get('skos:notation')) {
157
            return $this->resource->get('skos:notation')->getValue();
158
        }
159
160
        $exvocab = $this->getExvocab();
161
162
        // if the resource is from a another vocabulary known by the skosmos instance
163
        if ($exvocab) {
0 ignored issues
show
introduced by
$exvocab is of type Vocabulary, thus it always evaluated to true.
Loading history...
164
            return $this->getExternalNotation($exvocab, $this->getUri());
165
        }
166
        return null;
167
    }
168
169
    /**
170
     * Return the mapping as a JSKOS-compatible array.
171
     * @return array
172
     */
173
    public function asJskos($allowExternal = true, $lang = null, $hrefLink = null)
174
    {
175
        $propertyLabel = $this->getLabel($lang);
176
        $propertyLang = $lang;
177
        if (!is_string($propertyLabel)) {
178
            $propertyLang = $propertyLabel->getLang();
179
            $propertyLabel = $propertyLabel->getValue();
180
        }
181
        $ret = [
182
            // JSKOS
183
            'uri' => $this->source->getUri(),
184
            'notation' => $this->getNotation(),
185
            'type' => [$this->type],
186
            'prefLabel' => $propertyLabel,
187
            'from' => [
188
                'memberSet' => [
189
                    [
190
                        'uri' => (string) $this->source->getUri(),
191
                    ]
192
                ]
193
            ],
194
            'to' => [
195
                'memberSet' => [
196
                    [
197
                        'uri' => (string) $this->getUri()
198
                    ]
199
                ]
200
            ],
201
            // EXTRA
202
            'hrefLink' => $hrefLink, // link to resource as displayed in the UI
203
            'lang' => $propertyLang, // TBD: could it be part of the prefLabel?
204
            'vocabName' => (string) $this->getVocabName(), // vocabulary as displayed in the UI
205
            'typeLabel' => $this->model->getText($this->type), // a text used in the UI instead of, for example, skos:closeMatch
206
        ];
207
208
        $helpprop = $this->type . "_help";
209
        // see if we have a translation for the property help text
210
        $help = $this->model->getText($helpprop);
211
        if ($help != $helpprop) {
212
            $ret['description'] = $help;
213
        }
214
215
        $fromScheme = $this->vocab->getDefaultConceptScheme();
216
        if (isset($fromScheme)) {
217
            $ret['fromScheme'] = [
218
                'uri' => (string) $fromScheme,
219
            ];
220
        }
221
222
        $exvocab = $this->getExvocab();
223
        if (isset($exvocab)) {
224
            $ret['toScheme'] = [
225
                'uri' => (string) $exvocab->getDefaultConceptScheme(),
226
            ];
227
        }
228
229
        $notation = $this->getNotation();
230
        if (isset($notation)) {
231
            $ret['to']['memberSet'][0]['notation'] = (string) $notation;
232
        }
233
234
        $label = $this->getLabel($lang, $allowExternal);
235
        if (isset($label)) {
236
            if (is_string($label)) {
237
                list($labelLang, $labelValue) = ['', $label];
238
            } else {
239
                list($labelLang, $labelValue) = [$label->getLang(), $label->getValue()];
240
            }
241
            // set the language of the preferred label to be whatever returned
242
            $ret['lang'] = $labelLang;
243
244
            if ($labelValue != $this->getUri()) {
245
                // The `queryLabel()` method above will fallback to returning the URI
246
                // if no label was found. We don't want that here.
247
                $ret['to']['memberSet'][0]['prefLabel'] = [
248
                    $labelLang => $labelValue,
249
                ];
250
            }
251
        }
252
253
        return $ret;
254
    }
255
256
}
257