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