-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathXBRL-QName.php
313 lines (284 loc) · 8.39 KB
/
XBRL-QName.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* QName class and factory functions. This is ported from Arelle.
*
* @author Bill Seddon
* @version 0.9
* @Copyright (C) 2018 Lyquidity Solutions Limited
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
use lyquidity\xml\QName;
// Now this is implemeted in /xml/QName so the only purpose of this file is to make sure
// the QName class in loaded
if ( ! class_exists( '\\lyquidity\\xml\\schema\\SchemaTypes', true ) )
{
$xmlSchemaPath = isset( $_ENV['XML_LIBRARY_PATH'] )
? $_ENV['XML_LIBRARY_PATH']
: ( defined( 'XML_LIBRARY_PATH' ) ? XML_LIBRARY_PATH : __DIR__ . "/../xml/" );
require_once $xmlSchemaPath . '/bootstrap.php';
}
/**
* Generate a QName instance.
*
* @param array|string $value can be a QName, an array or a string. If a string it will be the
* namespace of the value of $name or a Clark notation.
* If an array then it will be an array representation of the QName.
* @param array|string $name Can be an array of prefix/namespace key/value pairs or it can be
* the prefixed local name
* @param bool $noPrefixIsNoNamespace If no prefix is found then there will be no namespace if this is true
* @param Exception $castException In case there is a cast exception
* @param Exception $prefixException In case there is a prefix exception
* @throws Exception
* @return \lyquidity\xml\QName
*/
function qname( $value, $name = null, $noPrefixIsNoNamespace = false, $castException = null, $prefixException = null )
{
return \lyquidity\xml\qname( $value, $name, $noPrefixIsNoNamespace, $castException, $prefixException );
}
return;
/**
* Convert a namespace/local name pair into a QName instance
* Does not handle localNames with prefix
*
* @param string $namespaceURI
* @param string $localName
* @return null|QName
*/
function qnameNsLocalName( $namespaceURI, $localName )
{
return new QName( null, $namespaceURI ? $namespaceURI : null, $localName );
}
/**
* Converts a string in the clark notation format to a QName instance
* Does not handle clark names with prefix
*
* @param string $clarkname
* @return null|QName
*/
function qnameClarkName( $clarkname )
{
// clark notation (with optional prefix)
if ( $clarkname && $clarkname[0] == '{' )
{
// namespaceURI,sep,prefixedLocalName = value[1:].rpartition('}')
$matches = null;
if ( ! preg_match( "/({(?<namespaceURI>.*)})?(?<prefixedLocalName>.*)/", $clarkname, $matches ) )
{
return null;
}
$namespaceURI = $matches['namespaceURI'] ? $matches['namespaceURI'] : null;
$prefixedLocalName = $matches['prefixedLocalName'];
// prefix,sep,localName = $prefixedLocalName.rpartition(':')
$matches = null;
if ( ! preg_match( "/((?<prefix>.*):)?(?<localName>.*)/", $prefixedLocalName, $matches ) )
{
return null;
}
$prefix = $matches['prefix'] ? $matches['prefix'] : null;
$localName = $matches['localName'] ? $matches['localName'] : null;
return new QName( $prefix, $namespaceURI, $localName );
}
else
{
return new QName( null, null, $clarkname );
}
}
/**
* Create a QName from a prefix:name pair. Use the namespace associated
* with $element to resolve the prefix (if there is one)
*
* @param SimpleXMLElement $element
* @param string $prefixedName
* @param Exception $prefixException
* @throws \Exception
* @return NULL|QName
*/
function qnameEltPfxName( $element, $prefixedName, $prefixException = null )
{
return \lyquidity\xml\qnameEltPfxName( $element, $prefixedName, $prefixException );
}
/**
* @deprecated
* Represents a namespace, prefix and localname
*/
class QNamex
{
/**
* The QName prefix
* @var string
*/
public $prefix;
/**
* The QName namespace
* @var string
*/
public $namespaceURI;
/**
* The QName local name
* @var string
*/
public $localName;
/**
* A hash of the QName
* @var string
*/
private $qnameValueHash;
/**
* Default constructor
*
* @param string $prefix
* @param string $namespaceURI
* @param string $localName
*/
public function __construct( $prefix, $namespaceURI, $localName )
{
$this->prefix = $prefix;
$this->namespaceURI = $namespaceURI;
$this->localName = $localName;
$this->qnameValueHash = hash( 'sha256', serialize( array( $this->namespaceURI, $this->localName ) ) );
}
/**
* Return the hash of the QName
*
* @return string
*/
public function getHash()
{
return $this->qnameValueHash;
}
/**
* Return a representation of the QName using a clark notation {namespace}prefix:name
*
* @return string
*/
public function clarkNotation()
{
if ( $this->namespaceURI )
{
return sprintf( '{%s}%s', $this->namespaceURI, $this->localName );
}
else
{
return $this->localName;
}
}
/**
* Create a string representation
*
* @return number|string
*/
public function __toString()
{
$namespaceURI = empty( $this->namespaceURI )
? ""
: "{{$this->namespaceURI}}";
return $namespaceURI . $this->localName;
}
/**
* Test whether one QName equals another
*
* @param QName $other
* @return boolean
*/
public function equals( $other )
{
try
{
return $this->qnameValueHash == $other->qnameValueHash ||
( $this->localName == $other->localName && $this->namespaceURI == $other->namespaceURI );
}
catch( \Exception $ex )
{
return false;
}
}
/**
* Test whether one QName is less than another
*
* @param QName $other
* @return boolean
*/
public function lessThan( $other )
{
return $this->namespaceURI == null && $other->namespaceURI ||
$this->namespaceURI && $other->namespaceURI && $this->namespaceURI < $other->namespaceURI ||
$this->namespaceURI == $other->namespaceURI && $this->localName < $other->localName;
}
/**
* Test whether one QName is less than or equal to another
*
* @param QName $other
* @return boolean
*/
public function lessThanOrEqual( $other )
{
return $this->namespaceURI == null && $other->namespaceURI ||
$this->namespaceURI && $other->namespaceURI && $this->namespaceURI < $other->namespaceURI ||
$this->namespaceURI == $other->namespaceURI && $this->localName <= $other->localName;
}
/**
* Test whether one QName is greater than another
*
* @param QName $other
* @return boolean
*/
public function greaterThan( $other )
{
return $this->namespaceURI && $other->namespaceURI == null ||
$this->namespaceURI && $other->namespaceURI && $this->namespaceURI > $other->namespaceURI ||
$this->namespaceURI == $other->namespaceURI && $this->localName > $other->localName;
}
/**
* Test whether one QName is greater or equal to another
*
* @param QName $other
* @return boolean
*/
public function greaterThanOrEqual( $other )
{
return $this->namespaceURI && $other->namespaceURI == null ||
$this->namespaceURI && $other->namespaceURI && $this->namespaceURI > $other->namespaceURI ||
$this->namespaceURI == $other->namespaceURI && $this->localName >= $other->localName;
}
/**
* Returns true if the QName is valid
*
* @return bool
*/
public function isValid()
{
// QName object bool is false if there is no local name (even if there is a namespace URI).
return (bool) $this->localName;
}
/**
* Returns true is both the local name and namespace are empty
* @return boolean
*/
public function isEmpty()
{
return empty( $this->localName ) && empty( $this->namespaceURI );
}
/**
* Convert the QName to an array
*/
public function toArray()
{
$result = array( 'localname' => $this->localName );
if ( isset( $this->prefix ) ) array( 'prefix' => $this->prefix );
if ( isset( $this->namespaceURI ) ) array( 'namespace' => $this->namespaceURI );
return $result;
}
}