Completed
Push — master ( 4ef44e...f0613e )
by Henri
03:01
created

ConceptPropertyValue::getLabel()   C

Complexity

Conditions 15
Paths 32

Size

Total Lines 37
Code Lines 23

Duplication

Lines 14
Ratio 37.84 %

Importance

Changes 0
Metric Value
dl 14
loc 37
rs 5.0504
c 0
b 0
f 0
cc 15
eloc 23
nc 32
nop 1

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Class for handling concept property values.
5
 */
6
class ConceptPropertyValue 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
    /** submembers */
9
    private $submembers;
10
    /** property type */
11
    private $type;
12
    /** content language */
13
    private $clang;
14
15 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...
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 = '')
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
        // uri if no label is found
72
        $label = $this->resource->shorten() ? $this->resource->shorten() : $this->getUri();
73
        return $label;
74
    }
75
76
    public function getType()
77
    {
78
        return $this->type;
79
    }
80
81
    public function getUri()
82
    {
83
        return $this->resource->getUri();
84
    }
85
86
    public function getVocab()
87
    {
88
        return $this->vocab;
89
    }
90
91
    public function getVocabName()
92
    {
93
        return $this->vocab->getTitle();
94
    }
95
96
    public function addSubMember($member, $lang = '')
97
    {
98
        $label = $member->getLabel($lang) ? $member->getLabel($lang) : $member->getLabel();
99
        $this->submembers[$label->getValue()] = $member;
100
        $this->sortSubMembers();
101
    }
102
103
    public function getSubMembers()
104
    {
105
        if (empty($this->submembers)) {
106
            return null;
107
        }
108
109
        return $this->submembers;
110
    }
111
112
    private function sortSubMembers()
113
    {
114
        if (!empty($this->submembers)) {
115
            ksort($this->submembers);
116
        }
117
118
    }
119
120
    public function isExternal() {
121
        $propertyUris = $this->resource->propertyUris();
122
        return empty($propertyUris);
123
    }
124
125
    public function getNotation()
126
    {
127
        if ($this->vocab->getConfig()->showNotation() && $this->resource->get('skos:notation')) {
128
            return $this->resource->get('skos:notation')->getValue();
129
        }
130
131
    }
132
133
    public function isReified() {
134
        return (!$this->resource->label() && $this->resource->getLiteral('rdf:value'));
135
    }
136
    
137
    public function getReifiedPropertyValues() {
138
        $ret = array();
139
        $props = $this->resource->propertyUris();
140
        foreach($props as $prop) {
141
            $prop = (EasyRdf\RdfNamespace::shorten($prop) !== null) ? EasyRdf\RdfNamespace::shorten($prop) : $prop;
142
            foreach ($this->resource->allLiterals($prop) as $val) {
143
                if ($prop !== 'rdf:value' && $this->resource->get($prop)) { // shown elsewhere
144
                    $ret[gettext($prop)] = new ConceptPropertyValueLiteral($this->resource->get($prop), $prop);
0 ignored issues
show
Bug introduced by
The call to ConceptPropertyValueLiteral::__construct() misses some required arguments starting with $resource.
Loading history...
145
                }
146
            }
147
            foreach ($this->resource->allResources($prop) as $val) {
148
                $ret[gettext($prop)] = new ConceptPropertyValue($this->model, $this->vocab, $val, $prop, $this->clang);
149
            }
150
        }
151
        return $ret;
152
    }
153
154
}
155