-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathXBRL.php
19530 lines (16955 loc) · 674 KB
/
XBRL.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Main XBRL taxonomy instance
*
* Almost all the XBRL 2.1 specification is implemented and support for XBRL DT 1.0.
* The omissions from the XBRL 2.1 specification are:
*
* Support has not been added for reference linkbases. The focus of this code is to
* support internal reporting and it is unlikely that documentation of internal systems
* will be accomplished using information in a reference linkbase.
*
* The arc roles general-special and similar-tuples are not supported
*
* The use of XPointer sytax in locator href values is accommodated but only to extract
* the XPointer value. To complete this support it would be necessary to create an XPath
* query from the value.
*
* elements with notAll has-hypercube roles are detected and the hypercubes are recorded
* but the they are not applied to negate dimensions, domains and members
*
* @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 XBRL\Formulas\Formulas;
use lyquidity\xml\QName;
use lyquidity\xml\schema\SchemaTypes;
use XBRL\Formulas\Resources\Filters\ConceptName;
use XBRL\Formulas\Resources\Variables\VariableSet;
/**
* Main XBRL control class
* @author Bill Seddon
*/
class XBRL {
// Static variables
/**
* A map of the schema namespaces to class
* @var array
*/
private static $namespace_to_class_map = array();
/**
* A map of the entry point namespaces to class
* @var array
*/
private static $entrypoints_to_class_map = array();
/**
* A map of the compiled taxonomy file to use for each requested taxonomy
* @var array
*/
private static $xsd_to_compiled_map = array();
/**
* When set this value will be returned by getDefaultLanguage
* @var string
*/
public static $specificLanguage = null;
// Instance variables
/**
* Reference to a global shared class that holds indexed references all taxonomy documents
* @var XBRL_Global $context
*/
public $context = null;
/**
* A list of the schemas directly imported by this schema
* @var array
*/ private $indirectNamespaces = array();
/**
* An array of schema files imported by this taxonomy
*/
public function getIndirectNamespaces()
{
return $this->indirectNamespaces;
}
/**
* An array of the schema namespaces that have used this schema
* @var array
*/ private $usedByNamespaces = array();
/**
*
* @param XBRL $taxonomy
*/
public function AddUserNamespace( $taxonomy )
{
if ( ! ( $taxonomy instanceof XBRL ) ) return;
$this->usedByNamespaces[] = $taxonomy->getNamespace();
}
/**
* A list of the schemas that have used this schema
*/
public function getUsedByNamespaces()
{
return $this->usedByNamespaces;
}
/**
* The XML of the texonomy schema document
* @var SimpleXMLElement $xbrlDocument
*/
protected $xbrlDocument = null;
/**
* Seggested by tim-vandecasteele to allow the same information in
* linkbases shared by more than one taxonomy to be loaded into all taxonomies
* @var array
* @see https://github.com/tim-vandecasteele/xbrl-experiment/commit/3610466123ffe936fd45b5a0299fa97baa4699ac
*/
private $processedLinkbases = array();
/**
* The elements in the taxonomy
* @var array $elementIndex
*/
private $elementIndex = array();
/**
* The elements in the taxonomy that are tuples
* @var array $tupleMembersIndex
*/
private $tupleMembersIndex = array();
/**
* The elements in the taxonomy that are hypercubes
* @var array $elementHypercubes
*/
private $elementHypercubes = array();
/**
* The elements in the taxonomy that are hypercube dimensions
* @var array $elementDimensions
*/
private $elementDimensions = array();
/**
* A list of any custom linkbase link element names
* @var array
*/
private $elementLinkTypes = array();
/**
* A list of any custim linkbase arc element name
* @var array
*/
private $elementArcTypes = array();
/**
* A list of any custom link indexed by roleUri
* @var array
*/
private $customRoles = array();
/**
* A list of any generic link indexed by roleUri
* @var array
*/
protected $genericRoles = array();
/**
* The file name of the taxonomy
* @var string $schemaLocation
*/
private $schemaLocation = "";
/**
* The namespace of the taxonomy
* @var string $namespace
*/
private $namespace = "";
/**
* The prefix used for the target namespace
* @var string
*/
private $prefix = null;
/**
* Has the taxonomy been loaded
* @var boolean $loadSuccess
*/
private $loadSuccess = false;
/**
* A list of the complex types from the taxonomy
* @var array $complexTypes
*/
private $complexTypes = array();
/**
* A list of arcrole types from the schema
* @var array $arcroleTypes
*/
private $arcroleTypes = array();
/**
* A list of arcrole type ids from the schema
* @var array $arcroleTypeIds The array is indexed by id with the value being a path that identifies the specific arcroleType
*/
private $arcroleTypeIds = array();
/**
* A list of role types from the schema
* @var array $roleTypes
*/
private $roleTypes = array();
/**
* A list of role type ids from the schema
* @var array $roleTypeIds The array is indexed by id with the value being a path that identifies the specific roleType
*/
private $roleTypeIds = array();
/**
* A list of linkbase types from the schema
* @var array $linkbaseTypes
*/
private $linkbaseTypes = array();
/**
* A list of role refs from the schema
* @var array $definitionRoleRefs
*/
private $definitionRoleRefs = array();
/**
* A list of role ref details for roles that are maintained in another taxonomy
* These will be saved with a compiled taxonomy or removed.
* @var array
*/
protected $foreignDefinitionRoleRefs = array();
/**
* A list of role refs from the schema
* @var array $referenceRoleRefs
*/
private $referenceRoleRefs = array();
/**
* Have the string for the taxonomy been loaded
* @var boolean $stringsLoaded
*/
private $stringsLoaded = false;
/**
* Caches the prefixes after the first time they are accessed
* @var array
*/
private $documentPrefixes = null;
/**
* The name of the XSD of the base taxonomy when an extension taxonomy is being used
* @var string
*/
private $baseTaxonomy = null;
/**
* An array containing alternatives to the default number of places to which displayed numeric
* values (such as monetaryItemType but not percentItemItem) will be displayed.
* By default, the @decimals attribute value will be used if provided. So, by default, an
* instance @decimals value of -3 will be divided by 1000 before it's displayed
*
* 0 = no rounding
* 2 = hundreds
* 3 = thousands
* 6 = millions
*
* @var integer $displayRounding
*/
private $displayRoundings = array();
/**
* Flag holding the current validation state
*
* @var bool
*/
private static $validating = false;
/**
* A list of the schema files imported by this schema
* @var array $schemaFiles
*/
private $importedFiles = array();
/**
* An array of schema files imported by this taxonomy
*/
public function getImportedFiles()
{
return is_array( $this->importedFiles ) ? $this->importedFiles : array();
}
/**
* A list of the schema files included by this schema
* @var array $schemaFiles
*/
private $includedFiles = array();
/**
* Flag set after linkbases have been processed so repeated processing can be avoided
* @var bool $linkbasesProcessed
*/
private $linkbasesProcessed = false;
/**
* A temporary variable to hold role types by linkbase file name and is used as
* part of the XDT validation processes
* This is temporary in the sense that it will not be recorded in the JSON store.
* @var array $linkbaseRoleTypes
*/
private $linkbaseRoleTypes = array();
/**
* A variable to hold role target roles by linkbase file name and is
* used as part of the XDT validation processes
* This is temporary in the sense that it will not be recorded in the JSON store.
* @var array $xdtTargetRoles
*/
private $xdtTargetRoles = array();
/**
* A list of 'extra' elements added when compiling an extension taxonomy
* @var array $extraElements
*/
private $extraElements = array();
/**
* A temp array used to hold a list of the roles added to custom or generic extended link and arcs
* @var array $customArcsAdded
*/
private $customArcsAdded = array();
/**
* arc names MUST be unique within a variable-set so this array maintains
* a list of discovered names and how locate the respective arc
* @var array $variableSetNames
*/
private $variableSetNames = array();
/**
* An array of linkbases in this document and the element ids they contain
* @var array
*/
private $linkbaseIds = array();
/**
* Flag indicating whether there are formulas in the taxonomy
* @var bool $hasFormulas
*/
private $hasFormulas = false;
/**
* A list of the linkbases processed and information about them
* @var array $linkbases
*/
private $linkbases = array();
/**
* A temporary collection of discovered enumeration concept ids
* @var array
*/
private $enumerations = array();
/**
* True if the instance has been loaded from a JSON file
* @var string
*/
private $loadedFromJSON = false;
/**
* A changeable function to allow a third party to control the behavior of beforeDimensionalPruned()
* @var callable
*/
public static $beforeDimensionalPrunedDelegate = null;
/**
* A changeable function to allow a third party to control the behavior of getBeginEndPreferredLabelPairs()
* @var callable
*/
public static $beginEndPreferredLabelPairsDelegate = null;
/**
* Variable for public functions getBeginEndPreferredLabelPairs
* @var array An array of array pairs wherre each member of the pair is a preferred label role
*/
public static $beginEndPreferredLabelPairs = array();
/**
* Static constructor
*/
public static function constructor()
{
self::$beginEndPreferredLabelPairs = array(
array(
XBRL_Constants::$labelRolePeriodStartLabel,
XBRL_Constants::$labelRolePeriodEndLabel
)
);
// Apply a default delegate
self::$beforeDimensionalPrunedDelegate = function( XBRL $taxonomy, array $dimensionalNode, array &$parentNode )
{
return $taxonomy->beforeDimensionalPruned( $dimensionalNode, $parentNode );
};
// Apply a default delegate
self::$beginEndPreferredLabelPairsDelegate = function()
{
return self::$beginEndPreferredLabelPairs;
};
}
/**
* Reset the static arrays
*/
public static function reset()
{
XBRL::$namespace_to_class_map = array();
XBRL::$entrypoints_to_class_map = array();
XBRL::$xsd_to_compiled_map = array();
}
/**
* Return the valiation state
* @return boolean
*/
public static function isValidating()
{
// If the class is set to validate then validate it is
return XBRL::$validating;
}
/**
* Sets the flag indicating whether or not the taxonomy should be validated as it is loaded from a schema file
*
* @param string $state
* @return bool The previous state
*/
public static function setValidationState( $state = true)
{
$previousState = XBRL::$validating;
XBRL::$validating = $state;
return $previousState;
}
/**
* Called to allow a class file to register xsd to class mapping
* @param array $map_entries Array of maps
* @param string $classname The name of the taxonomy class with which the $xsd_entries are associated
* @return void
*/
public static function add_namespace_to_class_map_entries( $map_entries, $classname )
{
if ( is_string( $map_entries ) && ! empty( $map_entries ) )
{
$map_entries = array( $map_entries );
}
if ( ! is_array( $map_entries ) || count( $map_entries ) === 0 ) return;
XBRL::$namespace_to_class_map = array_merge( XBRL::$namespace_to_class_map, array_fill_keys( $map_entries, $classname ) );
}
/**
* Called to allow a class file to register taxonomy entry point to class mapping
* @param array $map_entries Array of maps
* @param string $classname The name of the taxonomy class with which the $xsd_entries are associated
* @return void
*/
public static function add_entry_namespace_to_class_map_entries( $map_entries, $classname )
{
if ( ! is_array( $map_entries ) || count( $map_entries ) === 0 ) return;
XBRL::$entrypoints_to_class_map = array_merge( XBRL::$entrypoints_to_class_map, array_fill_keys( $map_entries, $classname ) );
}
/**
* Called to allow a class file to register xsd to class mapping
* @param Array $xsd_entries Array of maps
* @param string $compiled_taxonomy_name The name of the compiled taxonomy with which the $xsd_entries are associated
* @return void
*/
public static function add_xsd_to_compiled_map_entries( $xsd_entries, $compiled_taxonomy_name )
{
if ( ! is_array( $xsd_entries ) || count( $xsd_entries ) === 0 ) return;
global $compiled_taxonomy_name_prefix;
if ( strpos( $compiled_taxonomy_name_prefix, '\\') !== false ) $compiled_taxonomy_name_prefix = str_replace( '\\', '/', $compiled_taxonomy_name_prefix );
if ( strpos( $compiled_taxonomy_name_prefix, './') ) $compiled_taxonomy_name_prefix = XBRL::normalizePath( $compiled_taxonomy_name_prefix );
XBRL::$xsd_to_compiled_map = array_merge( XBRL::$xsd_to_compiled_map, array_fill_keys( $xsd_entries, $compiled_taxonomy_name_prefix . $compiled_taxonomy_name ) );
}
/**
* This function returns the name of the class to use to process XBRL taxonomies
* @param string $namespace
* @return string The class to be used for the namespace
*/
public static function class_from_namespace( $namespace )
{
return isset( XBRL::$namespace_to_class_map[ $namespace ] )
? XBRL::$namespace_to_class_map[ $namespace ]
: ( isset( XBRL::$namespace_to_class_map[ rtrim( $namespace, '/' ) ] )
? XBRL::$namespace_to_class_map[ rtrim( $namespace, '/' ) ]
: ( isset( XBRL::$namespace_to_class_map[ "$namespace/" ] )
? XBRL::$namespace_to_class_map[ "$namespace/" ]
: "XBRL"
)
);
}
/**
* This function returns the name of the class to use to process XBRL taxonomies based on the supported taxonomy entry points
* @param string $namespace
* @return string The class to be used for the namespace
*/
public static function class_from_entries_map( $namespace )
{
return isset( XBRL::$entrypoints_to_class_map[ $namespace ] ) ? XBRL::$entrypoints_to_class_map[ $namespace ] : false;
}
/**
* This function returns the name of the compiled taxonomy to use in place of the XSD
* @param string $xsd The name of the XSD to be loaded
* @return string The name of the corresponding compiled taxonomy
*/
public static function compiled_taxonomy_for_xsd( $xsd )
{
return isset( XBRL::$xsd_to_compiled_map[ $xsd ] ) ? XBRL::$xsd_to_compiled_map[ $xsd ] : null;
}
/**
* Loads a taxonomy from a file
* @param string[]|string $file A string containing a single filename of .json .zip or .xsd or an array of .xsd files
* @param bool $compiling True if the function is being called from the compile function. Defaults to false.
* @return false|XBRL
*/
public static function load_taxonomy( $file = null, $compiling = false )
{
if ( $file === null )
{
XBRL_Log::getInstance()->warning( "A file (.json, .zip or .xsd) must be provided" );
return false;
}
if ( is_array( $file ) )
{
XBRL_Log::getInstance()->info( " Files: " . implode( "\n\t", array_map( function( $file ) { return basename( $file ); }, $file ) ) );
}
else
{
XBRL_Log::getInstance()->info( " File: $file" );
// Convert the file to an array
$file = array( $file );
}
if ( ! count( $file ) )
{
XBRL_Log::getInstance()->warning( "There are no files provided" );
return false;
}
$xbrl = null;
// Modify the array so the scheme and extension are available
$files = array_map( function( $file ) {
$scheme = parse_url( $file, PHP_URL_SCHEME );
$extension = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) );
return array(
'file' => $file,
'scheme' => $scheme,
'extension' => $extension,
);
}, $file );
$allXsd = function( $files ) {
$xsdFiles = array_filter( $files, function( $file ) { return $file['extension'] === "xsd"; } );
return count( $xsdFiles ) == count( $files );
};
if ( $compiling && ! $allXsd( $files ) )
{
XBRL_Log::getInstance()->warning( "When compiling all the files provided MUST be .xsd" );
}
if ( count( $files ) > 1 && ! $allXsd( $files ) )
{
XBRL_Log::getInstance()->warning( "If more than one file is provided then BOTH files MUST be .xsd" );
return false;
}
foreach ( $files as &$file )
{
if ( ! $compiling && $file['extension'] === STANDARD_PREFIX_SCHEMA_ALTERNATIVE )
{
// Check to see if there is a pre-defined compiled file
$xsd = strtolower( pathinfo( $file['file'], PATHINFO_BASENAME ) );
$compiled_taxonomy_file = XBRL::compiled_taxonomy_for_xsd( $xsd );
if ( $compiled_taxonomy_file !== null )
{
$file['extension'] = "json";
$file['file'] = "$compiled_taxonomy_file.{$file['extension']}";
$file['scheme'] = "";
}
else
{
// Check to see if there is a compiled file of the same name
if ( file_exists( str_replace( ".xsd", ".json", $file['file'] ) ) )
{
$file['extension'] = "json";
$file['file'] = str_replace( ".xsd", ".json", $file['file'] );
}
}
}
if ( ! in_array( $file['scheme'], array( 'http', 'https' ) ) )
{
if ( ! file_exists( $file['file'] ) )
{
// First try the other sort of file
if ( strpos( $file['file'], '.json' ) )
{
$file['extension'] = 'zip';
$file['file'] = str_replace( '.json', '.zip', $file['file'] );
}
else
{
$file['extension'] = 'json';
$file['file'] = str_replace( '.zip', '.json', $file['file'] );
}
if ( ! file_exists( $file['file'] ) )
{
XBRL_Log::getInstance()->warning( "The requested file ({$file['file']}) does not exist." );
return false;
}
}
}
unset( $file );
}
if ( $allXsd( $files ) )
{
// Convert the array
$files = array_map( function( $file ) { return $file['file']; }, $files );
$xbrl = XBRL::withTaxonomy( $files, true );
if ( $xbrl === null || ! $xbrl->loadSuccess )
{
XBRL_Log::getInstance()->taxonomy_validation( "5.1", "The taxonomy could not be instantiated.",
array(
'count' => count( $files ),
'file(s)' => "'" . implode( "', '", $files ) . "'",
)
);
$xbrl = null;
}
return $xbrl;
}
// There should be only one file
$countFiles = count( $files );
if ( $countFiles > 1 )
{
XBRL_Log::getInstance()->warning( "There should be only one none .xsd file. $countFiles provided." );
return false;
}
$json = null;
$file = $files[0]['file'];
$extension = $files[0]['extension'];
if ( $extension === 'json' )
{
$json = file_get_contents( $file );
if ( ! $json )
{
XBRL_Log::getInstance()->warning( "Failed to open JSON store" );
return false;
}
}
else if ( $extension === 'zip' )
{
$zip = new ZipArchive();
if ( $zip->open( $file ) === true )
{
$json = $zip->getFromName( pathinfo( $file, PATHINFO_FILENAME ) . '.json' );
$zip->close();
} else
{
XBRL_Log::getInstance()->err( 'Failed to open zip file $file' );
return false;
}
}
else
{
XBRL_Log::getInstance()->err( "The requested file type ($extension) is not supported" );
return false;
}
if ( empty( $json ) )
{
XBRL_Log::getInstance()->err( "The required json in $file does not exist." );
return false;
}
$xbrl = XBRL::fromJSON( $json, dirname( $file ) );
if ( $xbrl === false )
{
XBRL_Log::getInstance()->err( "The taxonomy DTS contained in the file could not be created" );
return false;
}
$xbrl->afterMainTaxonomy();
return $xbrl;
}
/**
* This is a special case constructor for the 'main' instance so
* so additional processing can be done *after* the schemas have
* been loaded
* @param string[]|string $taxonomy_xsd The file containing the taxonomy xsd or an array containing a list of file to load
* @param boolean $useCache True if the the cache should be used
* @param string $cacheLocation The location of the cache. Null or not provide will use the default
*/
public static function withTaxonomy( $taxonomy_xsd, $useCache = false, $cacheLocation = null )
{
$context = XBRL_Global::getInstance();
if ( $useCache && ! $context->useCache )
{
$context->useCache = true;
$context->cacheLocation = $cacheLocation;
$context->initializeCache();
}
$taxonomy = XBRL::preProcessSchemaFile( $taxonomy_xsd );
if ( ! $taxonomy ) return $taxonomy;
XBRL::postProcessSchemaFile( $taxonomy_xsd );
return $taxonomy;
}
/**
* Process a set of schema files
*
* @param string|string[] $taxonomy_xsd
* @param number $depth
* @return NULL|XBRL
*/
private static function preProcessSchemaFile( $taxonomy_xsd, $depth = 0 )
{
$context = XBRL_Global::getInstance();
if ( ! is_array( $taxonomy_xsd ) )
{
// Make sure any fragments are removed
$parts = explode( "#", $taxonomy_xsd );
$taxonomy_xsd = $parts[0];
if ( empty( $taxonomy_xsd ) )
{
XBRL_Log::getInstance()->warning( "The taxonomy file name supplied is empty" );
return null;
}
$taxonomy_xsd = array( $taxonomy_xsd );
}
$processXsd = function( $depth = 0 ) use( &$processXsd, &$taxonomy_xsd, &$context )
{
if ( ! count( $taxonomy_xsd ) ) return false;
$taxonomyXsdFile = array_shift( $taxonomy_xsd );
$xbrlDocument = XBRL::getXml( $taxonomyXsdFile, $context );
if ( ! $xbrlDocument instanceof SimpleXMLElement)
{
// XBRL_Log::getInstance()->warning( "Unable to load taxonomy: $taxonomyXsdFile" );
XBRL_Log::getInstance()->instance_validation('4.2', "The schema file cannot be located relative to the instance document", array(
'name' => $taxonomyXsdFile
) );
return null;
}
if ( $xbrlDocument->getName() != "schema" )
{
XBRL_Log::getInstance()->taxonomy_validation( "5.1", "The file is not a schema file because the root element is not 'schema'.",
array(
'root' => $xbrlDocument->getName(),
'file' => $taxonomyXsdFile,
)
);
return null;
}
$namespace = (string) $xbrlDocument['targetNamespace'];
if ( isset( $context->importedSchemas[ $namespace ] ) )
{
$taxonomy = $context->importedSchemas[ $namespace ];
if ( ! property_exists( $taxonomy, 'xbrlDocument' ) || ! $taxonomy->xbrlDocument )
{
// $taxonomy->xbrlDocument = $xbrlDocument;
}
if ( ! isset( $context->schemaFileToNamespace[ $taxonomyXsdFile ] ) )
{
$context->schemaFileToNamespace[ $taxonomyXsdFile ] = $namespace;
$context->schemaFileToNamespace[ basename( $taxonomyXsdFile ) ] = $namespace;
}
return $taxonomy;
}
/**
* @var XBRL $classname
*/
$classname = XBRL::class_from_namespace( $namespace );
/**
* @var XBRL $taxonomy_instance
*/
$taxonomy_instance = new $classname();
$taxonomy_instance->context =& $context;
if ( ! $taxonomy_instance->loadSchema( $taxonomyXsdFile, $xbrlDocument, $namespace, $depth + 1, $processXsd ) )
{
return null;
}
return $taxonomy_instance;
};
$taxonomy_instance = $processXsd( $depth );
return $taxonomy_instance;
}
/**
* Process a set of schema files
*
* @param string|string[] $taxonomy_xsd
* @param number $depth
* @return NULL|XBRL
*/
private static function postProcessSchemaFile( $taxonomy_xsd, $depth = 0 )
{
$context = XBRL_Global::getInstance();
if ( ! is_array( $taxonomy_xsd ) )
{
if ( empty( $taxonomy_xsd ) )
{
XBRL_Log::getInstance()->warning( "The taxonomy file name supplied is empty" );
return null;
}
$taxonomy_xsd = array( $taxonomy_xsd );
}
$processXsd = function( $depth = 0 ) use( &$processXsd, &$taxonomy_xsd, &$context )
{
if ( ! count( $taxonomy_xsd ) ) return false;
$taxonomyXsdFile = array_shift( $taxonomy_xsd );
// Get the existing taxonomy for the file
/**
* @var XBRL $taxonomy
*/
$taxonomy = $context->getTaxonomyForXSD( $taxonomyXsdFile );
if ( ! $taxonomy )
{
XBRL_Log::getInstance()->warning( "The taxonomy for '$taxonomyXsdFile' cannot be found." );
return;
}
// If there is no xbrl document then the taxonomy has been loaded from a compiled taxonomy so linkbases will be loaded
if ( $taxonomy->xbrlDocument )
{
$taxonomy->loadLinkbases( $depth + 1, $processXsd );
}
return $taxonomy;
};
/**
* @var \XBRL $taxonomy
*/
$taxonomy = $processXsd( $depth );
if ( $taxonomy )
{
if ( $taxonomy->xbrlDocument && \XBRL::isValidating() )
{
// Look for circular references in each of the extended links
foreach ( $taxonomy->context->presentationRoleRefs as $role => $roleRef )
{
// If there is no hierarchy there can be no circular references
if ( ! isset( $roleRef['hierarchy'] ) ) continue;
$taxonomy->validateDirectedCycles( 'presentation', $role, $roleRef['hierarchy'], array(),
function( $role, $result, $linkbase ) {
XBRL_Log::getInstance()->taxonomy_validation( "5.2.4.2", "The linkbase contains circular references which are not permitted",
array(
'role' => "'$role'",
'node' => "'$result'",
'linkbase' => "'$linkbase'",
'error' => 'xbrldte:DRSDirectedCycleError',
)
);
}
);
}
}
// Look at the linkbases to determine if there are any definition additions that need processing
// if ( $taxonomy->linkbaseRefExists( XBRL_Constants::$DefinitionLinkbaseRef ) )
{
$taxonomy->validateDimensions( true );
}
// Look at the linkbases to determine if there are any definition additions that need processing
if ( $taxonomy->linkbaseRefExists( XBRL_Constants::$DefinitionLinkbaseRef ) ||
$taxonomy->linkbaseRefExists( XBRL_Constants::$DefinitionLinkbaseRef ) )
{
$taxonomy->fixupPresentationHypercubes();
}
$taxonomy->afterMainTaxonomy();
}
return $taxonomy;
}
/**
* Returns true if a linkbase ref exists with $linkbaseRefType for the taxonomy
* @param string $linkbaseRefType One of the standard linkbaseRef constants such as XBRL_Constants::$DefinitionLinkbaseRef
*/
private function linkbaseRefExists( $linkbaseRefType )
{
return isset( $this->linkbaseTypes[ $linkbaseRefType ] ) &&
count( $this->linkbaseTypes[ $linkbaseRefType ] );
}
/**
* Provides an opportunity for a descendant class implemenentation to take action after the main taxonomy is loaded
*/
public function afterMainTaxonomy()
{
// Do nothing
}
/**
* Provides an opportunity for a descendant class implemenentation to take action after each taxonomy is loaded
* @param string $taxonomy_schema
*/
protected function afterLoadTaxonomy( $taxonomy_schema )
{
// Do nothing
}
/**
* Provides an opportunity for a descendant class implemenentation to take action before each taxonomy is loaded
* @param string $taxonomy_schema
*/
protected function beforeLoadTaxonomy( $taxonomy_schema )
{
// Do nothing
}
/**
* Load a taxonomy from a store created from a .json file of an extension taxonomy (has a valid 'baseTaxonomy' element).
* @param array $store
* @param string $compiledFolder (optional)
* @return boolean|XBRL The resulting taxonomy instance or false if one cannot be created