Passed
Pull Request — master (#1205)
by Osma
04:18
created

ConceptProperty::sortValues()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 3
nop 0
dl 0
loc 37
rs 8.6666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class for handling concept properties.
5
 */
6
class ConceptProperty
7
{
8
    /** stores the property type */
9
    private $prop;
10
    /** stores the property supertype */
11
    private $super;
12
    /** stores the property label */
13
    private $label;
14
    /** stores the property tooltip */
15
    private $tooltip;
16
    /** stores the property values */
17
    private $values;
18
    /** flag whether the values are sorted, as we do lazy sorting */
19
    private $is_sorted;
20
    private $sort_by_notation;
21
22
    /**
23
     * Label parameter seems to be optional in this phase.
24
     * @param string $prop property type eg. 'rdf:type'.
25
     * @param string $label property label
26
     * @param string $tooltip property tooltip/description
27
     * @param string $super URI of superproperty
28
     * @param boolean $sort_by_notation whether to sort the property values by their notation code
29
     */
30
    public function __construct($prop, $label, $tooltip=null, $super=null, $sort_by_notation=false)
31
    {
32
        $this->prop = $prop;
33
        $this->label = $label;
34
        $this->tooltip = $tooltip;
35
        $this->values = array();
36
        $this->is_sorted = true;
37
        $this->super = $super;
38
        $this->sort_by_notation = $sort_by_notation;
39
    }
40
41
    /**
42
     * Gets the gettext translation for a property or returns the identifier as a fallback.
43
     */
44
    public function getLabel()
45
    {
46
        // first see if we have a translation
47
        // we don't maintain DC 1.1 translations separate from DC Terms
48
        $prop = (substr($this->prop, 0, 5) == 'dc11:') ?
49
            str_replace('dc11:', 'dc:', $this->prop) : $this->prop;
50
        $label = gettext($prop);
51
        if ($label != $prop) {
52
            return $label;
53
        }
54
55
        // if not, see if there was a label for the property in the graph
56
        if ($this->label !== null) {
57
            return $this->label;
58
        }
59
60
        // when no label is found, don't show the property at all
61
        return null;
62
    }
63
64
    /**
65
     * Returns an alphanumeric ID for the property, suitable for use as a CSS identifier.
66
     */
67
    public function getID()
68
    {
69
        return preg_replace('/[^A-Za-z0-9-]/', '_', $this->prop);
70
    }
71
72
    /**
73
     * Returns text for the property tooltip.
74
     * @return string
75
     */
76
    public function getDescription()
77
    {
78
        $helpprop = $this->prop . "_help";
79
80
        // see if we have a translation with the help text
81
        $help = gettext($helpprop);
82
        if ($help != $helpprop) {
83
            return $help;
84
        }
85
86
       // if not, see if there was a comment/definition for the property in the graph
87
        if ($this->tooltip !== null) {
88
            return $this->tooltip;
89
        }
90
91
        // when nothing is found, don't show the tooltip at all
92
        return null;
93
    }
94
95
    /**
96
     * Returns an array of the property values.
97
     * @return ConceptMappingPropertyValue[]
98
     */
99
    public function getValues()
100
    {
101
        if (!$this->is_sorted) {
102
            $this->sortValues();
103
        }
104
        return $this->values;
105
    }
106
107
    public function addValue($value)
108
    {
109
        $this->values[ltrim($value->getNotation() . ' ') . $value->getLabel() . rtrim(' ' . $value->getUri())] = $value;
110
        $this->is_sorted = false;
111
    }
112
113
    private function sortValues()
114
    {
115
        # TODO: sort by URI as last resort
116
        # Note that getLabel() returns URIs in case of no label and may return a prefixed value which affects sorting
117
        if (!empty($this->values)) {
118
            if ($this->sort_by_notation) {
119
                uasort($this->values, function($a, $b) {
120
                    $anot = $a->getNotation();
121
                    $bnot = $b->getNotation();
122
                    if ($anot == null) {
123
                        if ($bnot == null) {
124
                            // assume that labels are unique
125
                            return strcoll(strtolower($a->getLabel()), strtolower($b->getLabel()));
126
                        }
127
                        return 1;
128
                    }
129
                    else if ($bnot == null) {
130
                        return -1;
131
                    }
132
                    else {
133
                        // assume that notations are unique, choose strategy
134
                        if ($this->sort_by_notation == "lexical") {
135
                            return strcoll($anot, $bnot);
136
                        } else { // natural
137
                            return strnatcasecmp($anot, $bnot);
138
                        }
139
                    }
140
                });
141
            }
142
            else {
143
                uasort($this->values, function($a, $b) {
144
                    // assume that labels are unique
145
                    return strcoll(strtolower($a->getLabel()), strtolower($b->getLabel()));
146
                });
147
            }
148
        }
149
        $this->is_sorted = true;
150
    }
151
152
    /**
153
     * Returns property type as a string.
154
     * @return string eg. 'rdf:type'.
155
     */
156
    public function getType()
157
    {
158
        return $this->prop;
159
    }
160
161
    /**
162
     * Returns property supertype (?property skos:subPropertyOf ?super) as a string.
163
     * @return string eg. 'skos:hiddenLabel'.
164
     */
165
    public function getSubPropertyOf()
166
    {
167
        return $this->super;
168
    }
169
}
170