Completed
Pull Request — master (#947)
by
unknown
01:43
created

ConceptProperty::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 4
dl 0
loc 9
rs 9.9666
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 values */
15
    private $values;
16
    /** flag whether the values are sorted, as we do lazy sorting */
17
    private $is_sorted;
18
    private $sort_by_notation;
19
20
    /**
21
     * Label parameter seems to be optional in this phase.
22
     * @param string $prop property type eg. 'rdf:type'.
23
     * @param string $label
24
     */
25
    public function __construct($prop, $label, $super=null, $sort_by_notation=false)
26
    {
27
        $this->prop = $prop;
28
        $this->label = $label;
29
        $this->values = array();
30
        $this->is_sorted = true;
31
        $this->super = $super;
32
        $this->sort_by_notation = $sort_by_notation;
33
    }
34
35
    /**
36
     * Gets the gettext translation for a property or returns the identifier as a fallback.
37
     */
38
    public function getLabel()
39
    {
40
        // first see if we have a translation
41
        // we don't maintain DC 1.1 translations separate from DC Terms
42
        $prop = (substr($this->prop, 0, 5) == 'dc11:') ?
43
            str_replace('dc11:', 'dc:', $this->prop) : $this->prop;
44
        $label = gettext($prop);
45
        if ($label != $prop) {
46
            return $label;
47
        }
48
49
        // if not, see if there was a label for the property in the graph
50
        if ($this->label) {
51
            return $this->label;
52
        }
53
54
        // when no label is found, don't show the property at all
55
        return null;
56
    }
57
58
    /**
59
     * Returns a gettext translation for the property tooltip.
60
     * @return string
61
     */
62
    public function getDescription()
63
    {
64
        $helpprop = $this->prop . "_help";
65
66
        return gettext($helpprop); // can't use string constant, it'd be picked up by xgettext
67
    }
68
69
    /**
70
     * Returns an array of the property values.
71
     * @return ConceptMappingPropertyValue[]
72
     */
73
    public function getValues()
74
    {
75
        if (!$this->is_sorted) {
76
            $this->sortValues();
77
        }
78
        return $this->values;
79
    }
80
81
    public function addValue($value)
82
    {
83
        $this->values[ltrim($value->getNotation() . ' ') . $value->getLabel() . rtrim(' ' . $value->getUri())] = $value;
84
        $this->is_sorted = false;
85
    }
86
87
    private function sortValues()
88
    {
89
        # TODO: sort by URI as last resort
90
        # Note that getLabel() returns URIs in case of no label and may return a prefixed value which affects sorting
91
        if (!empty($this->values)) {
92
            if ($this->sort_by_notation) {
93
                uasort($this->values, function($a, $b) {
94
                    $anot = $a->getNotation();
95
                    $bnot = $b->getNotation();
96
                    if ($anot == null) {
97
                        if ($bnot == null) {
98
                            // assume that labels are unique
99
                            return strcoll(strtolower($a->getLabel()), strtolower($b->getLabel()));
100
                        }
101
                        return 1;
102
                    }
103
                    else if ($bnot == null) {
104
                        return -1;
105
                    }
106
                    else {
107
                        // assume that notations are unique
108
                        return strnatcasecmp($anot, $bnot);
109
                    }
110
                });
111
            }
112
            else {
113
                uasort($this->values, function($a, $b) {
114
                    // assume that labels are unique
115
                    return strcoll(strtolower($a->getLabel()), strtolower($b->getLabel()));
116
                });
117
            }
118
        }
119
        $this->is_sorted = true;
120
    }
121
122
    /**
123
     * Returns property type as a string.
124
     * @return string eg. 'rdf:type'.
125
     */
126
    public function getType()
127
    {
128
        return $this->prop;
129
    }
130
131
    /**
132
     * Returns property supertype (?property skos:subPropertyOf ?super) as a string.
133
     * @return string eg. 'skos:hiddenLabel'.
134
     */
135
    public function getSubPropertyOf()
136
    {
137
        return $this->super;
138
    }
139
}
140