ConceptProperty   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 60
dl 0
loc 163
rs 10
c 0
b 0
f 0

9 Methods

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