-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathXBRL-Equality.php
2239 lines (1959 loc) · 73.4 KB
/
XBRL-Equality.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 specification equality tests
*
* @author Bill Seddon
* @version 0.9
* @Copyright (C) 2017 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;
/**
* A static class that provides methods to test the equality of XBRL features
*
* The XBRL specification defines several different types of equality:
*
* C-Equal Context-equal: Items or sets or sequences of items having the same item type in s-equal contexts.
* P-Equal Parent-equal: instance items or tuples having the same parent.
* S-Equal Structure-equal: XML nodes that are either equal in the XML value space, or whose XBRL-relevant
* sub-elements and attributes are s-equal.
* U-Equal Unit-equal. u-equal numeric items having the same units of measurement.
* V-Equal Value-equal: c-equal items having either the same non-numeric value, or numeric values that are
* equal within some tolerance defined by the lesser of their respective @precision, implied @precision
* or @decimals attributes.
*
* In addition these rely on more basic equalities:
*
* X-Equal [XPath 1.0]-equal: The XPath "=" operator returns the value true.
* A-Equal Attribute-equal: The two attributes have local names and namespaces that are S-Equal and have values that are X-Equal
*/
class XBRL_Equality {
/** -------------------------------------------------------------------------------
*
* Public static variables
*
* ------------------------------------------------------------------------------- */
/**
* A comparison type representing a string value
* @var integer
*/
public static $EQUALITY_TYPE_STRING = 0;
/**
* A comparison type reprensenting a value of one of the types xs:decimal, xs:float, or xs:double
* @var integer
*/
public static $EQUALITY_TYPE_NUMBER = 1;
/**
* A comparison type reprensentng a value the type xs:boolean
* @var integer
*/
public static $EQUALITY_TYPE_BOOLEAN = 2;
/** -------------------------------------------------------------------------------
*
* private variables
*
* ------------------------------------------------------------------------------- */
/**
* An instance to use the evaluate XPath equality queries
*
* @var SimpleXMLElement
*/
private static $doc = null;
/** -------------------------------------------------------------------------------
*
* Public functions
*
* ------------------------------------------------------------------------------- */
/**
* The two attributes have local names and namespaces that are S-Equal and have values that are X-Equal
*
* @param string $value1 An attribute in the form 'localname' or 'prefix:localname'
* @param string $value2 An attribute in the form 'localname' or 'prefix:localname'
* @param string $type1 The type of $value1 (default: XBRL_Equality::$EQUALITY_TYPE_STRING)
* @param string $type2 The type of $value2 (default: XBRL_Equality::$EQUALITY_TYPE_STRING)
* @param array $namespaces A list of namespaces indexed by prefix from the current document
* @param XBRL_Types $types A reference to an XBRL_Types instance
* @return bool
*/
public static function attribute_equal( $value1, $value2, $type1, $type2, &$namespaces, &$types = null )
{
$qname1 = qname( $value1, $namespaces );
$qname2 = qname( $value2, $namespaces );
if ( ! is_null( $qname1 ) && ! is_null( $qname1 ) )
{
// The qnames should be equivalent
if ( ! $qname1->equals( $qname2 ) ) return false;
$value1 = $qname1->localName;
$value2 = $qname1->localName;
}
else if ( is_null( $qname1 ) || is_null( $qname1 ) )
{
return false;
}
return XBRL_Equality::xequal( $value1, $value2, $type1, $type2, $types );
}
/**
* <period> elements are S-Equal, and <entity> elements are S-Equal, and <scenario> elements are S-Equal.
*
* @param string $c1
* @param string $c2
*
* return bool
*/
public static function context_equal( $c1, $c2 )
{
if ( is_null( $c1 ) && is_null( $c2 ) ) return true;
if ( is_null( $c1 ) || is_null( $c2 ) ) return false;
// Check the arrays have the same elements
$result = XBRL_Equality::matchedKeyNames( $c1, $c2, array( 'entity', 'period', 'scenario' ) );
// if ( $result === false || ! is_array( $result ) || ! count( $result ) ) return false;
if ( $result === false || ! is_array( $result ) ) return false;
if ( ! count( $result ) ) return true;
foreach ( $result as $elementName )
{
switch ( $elementName )
{
case 'entity':
if ( ! XBRL_Equality::entity_equal( $c1['entity'], $c2['entity'] ) )
{
return false;
}
break;
case 'period':
if ( ! XBRL_Equality::period_equal( $c1['period'], $c2['period'] ) )
{
return false;
}
break;
case 'scenario':
if ( ! XBRL_Equality::segment_equal( $c1['scenario'], $c2['scenario'] ) )
{
return false;
}
break;
}
}
return true;
}
/**
* Make sure divide definitions are equal
*
* @param array $d1 An array of numerator and denominator elements
* @param array $d2 An array of numerator and denominator elements
* @param array $types A reference to the global XBRL_Types instance
* @param array $namespaces A list of the namespaces in the current document
* @return false|array
*/
public static function divide_equal( $d1, $d2, &$types, &$namespaces )
{
if ( is_null( $d1 ) && is_null( $d2 ) ) return true;
if ( is_null( $d1 ) || is_null( $d2 ) ) return false;
// Check the arrays have the same elements
$result = XBRL_Equality::matchedKeyNames( $d1, $d2, array( 'denominator', 'numerator' ) );
// if ( $result === false || ! is_array( $result ) || ! count( $result ) ) return false;
if ( $result === false || ! is_array( $result ) ) return false;
if ( ! count( $result ) ) return true;
foreach ( $result as $elementName )
{
switch ( $elementName )
{
case 'denominator':
if ( ! XBRL_Equality::measures_equal( $d1['denominator'], $d2['denominator'], $types, $namespaces ) )
{
return false;
}
break;
case 'numerator':
if ( ! XBRL_Equality::measures_equal( $d1['numerator'], $d2['numerator'], $types, $namespaces ) )
{
return false;
}
break;
}
}
return true;
}
/**
* <identifier> elements are S-Equal, and <segment> elements are S-Equal (with any missing segment
* treated as S-Equal to an empty <segment> element).
*
* @param string $e1
* @param string $e2
*
* return bool
*/
public static function entity_equal( $e1, $e2 )
{
if ( is_null( $e1 ) && is_null( $e2 ) ) return true;
if ( is_null( $e1 ) || is_null( $e2 ) ) return false;
// Check the arrays have the same elements
$result = XBRL_Equality::matchedKeyNames( $e1, $e2, array( 'identifier', 'segment' ) );
// if ( $result === false || ! is_array( $result ) || ! count( $result ) ) return false;
if ( $result === false || ! is_array( $result ) ) return false;
if ( ! count( $result ) ) return true;
foreach ( $result as $elementName )
{
switch ( $elementName )
{
case 'identifier':
if ( ! XBRL_Equality::identifier_equal( $e1['identifier'], $e2['identifier'] ) )
{
return false;
}
break;
case 'segment':
if ( ! XBRL_Equality::segment_equal( $e1['segment'], $e2['segment'] ) )
{
return false;
}
break;
}
}
return true;
}
/**
* Identifiers should be S-Equal
*
* @param array $i1 A pair of elements holding the scheme and value respectively
* @param array $i2 A pair of elements holding the scheme and value respectively
*
* @return bool
*/
public static function identifier_equal( $i1, $i2 )
{
return XBRL_Equality::matchedElements( $i1, $i2, array( 'scheme', 'value' ), true );
}
/**
* Make sure measure definitions are equal but the order does not matter
* Why? Because two measures represent value that commute (A*B == B*A)
*
* @param array $m1 An array of measure arrays
* @param array $m2 An array of measure arrays
* @param array $types A reference to the global XBRL_Types instance
* @param array $namespaces A list of the namespaces in the current document
* @return false|array
*/
public static function measures_equal( $m1, $m2, &$types, &$namespaces )
{
if ( is_null( $m1 ) && is_null( $m2 ) ) return true;
if ( is_null( $m1 ) || is_null( $m2 ) ) return false;
// Must have the same number of items
if ( count( $m1 ) != count( $m2 ) ) return false;
$toQName = function( $measure ) use( $types, $namespaces ) {
$qname = qname( $measure, $namespaces );
return is_null( $qname )
? $measure
: $qname->clarkNotation();
};
// Need to match up QNames on one side with QNames on the other so convert to clark notation
$m1qnames = array_map( $toQName, $m1 );
$m2qnames = array_map( $toQName, $m2 );
// Sort them so that if they are the same they will be in the same order
sort( $m1qnames );
sort( $m2qnames );
// Keys should be identical on both sides (keys willl be numeric)
if (
array_diff( array_keys( $m1qnames ), array_keys( $m2qnames ) ) ||
array_diff( array_keys( $m2qnames ), array_keys( $m1qnames ) )
)
{
return false;
}
// Check each measure
foreach ( $m1qnames as $key => $measure )
{
if ( ! XBRL_Equality::attribute_equal( $measure, $m2qnames[ $key ], XBRL_Equality::$EQUALITY_TYPE_STRING, XBRL_Equality::$EQUALITY_TYPE_STRING, $namespaces, $types ) )
{
return false;
}
}
return true;
}
/**
* <segment> elements are S-Equal (with any missing segment treated as S-Equal to an empty <segment> element).
* @param array $s1
* @param array $s2
* @param bool $matchOrdinalPosition
* @return bool
*/
public static function segment_equal( $s1, $s2, $matchOrdinalPosition = false )
{
if ( is_null( $s1 ) && is_null( $s2 ) ) return true;
if ( is_null( $s1 ) || is_null( $s2 ) ) return false;
// Check the arrays have the same elements
$result = XBRL_Equality::matchedKeyNames( $s1, $s2, array( 'member', 'explicitMember', 'typedMember' ) );
// if ( $result === false || ! is_array( $result ) || ! count( $result ) ) return false;
if ( $result === false || ! is_array( $result ) ) return false;
if ( ! count( $result ) ) return true;
foreach ( $result as $elementName )
{
// Must have the same number of items
if ( count( $s1[ $elementName ] ) != count( $s2[ $elementName ] ) ) return false;
// Keys should be identical on both sides
if (
array_diff( array_keys( $s1[ $elementName ] ) , array_keys( $s2[ $elementName ] ) ) ||
array_diff( array_keys( $s2[ $elementName ] ) , array_keys( $s1[ $elementName ] ) )
)
{
return false;
}
$validNames = array();
switch ( $elementName )
{
case "member":
$validNames[] = 'name';
$validNames[] = 'member';
$validNames[] = 'children';
break;
case "explicitMember":
$validNames[] = 'dimension';
$validNames[] = 'member';
break;
case "typedMember":
$validNames[] = 'dimension';
$validNames[] = 'member';
// BMS 2019-09-14 Having a go at fixing this
// BMS 2018-02-18 TODO
// Should check 'member' but the xequal check doesn't support it yet
break;
}
foreach ( $s1[ $elementName ] as $key => $details1 )
{
$details2 = $s2[ $elementName ][ $key ];
if ( $matchOrdinalPosition )
{
if ( ( isset( $details1['ordinal'] ) && ! isset( $details2['ordinal'] ) ) ||
( ! isset( $details1['ordinal'] ) && isset( $details2['ordinal'] ) ) ||
( isset( $details1['ordinal'] ) && isset( $details2['ordinal'] ) &&
$details1['ordinal'] != $details2['ordinal']
)
)
{
return false;
}
// $validNames[] = "ordinal";
}
if ( ! XBRL_Equality::matchedElements( $details1, $details2, $validNames ) )
{
return false;
}
// If both do not have an attributes element then continue to the next
if ( ! isset( $details1['attributes'] ) && ! isset( $details2['attributes'] ) )
{
continue;
}
// If either are missing if means there is a mismatch
if ( ! isset( $details1['attributes'] ) || ! isset( $details2['attributes'] ) )
{
return false;
}
foreach ( $details1['attributes'] as $name => $attribute )
{
if ( ! XBRL_Equality::matchedElements( $attribute, $details2['attributes'][$name], array( 'name', 'value' ) ) )
{
return false;
}
}
}
}
return true;
}
/**
* Make sure the period elements are consistent
*
* @param array $p1
* @param array $p2
* @return false|array
*/
public static function period_equal( $p1, $p2 )
{
return XBRL_Equality::matchedElements( $p1, $p2, array( 'startDate', 'endDate', 'instant', 'forever' ), true );
}
/**
* Make sure unit definitions are equal
*
* @param array $u1
* @param array $u2
* @param array $types A reference to the global XBRL_Types instance
* @param array $namespaces A list of the namespaces in the current document
* @return false|array
*/
public static function unit_equal( $u1, $u2, &$types, &$namespaces )
{
if ( is_null( $u1 ) && is_null( $u2 ) ) return true;
if ( is_null( $u1 ) || is_null( $u2 ) ) return false;
// If one value is an array (divide) they must *both* be array.
// If one value is a string (measure) they MUST *both* be string.
if ( is_string( $u1 ) && is_string( $u2 ) )
{
// It's a string so it's a measure
return XBRL_Equality::attribute_equal( $u1, $u2, XBRL_Equality::$EQUALITY_TYPE_STRING, XBRL_Equality::$EQUALITY_TYPE_STRING, $namespaces, $types );
}
if ( ! is_array( $u1 ) || ! is_array( $u2 ) )
{
return false;
}
// Check the arrays have the same elements
$result = XBRL_Equality::matchedKeyNames( $u1, $u2, array( 'divide', 'measures' ) );
if ( $result === false || ! is_array( $result ) ) return false;
if ( ! count( $result ) ) return true;
foreach ( $result as $elementName )
{
// There will be a divide or there will be measures
switch ( $elementName )
{
case 'divide':
if ( ! XBRL_Equality::divide_equal( $u1['divide'], $u2['divide'], $types, $namespaces ) )
{
return false;
}
break;
case 'measures':
if ( ! XBRL_Equality::measures_equal( $u1['measures'], $u2['measures'], $types, $namespaces ) )
{
return false;
}
break;
}
}
return true;
}
/**
* An XML object A is X-Equal to an XML object B if the [XPath 1.0] expression A = B returns the value true
* (see http://www.w3.org/TR/xpath.html#booleans). In the case of element and attribute values, those whose
* type are xs:decimal, xs:float, or xs:double, or derived from one of these types MUST be treated as numbers
* for the purposes of interpretation of http://www.w3.org/TR/xpath.html#booleans. If a value has type xs:boolean
* (or a type derived from xs:boolean), then it MUST be converted to an [XPath 1.0] Boolean with '1' and 'true' being
* converted to true and '0' and 'false' being converted to false. Values with any other XML Schema type are treated as
* [XPath 1.0] strings.
*
* @param string $value1 A value to compare
* @param string $value2 A value to compare
* @param string $type1 The type of $value1 (default: XBRL_Equality::$EQUALITY_TYPE_STRING)
* @param string $type2 The type of $value2 (default: XBRL_Equality::$EQUALITY_TYPE_STRING)
* // BMS 2018-02-18 Added because the nodes should be equal in type so type information is going to be needed
* @param XBRL_Types $types
* @return bool
*/
public static function xequal( $value1, $value2, $type1 = 0 /* XBRL_Equality::$EQUALITY_TYPE_STRING */, $type2 = 0 /* XBRL_Equality::EQUALITY_TYPE_STRING */, $types = null )
{
// If both are empty then they are the same (not necessarily right but the same)
if ( empty( $value1 ) && empty( $value2 ) ) return true;
$isPositiveInf1 = false;
$isNegativeInf1 = false;
$isPositiveInf2 = false;
$isNegativeInf2 = false;
// Clean the valuess
switch ( $type1 )
{
case XBRL_Equality::$EQUALITY_TYPE_BOOLEAN:
if ( XBRL_Equality::isBooleanValue( $value1 ) )
{
$value1 = filter_var( $value1, FILTER_VALIDATE_BOOLEAN ) ? "true()" : "false()";
}
else if ( is_string( $value1 ) )
{
$value1 = empty( $value1 ) ? "false()" : "true()";
}
else
{
return false;
}
break;
case XBRL_Equality::$EQUALITY_TYPE_NUMBER:
if ( trim( $value1, '-+' ) == 'INF' )
{
$isPositiveInf1 = $value1[0] != '-';
$isNegativeInf1 = $value1[0] == '-';
}
else
{
if ( ! is_numeric( $value1 ) ) return false;
$value1 = trim( $value1, '+' );
}
break;
default: // string
if ( ! is_array( $value1 ) )
{
// XPath 1.0 has no character escaping (XPath 2.0 does) so
// splitting on apostrophe and using the concat() function
$parts = explode( "'", $value1 );
$value1 = count( $parts ) > 1
? "concat('" . join( "', \"'\", '", $parts ) . "')"
: "'$value1'";
}
break;
}
switch ( $type2 )
{
case XBRL_Equality::$EQUALITY_TYPE_BOOLEAN:
if ( XBRL_Equality::isBooleanValue( $value2 ) )
{
$value2 = filter_var( $value2, FILTER_VALIDATE_BOOLEAN ) ? "true()" : "false()";
}
else if ( is_string( $value2 ) )
{
$value2 = empty( $value2 ) ? "false()" : "true()";
}
else
{
return false;
}
break;
case XBRL_Equality::$EQUALITY_TYPE_NUMBER:
if ( trim( $value1, '-+' ) == 'INF' )
{
$isPositiveInf2 = $value2[0] != '-';
$isNegativeInf2 = $value2[0] == '-';
}
else
{
if ( ! is_numeric( $value2 ) ) return false;
$value2 = trim( $value2, '+' );
}
break;
default: // String
if ( ! is_array( $value2 ) )
{
// XPath 1.0 has not character escaping (XPath 2.0 does) so
// splitting on apostrophe and using the concat() function
$parts = explode( "'", $value2 );
$value2 = count( $parts ) > 1
? "concat('" . join( "', \"'\", '", $parts ) . "')"
: "'$value2'";
}
break;
}
if ( is_array( $value1 ) )
{
return XBRL_Equality::compare_arrays_ordinal($value1, $value2);
}
else
{
if ( $isNegativeInf1 || $isNegativeInf2 || $isPositiveInf1 || $isPositiveInf2 )
{
return ( $isNegativeInf1 & $isNegativeInf2 ) || ( $isPositiveInf1 & $isPositiveInf2 );
}
// Create a singleton XML document instance that can be used to evaluate the XPath equality of two nodes
if ( is_null( XBRL_Equality::$doc ) )
{
$xml = "<a></a>";
XBRL_Equality::$doc = simplexml_load_string( $xml );
}
// Create an XPath query to evaluate the values
$result = XBRL_Equality::$doc->xpath( "/a[$value1 = $value2]" );
$result = count( $result ) > 0;
return $result;
}
}
/**
* Compare arrays ignoring the position of the elements
* @param array $value1
* @param array $value2
* @return bool
*/
public static function compare_arrays( $value1, $value2 )
{
if ( count( $value1 ) != count( $value2 ) ) return false;
$types = XBRL_Types::getInstance();
foreach ( $value1 as $key => $value )
{
if ( ! isset( $value2[ $key ] ) ) return false;
if ( $key === 'id' )
{
$pattern = "/^" . SchemaTypes::$ncName . "$/u";
if ( ! preg_match( $pattern, $value, $matches ) )
{
XBRL_Log::getInstance()->taxonomy_validation( "context", "id attribute is not a valid NCName", array( 'id' => $value ) );
}
if ( ! preg_match( $pattern, $value2[ $key ], $matches ) )
{
XBRL_Log::getInstance()->taxonomy_validation( "context", "id attribute is not a valid NCName", array( 'id' => $value[ $key ] ) );
}
}
else if ( is_numeric( $key ) && is_string( $value ) )
{
if ( ! is_string( $value2[ $key ] ) || $value != $value2[ $key ] )
{
return false;
}
}
else if ( is_array( $value ) )
{
if ( ! is_array( $value2[ $key ] ) ) return false;
// These arrays will represent a collection of elements (the key will be 'children') so compare them in order
if ( ! XBRL_Equality::compare_arrays_ordinal( $value, $value2[ $key ]) )
{
return false;
}
continue;
}
else if ( is_string( $key ) && in_array( $key, array( 'id', 'name', 'prefix', 'type' ) ) )
{
if ( $value != $value2[ $key ] )
{
return false;
}
continue;
}
// These can be attribute comparisons so if not the element value look for an attribute and get the type
$aType = $key === 'value'
? XBRL_Equality::xequalElementType( $types, $value1['name'], isset( $value1['prefix'] ) ? $value1['prefix'] : null )
: XBRL_Equality::xequalAttributeType( $types, $key, isset( $value1['prefix'] ) ? $value1['prefix'] : null );
$bType = $key === 'value'
? XBRL_Equality::xequalElementType( $types, $value2['name'], isset( $value2['prefix'] ) ? $value2['prefix'] : null )
: XBRL_Equality::xequalAttributeType( $types, $key, isset( $value2['prefix'] ) ? $value2['prefix'] : null );
if ( ! XBRL_Equality::xequal( $value, $value2[ $key ], $aType, $bType, $types ) )
{
return false;
}
}
return true;
}
/**
* Get the xequal comparison type an attribute
* @param XBRL_Types $types
* @param string $localName
* @param string|null $prefix
* @return number
*/
public static function xequalAttributeType( $types, $localName, $prefix )
{
$attribute = $types->getAttribute( $localName, $prefix );
if ( ! $attribute || ! isset( $attribute['types'] ) || ! count( $attribute['types'] ) ) return XBRL_Equality::$EQUALITY_TYPE_STRING;
return XBRL_Equality::xEqualComparisonType( array( 'type' => $attribute['types'][0] ), $types);
}
/**
* Get the xequal comparison type an element
* @param XBRL_Types $types
* @param string $localName
* @param string|null $prefix
* @return number
*/
private static function xequalElementType( $types, $localName, $prefix )
{
$element = $types->getElement( $localName, $prefix );
if ( ! $element || ! isset( $element['types'] ) || ! count( $element['types'] ) ) return XBRL_Equality::$EQUALITY_TYPE_STRING;
return XBRL_Equality::xEqualComparisonType( array( 'type' => $element['types'][0] ), $types);
}
/**
* Compare arrays using the position of the elements
* @param array $array1
* @param array $array2
* @return bool
*/
public static function compare_arrays_ordinal( $array1, $array2 )
{
if ( count( $array1 ) != count( $array2 ) ) return false;
if ( ! count( $array1 ) ) return true;
$types = XBRL_Types::getInstance();
$log = XBRL_Log::getInstance();
for( $i = 0; $i < count( $array1 ); $i++ )
{
$key1 = key( $array1 );
$key2 = key( $array2 );
if ( $key1 != $key2 ) return false;
$value1 = current( $array1 );
$value2 = current( $array2 );
next( $array1 );
next( $array2 );
if ( $key1 == 'id' )
{
$pattern = "/^" . SchemaTypes::$ncName . "$/u";
if ( ! preg_match( $pattern, $value1, $matches ) )
{
$log->taxonomy_validation( "context", "id attribute is not a valid NCName", array( 'id' => $value1 ) );
}
if ( ! preg_match( $pattern, $value2, $matches ) )
{
$log->taxonomy_validation( "context", "id attribute is not a valid NCName", array( 'id' => $value2 ) );
}
}
if ( is_array( $value1 ) )
{
if ( ! is_array( $value2 ) ) return false;
// These arrays will represent elements so compare the contents by name
if ( ! XBRL_Equality::compare_arrays( $value1, $value2 ) )
{
return false;
}
}
else if ( in_array( $key1, array( 'id', 'name', 'prefix', 'type' ) ) )
{
if ( $value1 != $value2 )
{
return false;
}
}
else
{
$value1Type = XBRL_Equality::xEqualComparisonType( $value1, $types );
$value2Type = XBRL_Equality::xEqualComparisonType( $value2, $types );
if ( ! XBRL_Equality::xequal( $value1, $value2, $value1Type, $value2Type, $types ) )
{
return false;
}
}
}
return true;
}
/** -------------------------------------------------------------------------------
*
* Private utility functions
*
* ------------------------------------------------------------------------------- */
/**
* Test whether $value is really a boolean. This function differs from the built in
* is_bool() function because it accommodates zero as false and any other number as true.
*
* @param mixed $value The value to be tested
* @return boolean
*/
private static function isBooleanValue( $value )
{
if ( is_bool( $value ) ) return true;
$result = filter_var( $value, FILTER_VALIDATE_BOOLEAN, array( 'flags' => FILTER_NULL_ON_FAILURE ) );
return $result !== null;
}
/**
* Display the results of a comparison
*
* @param bool $result The comparison result
* @param string $function The function executed to compare
* @param array $source A list function parameters
*/
private static function display( $result, $function, $source )
{
echo ($result ? "Match " : "Mismatch");
echo " $function ";
echo is_array( $source )
? implode( ', ', $source )
: " ($source)";
echo "\n";
}
/**
* Test to make sure an element exists in both arrays
*
* @param array $a An array holding content with element names to be matched
* @param array $b An array holding content with element names to be matched
* @param bool $elementName
* @return bool Returns true if both or neither array contains the element name
*/
private static function hasElement( $a, $b, $elementName )
{
return
( isset( $a[ $elementName ] ) && isset( $b[ $elementName ] ) ) ||
( ! isset( $a[ $elementName ] ) && ! isset( $b[ $elementName ] ) );
}
/**
* Check a pair of arrays to ensure they both have the same set of valid elements
*
* @param array $a An array holding content with element names to be matched
* @param array $b An array holding content with element names to be matched
* @param array $validNames A list of names that are valid for these arrays
* @return bool|array Returns false if the arrays contain a different number
* of arrays or an array of the common, valid names
*/
private static function matchedKeyNames( $a, $b, $validNames )
{
if ( ! is_array( $validNames ) || ! count( $validNames ) )
throw new Exception( "XBRL_Equality::matchedKeyNames $validNames MUST be an array and cannot be empty" );
if ( ! is_array( $a ) || ! is_array( $b ) )
throw new Exception( "XBRL_Equality::matchedKeyNames parameters \$a and \$b MUST be arrays" );
// Neither have content so they are the same (that's not necessarily correct be they are the same)
if ( ! count( $a ) && ! count( $b ) ) return array();
// Get a list of the different elements
$aValidKeys = array_intersect( array_keys( $a ), $validNames );
$bValidKeys = array_intersect( array_keys( $b ), $validNames );
// If they have a different number of valid key they can't be the same
if ( count( $aValidKeys ) != count( $bValidKeys ) ) return false;
// If there are no valid keys that may be a problem but for the caller to decide
if ( ! count( $aValidKeys ) ) return $aValidKeys;
// Arrays may have the same number of keys but different ones
$diffa = array_diff( $aValidKeys, $bValidKeys );
$diffb = array_diff( $bValidKeys, $aValidKeys );
// It's good there are no differences so return a valid array (it doesn't matter which because they are the same)
// If there are differences return false
return ! count( $diffa ) && ! count( $diffb )
? $aValidKeys
: false;
}
/**
* Check a pair of arrays to ensure they both have the same set of valid elements
*
* @param array $a An array holding content with element names to be matched
* @param array $b An array holding content with element names to be matched
* @param array $validNames A list of names that are valid for these arrays
* @param bool $emptyContentAllowed
* @return bool|array Returns false if the arrays contain a different number
* of arrays or an array of the common, valid names
*/
private static function matchedElements( $a, $b, $validNames, $emptyContentAllowed = false )
{
if ( is_null( $a ) && is_null( $b ) ) return true;
if ( is_null( $a ) || is_null( $b ) ) return false;
if ( ! is_array( $validNames ) || ! count( $validNames ) )
{
throw new Exception( "The list of valid element names is not valid" );
}
// Check the arrays have the same elements
$result = XBRL_Equality::matchedKeyNames( $a, $b, $validNames );
if ( $result === false || ! is_array( $result ) ) return false;
if ( ! $emptyContentAllowed && ! count( $result ) ) return false;
$types = XBRL_Types::getInstance();
foreach ( $validNames as $elementName )
{
// BMS 2018-02-18 Need to look at this because the types are necessary for the test to make sense.
if ( $elementName == 'name' )
{
if ( $a[ $elementName ] != $b[ $elementName ] )
{
return false;
}
continue;
}
if ( ! isset( $a[ $elementName ] ) || ! isset( $b[ $elementName ] ) )
{
continue;
}
$aType = XBRL_Equality::xEqualComparisonType( $a, $types );
$bType = XBRL_Equality::xEqualComparisonType( $b, $types );
if ( ! XBRL_Equality::xequal( $a[ $elementName ], $b[ $elementName ], $aType, $bType, $types ) )
{
return false;
}
}