@@ -50,6 +50,7 @@ public function convert($source): array
50
50
private function convertTypes (\DOMDocument $ source ): array
51
51
{
52
52
$ typesData = [];
53
+ $ parentChildData = [];
53
54
/** @var \DOMNodeList $contentTypes */
54
55
$ contentTypes = $ source ->getElementsByTagName ('type ' );
55
56
/** @var \DOMNode $contentType */
@@ -67,12 +68,12 @@ private function convertTypes(\DOMDocument $source): array
67
68
} elseif ('additional_data ' === $ childNode ->nodeName ) {
68
69
$ typesData [$ name ][$ childNode ->nodeName ] = $ this ->convertAdditionalData ($ childNode );
69
70
} elseif ('parents ' === $ childNode ->nodeName ) {
70
- $ typesData [$ name ][$ childNode ->nodeName ] = [
71
+ $ parentChildData [$ name ][$ childNode ->nodeName ] = [
71
72
'defaultPolicy ' => $ this ->getAttributeValue ($ childNode , 'default_policy ' ),
72
73
'types ' => $ this ->convertParentChildData ($ childNode , 'parent ' )
73
74
];
74
75
} elseif ('children ' === $ childNode ->nodeName ) {
75
- $ typesData [$ name ][$ childNode ->nodeName ] = [
76
+ $ parentChildData [$ name ][$ childNode ->nodeName ] = [
76
77
'defaultPolicy ' => $ this ->getAttributeValue ($ childNode , 'default_policy ' ),
77
78
'types ' => $ this ->convertParentChildData ($ childNode , 'child ' )
78
79
];
@@ -87,9 +88,9 @@ private function convertTypes(\DOMDocument $source): array
87
88
return (int )$ firstElement ['sortOrder ' ] <=> (int )$ secondElement ['sortOrder ' ];
88
89
});
89
90
90
- $ this ->convertParentChildDataToAllowedParents ($ typesData );
91
+ $ allowedParents = $ this ->convertParentChildDataToAllowedParents (array_keys ( $ typesData), $ parentChildData );
91
92
92
- return $ typesData ;
93
+ return array_merge_recursive ( $ typesData, $ allowedParents ) ;
93
94
}
94
95
95
96
/**
@@ -448,42 +449,43 @@ private function convertParentChildData(\DOMElement $elementNode, string $tagNam
448
449
{
449
450
$ data = [];
450
451
foreach ($ elementNode ->getElementsByTagName ($ tagName ) as $ node ) {
451
- $ data [] = [
452
- 'name ' => $ node ->attributes ->getNamedItem ('name ' )->nodeValue ,
453
- 'policy ' => $ node ->attributes ->getNamedItem ('policy ' )->nodeValue
454
- ];
452
+ $ name = $ node ->attributes ->getNamedItem ('name ' )->nodeValue ;
453
+ $ data [$ node ->attributes ->getNamedItem ('policy ' )->nodeValue ][] = $ name ;
455
454
}
456
455
return $ data ;
457
456
}
458
457
459
458
/**
460
459
* Convert parent and child data to allowed parents
461
460
*
462
- * @param array $typesData
461
+ * @param array $types
462
+ * @param array $parentChildData
463
+ * @return array
463
464
*/
464
- private function convertParentChildDataToAllowedParents (array & $ typesData )
465
+ private function convertParentChildDataToAllowedParents (array $ types , array $ parentChildData ): array
465
466
{
466
- // get an array of all content type names
467
- $ types = array_keys ($ typesData );
467
+ $ allowedParentsData = [];
468
468
469
469
// convert children
470
- $ allowedParents = $ this ->convertChildrenToAllowedParents ($ typesData , $ types );
471
- foreach ($ typesData as $ key => & $ typeData ) {
472
- $ typeData [ 'allowed_parents ' ] = $ allowedParents [$ key ];
470
+ $ allowedParents = $ this ->convertChildrenToAllowedParents ($ parentChildData , $ types );
471
+ foreach ($ types as $ type ) {
472
+ $ allowedParentsData [ $ type ][ 'allowed_parents ' ] = $ allowedParents [$ type ];
473
473
}
474
474
475
475
// convert parents
476
- $ typesData = $ this ->convertParentsToAllowedParents ($ typesData , $ types );
476
+ $ allowedParentsData = $ this ->convertParentsToAllowedParents ($ parentChildData , $ types , $ allowedParentsData );
477
+
478
+ return $ allowedParentsData ;
477
479
}
478
480
479
481
/**
480
482
* Convert children data to allow parents
481
483
*
482
- * @param array $typesData
483
- * @param array $allowedParents
484
+ * @param array $parentChildData
485
+ * @param array $types
484
486
* @return array
485
487
*/
486
- private function convertChildrenToAllowedParents (array $ typesData , array $ types ): array
488
+ private function convertChildrenToAllowedParents (array $ parentChildData , array $ types ): array
487
489
{
488
490
$ allowedParents = [];
489
491
@@ -492,30 +494,20 @@ private function convertChildrenToAllowedParents(array $typesData, array $types)
492
494
$ allowedParents [$ type ] = [];
493
495
}
494
496
495
- foreach ($ typesData as $ key => $ value ) {
497
+ foreach ($ parentChildData as $ key => $ value ) {
496
498
$ children = $ value ['children ' ] ?? [];
497
499
498
500
if (empty ($ children )) {
499
501
continue ;
500
502
}
501
503
502
- if ($ children ['defaultPolicy ' ] === 'deny ' ) {
503
- $ allow = $ this ->getContentTypesByPolicy ($ children ['types ' ], 'allow ' );
504
- foreach ($ allowedParents as $ type => $ parents ) {
505
- if (!in_array ($ type , $ allow )) {
506
- $ allowedParents [$ type ] = $ this ->removeDataInArray ($ key , $ parents );
507
- } else {
508
- $ allowedParents [$ type ][] = $ key ;
509
- }
510
- }
511
- } else {
512
- $ deny = $ this ->getContentTypesByPolicy ($ children ['types ' ], 'deny ' );
513
- foreach ($ allowedParents as $ type => $ parents ) {
514
- if (in_array ($ type , $ deny )) {
515
- $ allowedParents [$ type ] = $ this ->removeDataInArray ($ key , $ parents );
516
- } else {
517
- $ allowedParents [$ type ][] = $ key ;
518
- }
504
+ foreach ($ allowedParents as $ type => $ parents ) {
505
+ $ typeAllowed = in_array ($ type , $ children ['types ' ]['allow ' ] ?? []);
506
+ $ typeDenied = in_array ($ type , $ children ['types ' ]['deny ' ] ?? []);
507
+ if (($ children ['defaultPolicy ' ] === 'deny ' && !$ typeAllowed ) || $ typeDenied ) {
508
+ $ allowedParents [$ type ] = $ this ->removeDataInArray ($ key , $ parents );
509
+ } else {
510
+ $ allowedParents [$ type ][] = $ key ;
519
511
}
520
512
}
521
513
}
@@ -526,56 +518,35 @@ private function convertChildrenToAllowedParents(array $typesData, array $types)
526
518
/**
527
519
* Convert parents data to allowed parents
528
520
*
529
- * @param array $typesData
521
+ * @param array $parentChildData
530
522
* @param array $types
523
+ * @param array $allowedParentsData
531
524
* @return array
532
525
*/
533
- private function convertParentsToAllowedParents (array $ typesData , array $ types ): array
526
+ private function convertParentsToAllowedParents (array $ parentChildData , array $ types, array $ allowedParentsData ): array
534
527
{
535
- foreach ($ typesData as $ key => $ value ) {
528
+ foreach ($ parentChildData as $ key => $ value ) {
536
529
$ parent = $ value ['parents ' ] ?? [];
537
530
538
531
if (empty ($ parent )) {
539
532
continue ;
540
533
}
541
534
535
+ $ allowedTypes = $ parent ['types ' ]['allow ' ] ?? [];
536
+ $ deniedTypes = $ parent ['types ' ]['deny ' ] ?? [];
537
+
542
538
if ($ parent ['defaultPolicy ' ] === 'deny ' ) {
543
- $ allowedParents = $ this -> getContentTypesByPolicy ( $ parent [ ' types ' ], ' allow ' ) ;
539
+ $ allowedParents = $ allowedTypes ;
544
540
} else {
545
- $ allowedParents = $ types ;
546
- foreach ($ parent ['types ' ] as $ type ) {
547
- if ($ type ['policy ' ] === 'deny ' ) {
548
- $ allowedParents = $ this ->removeDataInArray ($ type ['name ' ], $ allowedParents );
549
- } else {
550
- $ allowedParents = array_merge (
551
- $ allowedParents ,
552
- $ this ->getContentTypesByPolicy ($ parent ['types ' ], 'allow ' )
553
- );
554
- }
541
+ $ allowedParents = array_merge ($ types , $ allowedTypes );
542
+ foreach ($ deniedTypes as $ type ) {
543
+ $ allowedParents = $ this ->removeDataInArray ($ type , $ allowedParents );
555
544
}
556
545
}
557
- $ typesData [$ key ]['allowed_parents ' ] = $ allowedParents ;
546
+ $ allowedParentsData [$ key ]['allowed_parents ' ] = $ allowedParents ;
558
547
}
559
548
560
- return $ typesData ;
561
- }
562
-
563
- /**
564
- * Get an array of content type names by policy (allow or deny)
565
- *
566
- * @param array $contentTypes
567
- * @param string $policy
568
- * @return array
569
- */
570
- private function getContentTypesByPolicy (array $ contentTypes , string $ policy ): array
571
- {
572
- $ data = [];
573
- foreach ($ contentTypes as $ type ) {
574
- if ($ type ['policy ' ] === $ policy ) {
575
- $ data [] = $ type ['name ' ];
576
- }
577
- }
578
- return $ data ;
549
+ return $ allowedParentsData ;
579
550
}
580
551
581
552
/**
0 commit comments