-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathXBRL-Types.php
286 lines (249 loc) · 11.2 KB
/
XBRL-Types.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
<?php
/**
* Implements class to hold and manage types.
*
* @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\schema\SchemaTypes;
/**
* Class implementation
*/
class XBRL_Types extends \lyquidity\xml\schema\SchemaTypes
{
/**
* The name of the persistence file
*
* @var string $filename
*/
private static $filename = "types.json";
/**
* Mock static constructor
*/
public static function __static()
{
parent::__static();
}
/**
* Get an instance of the types singleton
* @param Closure $instance (optional) A potentially descendant instance to use
* @return SchemaTypes
*/
public static function &getInstance( $instance = null )
{
if ( parent::hasInstance() )
{
return parent::getInstance();
}
/**
* @var XBRL_Types $instance
*/
$instance = parent::getInstance( new self() );
$instance->fromFile();
return $instance;
}
/**
* Default constructor
*/
public function __construct()
{
parent::__construct();
}
/**
* Remove all existing element information
* @return void
*/
public function clearElements()
{
parent::clearElements();
$types = new self();
$types->fromFile();
$this->elements = $types->elements;
}
/**
* Used to create/re-create the XBRL schema types cache file
*/
public static function createJSONFile()
{
$xbrlSchemaFolder = dirname( __FILE__ ) . "/../xbrl";
$global = XBRL_Global::getInstance();
$global->useCache = true;
$global->initializeCache();
$global->setEntityLoader( $global->cacheLocation );
$log = XBRL_Log::getInstance()->debugLog();
$types = new XBRL_Types();
$types->createBaseTypes();
$types->processSchema( "$xbrlSchemaFolder/core/xbrl-instance-2003-12-31.xsd", true );
$types->processSchema( "$xbrlSchemaFolder/xbrldt/xbrldt-2005.xsd", true );
// Add a custom type for validation purposes. These are needed by the XPath 2.0
// node checking but are not part of the Xml built-in types so are added here.
// $types->AddSimpleType( "xs", "yearMonthDuration", "xsd:duration" );
// $types->AddSimpleType( "xs", "dayTimeDuration", "xsd:duration" );
if ( ! $types->toJSONFile() )
{
$log->err( "Problem" );
}
}
/**
* Save the type information
* @param $filename
*/
public function toJSONFile( $filename = null )
{
$typesFilename = is_null( $filename )
? dirname( __FILE__ ) . "/" . XBRL_Types::$filename
: $filename;
$json = $this->toJSON();
if ( $json === false ) return false;
if ( file_put_contents( $typesFilename, $json ) ) return true;
XBRL_Log::getInstance()->err( "Failed to write '$typesFilename'" );
return false;
}
/**
* Allows a constructor to load types from a json file
* @param string $source File name
* @return bool
*/
public function fromFile( $source = null )
{
$typesFilename = is_null( $source )
? dirname( __FILE__ ) . "/" . XBRL_Types::$filename
: $source;
if ( ! file_exists( $typesFilename ) ) return;
$json = file_get_contents( $typesFilename );
if ( $json === false ) return false;
return $this->fromJSON( $json );
}
/**
* Removes all elements with the given prefix
* @param string $prefix
* @return int
*/
public function removeElementsForPrefix( $prefix )
{
$result = 0;
foreach ( $this->elements as $qname => $element )
{
if ( strcasecmp( $element['prefix'], $prefix ) !== 0 ) continue;
$result++;
unset( $this->elements[ $qname ] );
}
foreach ( $this->types as $qname => $type )
{
if ( strcasecmp( $type['prefix'], $prefix ) !== 0 ) continue;
$result++;
unset( $this->types[ $qname ] );
}
if ( isset( $this->processedSchemas[ $prefix ] ) )
{
unset( $this->processedSchemas[ $prefix ] );
}
return $result;
}
/**
* Removes all elements with the given prefix
* @param XBRL $taxonomy
* @return int
*/
public function removeElementsTaxonomy( $taxonomy )
{
$result = 0;
$prefix = $taxonomy->getPrefix();
$result = $this->removeElementsForPrefix( $prefix );
foreach ( $taxonomy->getElements() as $id => $taxElement )
{
if ( isset( $this->typeIds[ $id ] ) )
{
unset( $this->typeIds[ $id ] );
}
}
return $result;
}
/**
* A cache of the types array to prevent it being reconstructed every call
* @var array $xbrlItemTypesCache
*/
private $xbrlItemTypesCache = null;
/**
* Returns a list of all the xBRL defined types
* @return string[]
*/
public function xbrlItemTypeNames()
{
if ( is_null( $this->xbrlItemTypesCache ) )
{
$this->xbrlItemTypesCache = array_keys( $this->xbrlItemTypes() );
}
return $this->xbrlItemTypesCache;
}
/**
* Returns a list of all the xBRL defined types
* @return string[][]
*/
public function xbrlItemTypes()
{
// BMS 2018-04-09 Test candidates changed.
return array(
'xbrli:decimalItemType' => array( 'type' => 'xbrli:decimalItemType', 'base' => 'xs:decimal', 'unitRef' => 'yes' ),
'xbrli:floatItemType' => array( 'type' => 'xbrli:floatItemType', 'base' => 'xs:float', 'unitRef' => 'yes' ),
'xbrli:doubleItemType' => array( 'type' => 'xbrli:doubleItemType', 'base' => 'xs:double', 'unitRef' => 'yes' ),
// The following numeric types are all based on the XML Schema built-in types that are derived by restriction from decimal.
'xbrli:integerItemType' => array( 'type' => 'xbrli:integerItemType', 'base' => 'xs:integer', 'unitRef' => 'yes' ),
'xbrli:nonPositiveIntegerItemType' => array( 'type' => 'xbrli:nonPositiveIntegerItemType', 'base' => 'xs:nonPositiveInteger', 'unitRef' => 'yes' ),
'xbrli:negativeIntegerItemType' => array( 'type' => 'xbrli:negativeIntegerItemType', 'base' => 'xs:negativeInteger', 'unitRef' => 'yes' ),
'xbrli:longItemType' => array( 'type' => 'xbrli:longItemType', 'base' => 'xs:long', 'unitRef' => 'yes' ),
'xbrli:intItemType' => array( 'type' => 'xbrli:intItemType', 'base' => 'xs:int', 'unitRef' => 'yes' ),
'xbrli:shortItemType' => array( 'type' => 'xbrli:shortItemType', 'base' => 'xs:short', 'unitRef' => 'yes' ),
'xbrli:byteItemType' => array( 'type' => 'xbrli:byteItemType', 'base' => 'xs:byte', 'unitRef' => 'yes' ),
'xbrli:nonNegativeIntegerItemType' => array( 'type' => 'xbrli:nonNegativeIntegerItemType', 'base' => 'xs:nonNegativeInteger', 'unitRef' => 'yes' ),
'xbrli:unsignedLongItemType' => array( 'type' => 'xbrli:unsignedLongItemType', 'base' => 'xs:unsignedLong', 'unitRef' => 'yes' ),
'xbrli:unsignedIntItemType' => array( 'type' => 'xbrli:unsignedIntItemType', 'base' => 'xs:unsignedInt', 'unitRef' => 'yes' ),
'xbrli:unsignedShortItemType' => array( 'type' => 'xbrli:unsignedShortItemType', 'base' => 'xs:unsignedShort', 'unitRef' => 'yes' ),
'xbrli:unsignedByteItemType' => array( 'type' => 'xbrli:unsignedByteItemType', 'base' => 'xs:unsignedByte', 'unitRef' => 'yes' ),
'xbrli:positiveIntegerItemType' => array( 'type' => 'xbrli:positiveIntegerItemType', 'base' => 'xs:positiveInteger', 'unitRef' => 'yes' ),
// The following numeric types are all types that have been identified as having particular relevance to the domain space addressed by XBRL and are hence included in addition to the built-in types from XML Schema.
'xbrli:monetaryItemType' => array( 'type' => 'xbrli:monetaryItemType', 'base' => 'xbrl', 'unitRef' => ':monetary yes' ),
'xbrli:sharesItemType' => array( 'type' => 'xbrli:sharesItemType', 'base' => 'xbrl', 'unitRef' => ':shares yes' ),
'xbrli:pureItemType' => array( 'type' => 'xbrli:pureItemType', 'base' => 'xbrl', 'unitRef' => ':pure yes' ),
// type with the numerator being a decimal and the denominator being a non-zero, decimal (xbrli:nonZeroDecimal)
'xbrli:fractionItemType' => array( 'type' => 'xbrli:fractionItemType', 'base' => 'xs:complex', 'unitRef' => 'yes' ),
// The following non-numeric types are all based on XML Schema built-in types that are not derived from either decimal or string.
'xbrli:stringItemType' => array( 'type' => 'xbrli:stringItemType', 'base' => 'xs:string', 'unitRef' => 'no' ),
'xbrli:booleanItemType' => array( 'type' => 'xbrli:booleanItemType', 'base' => 'xs:boolean', 'unitRef' => 'no' ),
'xbrli:hexBinaryItemType' => array( 'type' => 'xbrli:hexBinaryItemType', 'base' => 'xs:hexBinary', 'unitRef' => 'no' ),
'xbrli:base64BinaryItemType' => array( 'type' => 'xbrli:base64BinaryItemType', 'base' => 'xs:base64Binary', 'unitRef' => 'no' ),
'xbrli:anyURIItemType' => array( 'type' => 'xbrli:anyURIItemType', 'base' => 'xs:anyURI', 'unitRef' => 'no' ),
'xbrli:QNameItemType' => array( 'type' => 'xbrli:QNameItemType', 'base' => 'xs:QName', 'unitRef' => 'no' ),
'xbrli:durationItemType' => array( 'type' => 'xbrli:durationItemType', 'base' => 'xs:duration', 'unitRef' => 'no' ),
'xbrli:dateTimeItemType' => array( 'type' => 'xbrli:dateTimeItemType', 'base' => 'xbrl', 'unitRef' => ':dateUnion (union of date and dateTime) no' ),
'xbrli:timeItemType' => array( 'type' => 'xbrli:timeItemType', 'base' => 'xs:time', 'unitRef' => 'no' ),
'xbrli:dateItemType' => array( 'type' => 'xbrli:dateItemType', 'base' => 'xs:date', 'unitRef' => 'no' ),
'xbrli:gYearMonthItemType' => array( 'type' => 'xbrli:gYearMonthItemType', 'base' => 'xs:gYearMonth', 'unitRef' => 'no' ),
'xbrli:gYearItemType' => array( 'type' => 'xbrli:gYearItemType', 'base' => 'xs:gYear', 'unitRef' => 'no' ),
'xbrli:gMonthDayItemType' => array( 'type' => 'xbrli:gMonthDayItemType', 'base' => 'xs:gMonthDay', 'unitRef' => 'no' ),
'xbrli:gDayItemType' => array( 'type' => 'xbrli:gDayItemType', 'base' => 'xs:gDay', 'unitRef' => 'no' ),
'xbrli:gMonthItemType' => array( 'type' => 'xbrli:gMonthItemType', 'base' => 'xs:gMonth', 'unitRef' => 'no' ),
// The following non-numeric types are all based on the XML Schema built-in types that are derived by restriction (and/or list) from string.
'xbrli:normalizedStringItemType' => array( 'type' => 'xbrli:normalizedStringItemType', 'base' => 'xs:normalizedString', 'unitRef' => 'no' ),
'xbrli:tokenItemType' => array( 'type' => 'xbrli:tokenItemType', 'base' => 'xs:token', 'unitRef' => 'no' ),
'xbrli:languageItemType' => array( 'type' => 'xbrli:languageItemType', 'base' => 'xs:language', 'unitRef' => 'no' ),
'xbrli:NameItemType' => array( 'type' => 'xbrli:NameItemType', 'base' => 'xs:Name', 'unitRef' => 'no' ),
'xbrli:NCNameItemType' => array( 'type' => 'xbrli:NCNameItemType', 'base' => 'xs:NCName', 'unitRef' => 'no' ),
);
}
}
XBRL_Types::__static();