Completed
Push — master ( bcbd9b...28b3c2 )
by Osma
09:04 queued 02:58
created

ConceptPropertyValue::getVocabName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class for handling concept property values.
5
 */
6
class ConceptPropertyValue extends VocabularyDataObject
7
{
8
    /** submembers */
9
    private $submembers;
10
    /** property type */
11
    private $type;
12
    /** content language */
13
    private $clang;
14
15
    public function __construct($model, $vocab, $resource, $prop, $clang = '')
16
    {
17
        parent::__construct($model, $vocab, $resource);
18
        $this->submembers = array();
19
        $this->type = $prop;
20
        $this->clang = $clang;
21
    }
22
23
    public function __toString()
24
    {
25
        $label = is_string($this->getLabel()) ? $this->getLabel() : $this->getLabel()->getValue();
26
        if ($this->vocab->getConfig()->sortByNotation()) {
27
            $label = $this->getNotation() . $label;
28
        }
29
30
        return $label;
31
    }
32
33
    public function getLang()
34
    {
35
        return $this->getEnvLang();
36
    }
37
38
    public function getLabel($lang = '', $fallback = 'uri')
39
    {
40
        if ($this->clang) {
41
            $lang = $this->clang;
42
        }
43
        if ($this->vocab->getConfig()->getLanguageOrder($lang)) {
44 View Code Duplication
            foreach ($this->vocab->getConfig()->getLanguageOrder($lang) as $fallback) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
45
                if ($this->resource->label($fallback) !== null) {
46
                    return $this->resource->label($fallback);
47
                }
48
                // We need to check all the labels in case one of them matches a subtag of the current language
49
                if ($this->resource->allLiterals('skos:prefLabel')) {
50
                    foreach($this->resource->allLiterals('skos:prefLabel') as $label) {
51
                        // the label lang code is a subtag of the UI lang eg. en-GB - create a new literal with the main language
52
                        if ($label !== null && strpos($label->getLang(), $fallback . '-') === 0) {
53
                            return EasyRdf\Literal::create($label, $fallback);
54
                        }
55
                    }
56
                }
57
            }
58
        }
59
60
        if ($this->resource->label($lang) !== null) { // current language
61
            return $this->resource->label($lang);
62
        } elseif ($this->resource->label($this->vocab->getConfig()->getDefaultLanguage()) !== null) { // vocab default language
63
            return $this->resource->label($this->vocab->getConfig()->getDefaultLanguage());
64
        } elseif ($this->resource->label() !== null) { // any language
65
            return $this->resource->label();
66
        } elseif ($this->resource->getLiteral('rdf:value', $lang) !== null) { // current language
67
            return $this->resource->getLiteral('rdf:value', $lang);
68
        } elseif ($this->resource->getLiteral('rdf:value') !== null) { // any language
69
            return $this->resource->getLiteral('rdf:value');
70
        }
71
72
        if ($fallback == 'uri') {
73
            // return uri if no label is found
74
            $label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri();
75
            return $label;
76
        }
77
        return null;
78
    }
79
80
    public function getType()
81
    {
82
        return $this->type;
83
    }
84
85
    public function getUri()
86
    {
87
        return $this->resource->getUri();
88
    }
89
90
    public function getVocab()
91
    {
92
        return $this->vocab;
93
    }
94
95
    public function getVocabName()
96
    {
97
        return $this->vocab->getTitle();
98
    }
99
100
    public function addSubMember($member, $lang = '')
101
    {
102
        $label = $member->getLabel($lang) ? $member->getLabel($lang) : $member->getLabel();
103
        $this->submembers[$label->getValue()] = $member;
104
        $this->sortSubMembers();
105
    }
106
107
    public function getSubMembers()
108
    {
109
        if (empty($this->submembers)) {
110
            return null;
111
        }
112
113
        return $this->submembers;
114
    }
115
116
    private function sortSubMembers()
117
    {
118
        if (!empty($this->submembers)) {
119
            ksort($this->submembers);
120
        }
121
122
    }
123
124
    public function isExternal() {
125
        $propertyUris = $this->resource->propertyUris();
126
        return empty($propertyUris);
127
    }
128
129
    public function getNotation()
130
    {
131
        if ($this->vocab->getConfig()->showNotation() && $this->resource->get('skos:notation')) {
132
            return $this->resource->get('skos:notation')->getValue();
133
        }
134
135
    }
136
137
    public function isReified() {
138
        return (!$this->resource->label() && $this->resource->getLiteral('rdf:value'));
139
    }
140
141
    public function getReifiedPropertyValues() {
142
        $ret = array();
143
        $props = $this->resource->propertyUris();
144
        foreach($props as $prop) {
145
            $prop = (EasyRdf\RdfNamespace::shorten($prop) !== null) ? EasyRdf\RdfNamespace::shorten($prop) : $prop;
146 View Code Duplication
            foreach ($this->resource->allLiterals($prop) as $val) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
147
                if ($prop !== 'rdf:value') { // shown elsewhere
148
                    $ret[gettext($prop)] = new ConceptPropertyValueLiteral($this->model, $this->vocab, $this->resource, $val, $prop);
149
                }
150
            }
151 View Code Duplication
            foreach ($this->resource->allResources($prop) as $val) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
152
                $ret[gettext($prop)] = new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang);
153
            }
154
        }
155
        return $ret;
156
    }
157
158
}
159