Completed
Push — master ( a2b3dc...439a57 )
by Henri
14s
created

ConceptMappingPropertyValue::getResourceLabel()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 12
nc 10
nop 2
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 View Code Duplication
    public function __construct($model, $vocab, $resource, $prop, $clang = '')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 getNotation()
137
    {
138
        if ($this->resource->get('skos:notation')) {
139
            return $this->resource->get('skos:notation')->getValue();
140
        }
141
142
        $exvocab = $this->getExvocab();
143
144
        // if the resource is from a another vocabulary known by the skosmos instance
145
        if ($exvocab) {
146
            return $this->getExternalNotation($exvocab, $this->getUri());
147
        }
148
        return null;
149
    }
150
151
}
152
153