Passed
Push — main ( 1b906f...9e02d7 )
by Osma
03:19 queued 13s
created

ConceptMappingPropertyValue::getSortKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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 = '', $queryExVocabs = true)
0 ignored issues
show
Unused Code introduced by
The parameter $queryExVocabs is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

47
    public function getLabel($lang = '', /** @scrutinizer ignore-unused */ $queryExVocabs = true)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

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