File size: 1,760 Bytes
d2897cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

namespace Mautic\ConfigBundle\Mapper\Helper;

class RestrictionHelper
{
    /**
     * Ensure that the array has string indexes for congruency with a nested array similar to ['db_host', 'monitored_email' => ['EmailBundle_bounces'];.
     */
    public static function prepareRestrictions(array $restrictedParameters): array
    {
        $prepared = [];
        foreach ($restrictedParameters as $key => $value) {
            $newKey            = (is_numeric($key)) ? $value : $key;
            $prepared[$newKey] = (is_array($value)) ? self::prepareRestrictions($value) : $value;
        }

        return $prepared;
    }

    /**
     * Remove fields that are restricted.
     */
    public static function applyRestrictions(array $configParameters, array $restrictedParameters, $restrictedParentKey = null): array
    {
        if ($restrictedParentKey) {
            if (!isset($restrictedParameters[$restrictedParentKey])) {
                // No restrictions
                return $configParameters;
            }

            $restrictedParameters = $restrictedParameters[$restrictedParentKey];
        }

        foreach ($configParameters as $key => $value) {
            // The entire form type is restricted
            if (isset($restrictedParameters[$key]) && !is_array($restrictedParameters[$key])) {
                unset($configParameters[$key]);

                continue;
            }

            // A sub type of the form type is restricted
            if (is_array($value)) {
                $configParameters[$key] = self::applyRestrictions($value, $restrictedParameters, $key);

                continue;
            }

            // Otherwise no restrictions are in place
        }

        return $configParameters;
    }
}