-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQName.php
213 lines (194 loc) · 5.11 KB
/
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
<?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/>.
*
*/
namespace lyquidity\xml;
/**
* Represents a namespace, prefix and localname
*/
class QName
{
/**
* 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;
}
}