/
DataType.php
225 lines (198 loc) · 6.51 KB
/
DataType.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<?php declare(strict_types = 1);
namespace JSKOS;
use InvalidArgumentException;
const IRI_PATTERN = '!^((?P<scheme>[a-z][a-z0-9+.-]*):)' .
'((?P<doubleslash>//)(?P<authority>[^/?#]*))?(?P<path>[^?#]*)' .
'((?<querydef>\?)(?P<query>[^#]*))?(#(?P<fragment>.*))?!';
const DATE_PATTERN = '!^(?P<year>-?\d\d\d\d)' .
'(-((?P<month>\d\d)(' .
'-(?P<day>\d\d)' .
'(T\d\d:\d\d:\d\d(\.\d+)?)?' .
'(Z|[+-]\d\d:\d\d)?' .
')?))?$!';
const LANGUAGE_PATTERN = '/^[a-z]{2,3}(?:-[A-Z]{2,3}(?:-[a-zA-Z]{4})?)?$/';
const LANGUAGE_RANGE_PATTERN = '/^([a-z]{2,3}(?:-[A-Z]{2,3}(?:-[a-zA-Z]{4})?)?)?-$/';
/**
* Base class of JSKOS Resources and fields.
*/
abstract class DataType extends PrettyJsonSerializable
{
use Constructor;
const FIELDS = [];
/**
* Get field definition from FIELDS, including parent classes.
*/
protected static function fieldType(string $field, bool $strict = false)
{
$class = get_called_class();
while ($class && $class != self::class) {
if (isset($class::FIELDS[$field])) {
return $class::FIELDS[$field];
}
$class = get_parent_class($class);
}
if ($strict) {
throw new InvalidArgumentException(
get_called_class() . "->$field does not exist"
);
}
}
private function fieldException(string $field, string $message)
{
return new InvalidArgumentException(
get_called_class() . "->$field must $message"
);
}
protected function setField(string $field, $value, bool $strict = true)
{
if ($field == '@context') {
return;
}
$type = static::fieldType($field, $strict);
if (!$type) {
return;
}
if (is_null($value)) {
$value = null;
} elseif (is_array($type)) { # Set or Listing
if ($type[0] == 'Set') {
if (!($value instanceof Set)) {
if (is_array($value)) {
$class = 'JSKOS\\' . $type[1];
$value = new Set(
array_map(function($m) use ($class) {
if (is_null($m)) {
return null;
}
if ($m instanceof $class) {
return $m;
}
return new $class($m);
}, $value)
);
} else {
throw $this->fieldException($field, "be a Set");
}
}
# TODO: check member types
} else { # Listing
if (!($value instanceof Listing)) {
if (is_array($value)) {
$value = new Listing($value);
} else {
throw $this->fieldException($field, "be a Listing");
}
}
# TODO: check member types
}
} elseif ($type == 'LanguageMapOfStrings') {
if (!($value instanceof LanguageMapOfStrings)) {
$value = new LanguageMapOfStrings($value);
}
} elseif ($type == 'LanguageMapOfLists') {
if (!($value instanceof LanguageMapOfLists)) {
$value = new LanguageMapOfLists($value);
}
} elseif ($type == 'ConceptScheme') {
if (!($value instanceof ConceptScheme)) {
$value = new ConceptScheme($value);
}
} elseif ($type != '*' && !DataType::hasType($value, $type)) {
throw $this->fieldException($field, "match JSKOS\DataType::is$type");
}
$this->$field = $value;
}
public function __set($field, $value)
{
$this->setField($field, $value);
}
public function &__get($field)
{
$type = static::fieldType($field);
if ($type) {
if (is_null($this->$field)) {
$null = null;
return $null;
} else {
return $this->$field;
}
} else {
trigger_error(
"Undefined property: " . get_called_class() . "::$$field",
\E_USER_NOTICE
);
}
}
public function __isset($field): bool
{
return !!static::fieldType($field);
}
/**
* Check whether a given value looks like an URI.
*/
public static function isURI($uri): bool
{
return is_string($uri) && preg_match(IRI_PATTERN, $uri) === 1;
}
/**
* Check whether a given value looks like an http/https URL.
*/
public static function isURL($url): bool
{
return is_string($url) &&
preg_match(IRI_PATTERN, $url, $match) &&
($match[2] == 'http' || $match[2] == 'https');
}
/**
* Check whether a given value looks like a date.
*/
public static function isDate($date): bool
{
return is_string($date) && preg_match(DATE_PATTERN, $date) === 1;
}
/**
* Check whether a given value looks like a language tag.
*/
public static function isLanguage($language): bool
{
return is_string($language) && preg_match(LANGUAGE_PATTERN, $language) === 1;
}
/**
* Check whether a given value looks like a language range.
*/
public static function isLanguageRange($range): bool
{
return is_string($range) && preg_match(LANGUAGE_RANGE_PATTERN, $range) === 1;
}
/**
* Check whether a given value looks like a language or language range.
*/
public static function isLanguageOrRange($language): bool
{
return is_string($language) &&
(preg_match(LANGUAGE_PATTERN, $language) === 1 ||
preg_match(LANGUAGE_RANGE_PATTERN, $language) === 1);
}
/**
* Check whether a given value is a string.
*/
public static function isString($string): bool
{
return is_string($string);
}
/**
* Check whether a given value is a concept scheme
*/
public static function isConceptScheme($scheme): bool
{
return $scheme instanceof ConceptScheme;
}
/**
* Check whether a given value is of given type.
*/
public static function hasType($value, string $type): bool
{
$method = "is$type";
return self::$method($value);
}
}