1
|
|
|
<?php declare(strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace JSKOS; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This JSKOS Service can be queried by URI and (optionally) Notation. A |
7
|
|
|
* JSKOS Item of predefined type (Concept, ConceptScheme...) is returned |
8
|
|
|
* if the requested URI matches a given prefix and an optional notation |
9
|
|
|
* suffix or if the requested notation looks valid. |
10
|
|
|
*/ |
11
|
|
|
class URISpaceService extends Service |
12
|
|
|
{ |
13
|
|
|
private $config = []; |
14
|
|
|
|
15
|
|
|
protected $supportedParameters = ['notation']; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Create a new URISpaceService based on configurations. |
19
|
|
|
* |
20
|
|
|
* Configuration for each JSKOS class consists of the mandatory |
21
|
|
|
* field "uriSpace" and the optional fields "notationPattern" and |
22
|
|
|
* "notationNormalizer". |
23
|
|
|
*/ |
24
|
|
|
public function __construct(array $config) |
25
|
|
|
{ |
26
|
|
|
foreach ($config as $type => $typeConfig) { |
27
|
|
|
if (!$typeConfig['uriSpace']) { |
28
|
|
|
throw new \Exception('Missing field uriSpace'); |
29
|
|
|
} elseif (!DataType::isURI($typeConfig['uriSpace'])) { |
30
|
|
|
throw new \Exception('uriSpace must be an URI'); |
31
|
|
|
} |
32
|
|
|
if (!class_exists("JSKOS\\$type")) { |
33
|
|
|
throw new \Exception("Class JSKOS\\$type not found!"); |
34
|
|
|
} |
35
|
|
|
if (!isset($typeConfig['notationPattern'])) { |
36
|
|
|
$typeConfig['notationPattern'] = '/.*/'; |
37
|
|
|
} |
38
|
|
|
if (isset($typeConfig['notationNormalizer'])) { |
39
|
|
|
// THIS CAN BE A SECURITY ISSUE! |
40
|
|
|
$normalizer = $typeConfig['notationNormalizer']; |
41
|
|
|
if (!function_exists($normalizer)) { |
42
|
|
|
throw new \Exception("Function $normalizer not found!"); |
43
|
|
|
} |
44
|
|
|
} else { |
45
|
|
|
$typeConfig['notationNormalizer'] = null; |
46
|
|
|
} |
47
|
|
|
$this->config[$type] = [ |
48
|
|
|
'uriSpace' => $typeConfig['uriSpace'], |
49
|
|
|
'notationPattern' => $typeConfig['notationPattern'], |
50
|
|
|
'notationNormalizer' => $typeConfig['notationNormalizer'], |
51
|
|
|
]; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function query(array $query, string $path=''): Result |
56
|
|
|
{ |
57
|
|
|
$class = null; |
58
|
|
|
|
59
|
|
|
if (isset($query['uri']) && $query['uri'] !== "") { |
60
|
|
|
foreach ($this->config as $type => $config) { |
61
|
|
|
// request URI matches uriSpace |
62
|
|
|
if (strpos($query['uri'], $config['uriSpace']) === 0) { |
63
|
|
|
$uri = $query['uri']; |
64
|
|
|
$notation = substr($uri, strlen($config['uriSpace'])); |
65
|
|
|
|
66
|
|
|
if (!preg_match($config['notationPattern'], $notation)) { |
67
|
|
|
return new Result(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ($config['notationNormalizer']) { |
71
|
|
|
$notation = $config['notationNormalizer']($notation); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (!$notation && $notation !== '0') { |
75
|
|
|
unset($notation); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$class = "JSKOS\\$type"; |
79
|
|
|
|
80
|
|
|
break; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
if (!isset($uri)) { |
84
|
|
|
return new Result(); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if (isset($query['notation']) && $query['notation'] !== "") { |
89
|
|
|
if (isset($notation)) { |
90
|
|
|
if ($query['notation'] != $notation) { |
91
|
|
|
// requested notation and uri do not match |
92
|
|
|
return new Result(); |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
foreach ($this->config as $type => $config) { |
96
|
|
|
if (preg_match($config['notationPattern'], $query['notation'])) { |
97
|
|
|
$notation = $query['notation']; |
98
|
|
|
if ($config['notationNormalizer']) { |
99
|
|
|
$notation = $config['notationNormalizer']($notation); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$class = "JSKOS\\$type"; |
103
|
|
|
|
104
|
|
|
if (!isset($uri)) { |
105
|
|
|
$uri = $config['uriSpace'] . $notation; |
106
|
|
|
} |
107
|
|
|
break; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
if (!isset($notation)) { |
111
|
|
|
return new Result(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($class && isset($uri)) { |
117
|
|
|
if (isset($notation)) { |
118
|
|
|
$content = new $class(['uri' => $uri, 'notation' => [$notation]]); |
119
|
|
|
} else { |
120
|
|
|
$content = new $class(['uri' => $uri]); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return isset($content) ? new Result([$content]) : new Result(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|