-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathXBRL-Instance.php
7584 lines (6516 loc) · 249 KB
/
XBRL-Instance.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
/**
* XBRL Instance
* @author Bill Seddon
* @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\Exceptions\FormulasException;
use lyquidity\xml\QName;
/**
* XBRL instance document class
*
* @author Bill Seddon
* @version 0.9
*
* Copyright (C) 2016 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/>.
*
*/
/**
* Allow the simple xml element to be extended
*/
$utilitiesPath = isset( $_ENV['UTILITIIES_LIBRARY_PATH'] )
? $_ENV['UTILITIES_LIBRARY_PATH']
: ( defined( 'UTILITIES_LIBRARY_PATH' ) ? UTILITIES_LIBRARY_PATH : __DIR__ . "/../utilities/" );
require_once $utilitiesPath . 'SimpleXMLElementToArray.php';
require_once $utilitiesPath . 'tuple-dictionary.php';
/**
* Provides functions to read and interpret instance documents
* If an instance document references a schema that has not been loaded it will
* be loaded and stored in a static array indexed by the schema filename.
* @author Bill Seddon
*/
class XBRL_Instance
{
/**
* A static array of instances so the same taxonomy is used
* @var array
*/
private static $instance_taxonomy = array();
/**
* A list of the units defined in the instance document
* @var array
*/
private $units = array();
/**
* A list of the contexts defined in the instance document
* @var array
*/
private $contexts = array();
/**
* A list of the segments defined in the instance document
* @var array
*/
private $segments = array();
/**
* A list of the elements defined in the instance document
* @var array
*/
private $elements = array();
/**
* The instance document XML
* @var SimpleXMLElement
*/
private $instance_xml = null;
/**
* The namespaces in the instance document
* @var array
*/
private $instance_namespaces = array();
/**
* The name of the instance document
* @var string
*/
private $schemaFilename = "";
/**
* An array of taxonomies (XBRL instances) associated with the instance document indexed by namespace
* @var array
*/
private $taxonomyToNamespaceMap = null;
/**
* The currency of the instance document
* @var string
*/
private $defaultCurrency = "USD";
/**
* The name of the document used for this instance
* @var string
*/
private $document_name = "";
/**
* A unique identifier for the document
* @var string
*/
private $guid = null;
/**
* Any error associated with problems parsing the instance document
* @var string
*/
private $error = null;
/**
* A list of references to tuples
* @var array $tupleRefs
*/
private $tupleRefs = array();
/**
* An array of footnote arcs and texts. The array will have two elements:
* One indexed by:
* [the standard footnote role]
* [lang]
* [footnote label] = text
*
* The other indexed by 'arcs'
* [element id]
* [0] = footnote label 1
* [1] = footnote label x
* ...
*
* @var array
*/
private $footnotes = array();
/**
* An array indexed by the guid of fact entries. This behaves like
* clustered index.
*
* The content of each item in the array will be the id of the element
* holding the corresponding fact entry. A fact entry can then be retrieved
* by accessing the element based on the id held against the guid and then
* searching for the guid within the list of entries assigned to the
* element. Usually be only two or three so the lookup time will be tiny.
*
* @var array
*/
private $uniqueFactIds = array();
/**
* An array of the contexts being used in the report
* @var array
*/
public $usedContexts = array();
/**
* True if the caller wants to allow one or more <xbrl> containers to appear in a larger document
* @var bool $allowNested
*/
public $allowNested = false;
/**
* Location if there is one of compiled taxonomies
* @var string
*/
private $compiledLocation = null;
/**
* The name of the class to initialize
* @var string (Defaults to 'XBRL')
*/
private $className = 'XBRL';
/**
* Create an instance from an instance document using a base taxonomy if possible
*
* @param string $instance_document The file containing the instance information
* @param string $compiledLocation The location of compiled taxonomies
* @param string $className The name of an XBRL class to initialize (defaults to 'XBRL')
* @param bool $useCache (default: false) If true the instance document will be read from the cache
* @return XBRL_Instance|bool
*/
public static function FromInstanceDocumentWithExtensionTaxonomy( $instance_document, $compiledLocation, $className = 'XBRL', $useCache = false )
{
try
{
$instance = new XBRL_Instance();
$instance->compiledLocation = $compiledLocation;
$instance->className = $className;
if ( ! $instance->initialise( $instance_document, null, $useCache ) )
{
return false;
}
}
catch ( FormulasException $ex)
{
throw $ex;
}
catch( Exception $ex )
{
$instance->error = "Error initialising the instance document. It may contain invalid XML.\n";
return false;
}
return $instance;
}
/**
* Create an instance from an instance document
*
* @param string $instance_document The file containing the instance information
* @param string $taxonomy_file The taxonomy for the instance document
* @param XBRL_Instance $instance A reference to the instance created by this call
* @param bool $allowNested (optional: false) True if the caller wants to allow one or more <xbrl> containers to appear in a larger document
* @param bool $useCache (default: false) If true the instance document will be read from the cache
* @return XBRL_Instance|bool
*/
public static function FromInstanceDocument( $instance_document, $taxonomy_file = null, &$instance = null, $allowNested = false, $useCache = false )
{
try
{
$instance = new XBRL_Instance();
$instance->allowNested = $allowNested;
if ( ! $instance->initialise( $instance_document, $taxonomy_file, $useCache ) )
{
return false;
}
}
catch ( FormulasException $ex)
{
throw $ex;
}
catch( Exception $ex )
{
$instance->error = "Error initialising the instance document. It may contain invalid XML.\n";
return false;
}
return $instance;
}
/**
* Creates an instance object from a JSON string, perhaps in a zip file
* @param string $cache_path
* @param string $cache_basename
* @param string $taxonomyNamespace
* @param string $compiledTaxonomyFile
* @param \SimpleXMLElement $originalXml
* @return XBRL_Instance
*/
public static function FromInstanceCache( $cache_path, $cache_basename, $taxonomyNamespace, $compiledTaxonomyFile, $originalXml = null )
{
$xbrl = XBRL::load_taxonomy( $compiledTaxonomyFile );
if ( ! $xbrl ) return false;
$json = null;
if ( file_exists( $cache_basename ) )
{
$json = file_get_contents( $cache_basename );
}
else if ( XBRL::endsWith( $cache_basename, '.zip' ) )
{
$zip = new \ZipArchive();
$zip->open( "$cache_path/$cache_basename" );
$json = $zip->getFromName( basename( $cache_basename, '.zip' ) . '.json' );
}
else
{
$json = file_get_contents( "$cache_path/$cache_basename" );
}
$instance = new XBRL_Instance();
$array = json_decode( $json, true );
$instance->allowNested = $array['allowNested'];
$instance->contextDimensionMemberList = $array['contextDimensionMemberList'];
$instance->contexts = $array['contexts'];
$instance->document_name = $array['document_name'];
$instance->duplicateFacts = TupleDictionary::fromJSON( $array['duplicateFacts'] );
$instance->elements = $array['elements'];
$instance->error = $array['error'];
$instance->footnotes = $array['footnotes'];
$instance->guid = $array['guid'];
$instance->instance_namespaces = $array['instance_namespaces'];
$instance->segments = $array['segments'];
$instance->tupleRefs = $array['tupleRefs'];
$instance->uniqueFactIds = $array['uniqueFactIds'];
$instance->units = $array['units'];
$instance->usedContexts = $array['usedContexts'];
// Look for it in the same folder to begin with
if ( $originalXml )
{
$xml = $originalXml;
}
else
{
$xml = XBRL::getXml( $instance->document_name, XBRL_Global::getInstance() );
}
if ( $xml )
{
$instance->instance_xml = $xml;
}
$taxonomy = $xbrl->getTaxonomyForNamespace( $taxonomyNamespace );
XBRL_Instance::$instance_taxonomy[ $taxonomy->getSchemaLocation() ] = $taxonomy;
$instance->schemaFilename = $taxonomy->getSchemaLocation();
$instance->defaultCurrency = $taxonomy->getDefaultCurrency();
$instance->taxonomyToNamespaceMap = $taxonomy->getImportedSchemas();
return $instance;
}
/**
* Convert the instance to a JSON string
* @return string
*/
public function toJSON()
{
return json_encode( array(
// 'instance_taxonomy' => $this->instance_taxonomy,
'allowNested' => $this->allowNested,
// 'cacheContextElements' => $this->cacheContextElements,
// 'cacheDocumentNamespaces' => $this->cacheDocumentNamespaces,
// 'cacheNamespaces' => $this->cacheNamespaces,
'contextDimensionMemberList' => $this->contextDimensionMemberList,
'contexts' => $this->contexts,
// 'defaultCurrency' => $this->defaultCurrency,
'document_name' => $this->document_name,
'duplicateFacts' => $this->duplicateFacts->toJSON(),
'elements' => $this->elements,
'error' => $this->error,
'footnotes' => $this->footnotes,
'guid' => $this->guid,
'instance_namespaces' => $this->instance_namespaces,
// 'instance_xml' => $this->instance_xml,
// 'schemaFilename' => $this->schemaFilename,
'segments' => $this->segments,
// 'taxonomyToNamespaceMap' => array_keys( $this->taxonomyToNamespaceMap ),
'tupleRefs' => $this->tupleRefs,
'uniqueFactIds' => $this->uniqueFactIds,
'units' => $this->units,
'usedContexts' => $this->usedContexts,
) );
}
/**
* Perist an instance to a file containing a JSON representation
* @param string $output_path
* @param string $output_basename
* @return bool
*/
public function toInstanceCache( $output_path, $output_basename )
{
file_put_contents( "$output_path/$output_basename.json", $this->toJSON() );
$zip = new ZipArchive();
$zip->open( "$output_path/$output_basename.zip", ZipArchive::CREATE | ZipArchive::OVERWRITE );
$zip->addFile( "$output_path/$output_basename.json", "$output_basename.json" );
if ( $zip->close() === false )
{
XBRL_Log::getInstance()->err( "Error closing zip file" );
XBRL_Log::getInstance()->err( $zip->getStatusString() );
}
}
/**
* Returns the type of the element
* @param string $element The element from which to access the type
* @return string The type as a string
*/
public static function getElementType( $element )
{
return isset( $element['taxonomy_element'] ) ? $element['taxonomy_element']['type'] : "";
}
/**
* Resets the lists of existing instance taxonomies
* @param string $resetGlobal
*/
public static function reset( $resetGlobal = true )
{
XBRL_Instance::$instance_taxonomy = array();
if ( ! $resetGlobal ) return;
XBRL_Global::reset();
XBRL_Types::reset();
}
/**
* The original name of the instance document
* @return string
*/
public function getDocumentName()
{
return $this->document_name;
}
/**
* Get the entity of the element. For a simple concept this is easy but for a tuple its necessary to look at the tuple elements
* @param array $element
* @return string The entity of the element
*/
public function getEntityForElement( $element )
{
if ( isset( $element['contextRef'] ) )
{
return $this->contexts[ $element['contextRef'] ]['entity']['identifier']['value'];
}
if ( isset( $element['tuple_elements'] ) )
{
foreach ( $element['tuple_elements'] as $elementKey => $tuple_elements )
{
foreach ( $tuple_elements as $tupleIndex => $tuple_element )
{
$result = $this->getEntityForElement( $tuple_element );
if ( $result ) return $result;
}
}
}
throw new Exception( "The element '{$element['taxonomy_element']['id']}' has no context ref but also has no tuple members (getEntityForElement)" );
}
/**
* Get the year of the element. For a tuple concept return false.
* @param array $element
* @return string The entity of the element
*/
public function getYearForElement( $element )
{
return isset( $element['contextRef'] ) ? substr( $this->contexts[ $element['contextRef'] ]['period']['endDate'], 0, 4 ) : false;
}
/**
* Any information about document processing issues
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* A unique identifier for this instance
* @return string
*/
public function getGuid()
{
return $this->guid;
}
/**
* Get the elements from the instance document
* @return InstanceElementsFilter
*/
public function getElements()
{
return new InstanceElementsFilter( $this, $this->elements );
}
/**
* Get the instance namespaces
*/
public function &getInstanceNamespaces()
{
return $this->instance_namespaces;
}
/**
* Add a namespace to the list
* @param string $prefix
* @param string $namespace
* @return void
*/
public function addNamespace( $prefix, $namespace )
{
$this->instance_namespaces[ $prefix ] = $namespace;
$this->cacheNamespaces = null;
}
/**
* Get the SimpleXMLElement of the document
* @return SimpleXMLElement
*/
public function getInstanceXml()
{
return $this->instance_xml;
}
/**
* Get the taxonomy for the instance document
* @return XBRL
*/
public function &getInstanceTaxonomy()
{
return XBRL_Instance::$instance_taxonomy[ $this->schemaFilename ];
}
/**
* Get the instance namespace for a specific prefix
* @param string $prefix
* @return string The corresponding namespace or null
*/
public function getNamespaceForPrefix( $prefix )
{
if ( ! isset( $this->instance_namespaces[ $prefix ] ) ) return null;
return $this->instance_namespaces[ $prefix ];
}
/**
* Get the segments from the instance document
* @return array
*/
public function getSegments()
{
return $this->segments;
}
/**
* Get the contexts from the instance document
* @return ContextsFilter
*/
public function getContexts()
{
return new ContextsFilter( $this, $this->contexts );
}
/**
* Get a specific context
* @param string $id The id of the context to retrieve
* @return array|null
*/
public function getContext( $id )
{
return isset( $this->contexts[ $id ] ) ? $this->contexts[ $id ] : null;
}
/**
* Returns true if the dimension reference can be found in a segment or scenario
* @param string $contextRef
* @param string $dimension
* @param boolean $includeDefault
* @return boolean
*/
public function hasDimension( $contextRef, $dimension, $includeDefault = false )
{
if ( ! isset( $this->contexts[ $contextRef ] ) )
{
return false;
}
$paths = array(
array( 'entity', 'segment' ),
array( 'scenario' ),
array( 'entity', 'scenario' ),
array( 'segment' ),
);
foreach ( $paths as $path )
{
$context = $this->contexts[ $contextRef ];
foreach ( $path as $element )
{
if ( ! isset( $context[ $element ] ) ) continue 2;
$context = $context[ $element ];
}
if ( isset( $context['explicitMember'] ) )
{
foreach ( $context['explicitMember'] as $dimensionDefinition )
{
if ( $dimensionDefinition['dimension'] == $dimension ) return true;
}
}
if ( isset( $context['typedMember'] ) )
{
foreach ( $context['typedMember'] as $dimensionDefinition )
{
if ( $dimensionDefinition['dimension'] == $dimension ) return true;
}
}
}
return false;
}
/**
* Returns true if the $dimension has a default
* @param string $dimension
* @return boolean
*/
public function hasDefaultDimensionMember( $dimension )
{
$qname = qname( $dimension, $this->instance_namespaces );
$taxonomy = $this->getInstanceTaxonomy()->getTaxonomyForNamespace( $qname->namespaceURI );
$element = $taxonomy->getElementByName( $qname->localName );
if ( isset( $taxonomy->context->dimensionDefaults[ "{$taxonomy->getTaxonomyXSD()}#{$element['id']}" ] ) )
{
return true;
}
return false;
}
/**
* Get the default currency
* @return string
*/
public function getDefaultCurrency()
{
return $this->defaultCurrency;
}
/**
* Set the default currency
* @param string $defaultCurrency The default currency to use
* @return void
*/
public function setDefaultCurrency( $defaultCurrency )
{
$this->defaultCurrency = $defaultCurrency;
}
/**
* Get the schema file name of the instances used in this report
* @return string
*/
public function getSchemaFilename()
{
return $this->schemaFilename;
}
/**
* Get the units of the instances used in this report
* @return array
*/
public function getUnits()
{
return $this->units;
}
/**
* Get a specific unit
* @param string $id The id of the unit to retrieve
* @return string|null
*/
public function getUnit( $id )
{
return isset( $this->units[ $id ] ) ? $this->units[ $id ] : null;
}
/**
* Get a specific element
* @param string $id The id of the element to retrieve
* @return string[][]|null
*/
public function getElement( $id )
{
$result = array();
if ( isset( $this->elements[ $id ] ) )
{
foreach ( $this->elements[ $id ] as $key => &$element )
{
if ( isset( $element['parent'] ) ) continue;
$element['parent'] = 'xbrl';
}
unset( $element );
$result = $this->elements[ $id ];
}
if ( ! isset( $this->tupleRefs[ $id ] ) )
{
return $result;
}
// The rest of this needs sorting
$tuple = $this->tupleRefs[ $id ];
$tupleId = key( $tuple );
if ( ! isset( $this->elements[ $tupleId ] ) )
{
return $result;
}
// $fact = array();
foreach ( $tuple[ $tupleId ] as $index )
{
foreach ( $this->elements[ $tupleId ][ $index ]['tuple_elements'][ $id ] as $key => $entry )
{
if ( ! isset( $entry['tupleid'] ) )
{
$entry['tupleid'] = $tupleId;
}
$result[ $entry['guid'] ] = $entry;
}
}
return $result;
}
/**
* Add an entry to a fact element id
*
* @param string $id The id if the fact being recorded
* @param array $entry The entry arry to be recorded
*/
private function addElement( $id, $entry )
{
$this->elements[ $id ][] = $entry;
}
/**
* A cache of a reverse lookup of instance document namespaces
* @var array
*/
private $cacheDocumentNamespaces = null;
/**
* Returns a prefix for an xbrlInstance namespace
* This only looks at the namespaces in the root of the instance dopcument
* @param string $namespace
*/
public function getPrefixForDocumentNamespace( $namespace )
{
if ( is_null( $this->cacheDocumentNamespaces ) )
{
$this->cacheDocumentNamespaces = array_flip( $this->instance_xml->getDocNamespaces() );
// If the default namespace is one of the standards ones then the prefix may be missing
$standardNamespaces = array_flip( XBRL_Constants::$standardPrefixes );
$docNamespace = array_search( '', $this->cacheDocumentNamespaces );
if ( $docNamespace )
{
if ( isset( $standardNamespaces[ $docNamespace ] ) )
{
$this->cacheDocumentNamespaces[ $docNamespace ] = $standardNamespaces[ $docNamespace ];
}
else
{
// Get the namespace for the respective namespace
$taxonomy = $this->getInstanceTaxonomy()->getTaxonomyForNamespace( $docNamespace );
if ( $taxonomy )
{
$this->cacheDocumentNamespaces[ $docNamespace ] = $taxonomy->getPrefix();
}
}
}
}
return isset( $this->cacheDocumentNamespaces[ $namespace ] )
? $this->cacheDocumentNamespaces[ $namespace ]
: false;
}
/**
* A cache of a reverse lookup of $this->instance_namespaces
* @var array
*/
private $cacheNamespaces;
/**
* Get the prefix of a namespace
*
* @param string $namespace
* @return string|false Will return the prefix or false if one does not exist
*/
public function getPrefixForNamespace( $namespace )
{
if (
! isset( $this->cacheNamespaces ) ||
count( $this->cacheNamespaces ) != count( $this->instance_namespaces )
)
{
$this->cacheNamespaces = array_flip( $this->instance_namespaces );
// If the default namespace is one of the standards ones then the prefix may be missing
$standardNamespaces = array_flip( XBRL_Constants::$standardPrefixes );
if ( isset( $this->instance_namespaces[''] ) )
{
if ( isset( $standardNamespaces[ $this->instance_namespaces[''] ] ) )
{
$this->cacheNamespaces[ $this->instance_namespaces[''] ] = $standardNamespaces[ $this->instance_namespaces[''] ];
}
}
}
return isset( $this->cacheNamespaces[ $namespace ] )
? $this->cacheNamespaces[ $namespace ]
: false;
}
/**
* Get the taxonomy associated with a namespace
*
* @param string $namespace
* @return XBRL|false Will return the XBRL representation of the schema or false if one does not exist
*/
public function getTaxonomyForNamespace( $namespace )
{
if ( ! isset( $this->taxonomyToNamespaceMap[ $namespace ] ) ) return false;
return $this->taxonomyToNamespaceMap[ $namespace ];
}
/**
* Get the segment of the context
* @param array $context
* @return array|NULL
*/
public function getContextSegment( $context )
{
return isset( $context['entity']['segment'] )
? $context['entity']['segment']
: ( isset( $context['entity']['scenario'] )
? $context['entity']['scenario']
: (isset( $context['segment'] )
? $context['segment']
: ( isset( $context['scenario'] )
? $context['scenario']
: null
)
)
);
}
/**
* Caches context refs so they do not need to be accessed more than once
* @var array $cacheContextElements
*/
private $cacheContextElements = array();
/**
* Get the log instance
* @return XBRL_Log
*/
private function log()
{
return XBRL_Log::getInstance();
}
/**
* Returns an entry for a guid or false
* @param string $guid
* @return array|false
*/
private function &getEntryForGuid( $guid )
{
if ( ! isset( $this->uniqueFactIds[ $guid ] ) )
{
return false;
}
// Get the element
$facts = $this->getElement( $this->uniqueFactIds[ $guid ] );
// And find the entry with the corresponding guid
foreach ( $facts as $key => &$entry )
{
if ( $entry['guid'] == $guid ) return $entry;
}
return false;
}
/**
* Get an array of explicit dimension member elements for a context
*
* @param array|string $contextRef If a string the name of a context; if an array, one containing the context detail
* @param boolean|array[string] $getText If true include the element text in the result. If the argument is an array it will be an array of preferred labels.
* @param string $elementName The name of the element within the entity element to use [segment|scenario]
* @return string[][] False if the requested context id does not exist or the context is not dimensional otherwise any array of dimension member elements
* @TODO If the context does not contain an explicitMember element the function should check the dimensional taxonomy.
* This will require that the primary element is passed as an argument
*/
public function getElementsForContext( $contextRef, $getText = false, $elementName = '' )
{
if ( isset( $this->cacheContextElements[ $contextRef ][ $elementName ] ) )
{
return $this->cacheContextElements[ $contextRef ][ $elementName ];
}
if ( $elementName && ( $elementName !== 'segment' && $elementName !== 'scenario' ) )
{
$this->log()->err( "The element name must be 'segment' or 'scenario'" );
return false;
}
// If the context parameter provided is a string then lookup the context
if ( is_string( $contextRef ) )
{
if ( ! isset( $this->contexts[ $contextRef ] ) )
{
$this->log()->err( "The context '$contextRef' does not exist" );
return false;
}
$context = $this->contexts[ $contextRef ];
}
else
$context = $contextRef;
// print_r($context);
if ( ! isset( $context['entity'] ) )
{
// $this->log()->err( "Cannot find element 'entity' in the context array for context reference '$contextRef'." );
return false;
}
$entity = $context['entity'];
$segment = $elementName
? ( isset( $context[ $elementName ] )
? $context[ $elementName ]
: null )
: $this->getContextSegment( $context );
if ( is_null( $segment ) )
{
if ( ! isset( $entity[ $elementName ] ) )
{
// $this->log()->err( "Cannot find element '$elementName' in the 'entity' element of the context array for context reference '$contextRef'." );
$this->cacheContextElements[ $contextRef ][ $elementName ] = false;
return false;
}
$segment = $entity[ $elementName ];
}
if ( isset( $segment['explicitMember'] ) || isset( $segment['typedMember'] ) || isset( $segment['member'] ) )
{
$elements = array();
if ( isset( $segment['explicitMember'] ) )
{
$segmentMember = $segment['explicitMember'];
foreach ( $segmentMember as $key => $member )
{
if ( ! isset( $member['dimension'] ) || ! isset( $member['member'] ) )
{
$this->log()->err( "The dimension or member component element of the context cannot be found" );
continue;
}
$components = array( 'type' => 'explicitMember' );
$elementNames = array( 'dimension', 'member' );
foreach ( $elementNames as $name )
{
$component = $member[ $name ];
$parts = explode( ":", $component );
$prefix = $parts[0];
$componentName = $parts[1];
$components[ $name ] = $this->getElementForReference( $prefix, $componentName, $getText );
if ( ! $components[ $name ] )
{
$components[ $name ] = array( 'namespace' => $parts[0], 'element' => $parts[1] );
}
}
$elements[] = $components;
}
}