Issues (177)

model/ConceptMappingPropertyValue.php (4 issues)

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