function HTMLRestrictions::mergeAllowedElementsLevel

Same name in other branches
  1. 9 core/modules/ckeditor5/src/HTMLRestrictions.php \Drupal\ckeditor5\HTMLRestrictions::mergeAllowedElementsLevel()
  2. 10 core/modules/ckeditor5/src/HTMLRestrictions.php \Drupal\ckeditor5\HTMLRestrictions::mergeAllowedElementsLevel()

Merge arrays of allowed elements according to HTMLRestrictions rules.

Parameters

array $array1: The first array of allowed elements.

array $array2: The second array of allowed elements.

Return value

array Merged array of allowed elements.

1 call to HTMLRestrictions::mergeAllowedElementsLevel()
HTMLRestrictions::merge in core/modules/ckeditor5/src/HTMLRestrictions.php
Computes set union of two HTML restrictions, with wildcard support.

File

core/modules/ckeditor5/src/HTMLRestrictions.php, line 887

Class

HTMLRestrictions
Represents a set of HTML restrictions.

Namespace

Drupal\ckeditor5

Code

private static function mergeAllowedElementsLevel(array $array1, array $array2) : array {
    $union = [];
    $array1_keys = array_keys($array1);
    $array2_keys = array_keys($array2);
    $common_keys = array_intersect($array1_keys, $array2_keys);
    if (count($common_keys) === 0) {
        // There are no keys in common, simply append the arrays.
        $union = $array1 + $array2;
    }
    else {
        // For all the distinct keys, append them to the result.
        $filter_keys = array_flip($common_keys);
        // Add all unique keys from $array1.
        $union += array_diff_key($array1, $filter_keys);
        // Add all unique keys from $array2.
        $union += array_diff_key($array2, $filter_keys);
        // There are some keys in common that need to be merged.
        foreach ($common_keys as $key) {
            $value1 = $array1[$key];
            $value2 = $array2[$key];
            $value1_is_bool = is_bool($value1);
            $value2_is_bool = is_bool($value2);
            // When both values are boolean, combine the two.
            if ($value1_is_bool && $value2_is_bool) {
                $union[$key] = $value1 || $value2;
            }
            elseif ($value1_is_bool) {
                $union[$key] = $value1 ?: $value2;
            }
            elseif ($value2_is_bool) {
                $union[$key] = $value2 ?: $value1;
            }
            elseif (is_array($value1) && is_array($value2)) {
                $union[$key] = self::mergeAllowedElementsLevel($value1, $value2);
            }
        }
    }
    // Make sure the order of the union array matches the order of the keys in
    // the arrays provided.
    $ordered = [];
    foreach (array_merge($array1_keys, $array2_keys) as $key) {
        $ordered[$key] = $union[$key];
    }
    return $ordered;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.