1 | <?php namespace Stevenmaguire\Http\Middleware; |
||
3 | trait CspValidationTrait |
||
4 | { |
||
5 | /** |
||
6 | * Validates a given profiles configuration. |
||
7 | * |
||
8 | * @param array $profiles |
||
9 | * |
||
10 | * @return boolean |
||
11 | * @throws CspValidationException |
||
12 | */ |
||
13 | 12 | public static function validateProfiles(array $profiles) |
|
14 | { |
||
15 | 12 | $messages = []; |
|
16 | |||
17 | 12 | if (isset($profiles['default'])) { |
|
18 | 5 | static::validateProfilesDefault($profiles['default'], $messages); |
|
19 | 5 | } |
|
20 | |||
21 | 12 | if (isset($profiles['profiles'])) { |
|
22 | 6 | static::validateProfilesConfiguration($profiles['profiles'], $messages); |
|
23 | 6 | } |
|
24 | |||
25 | 12 | if (empty($messages)) { |
|
26 | 6 | return true; |
|
27 | } |
||
28 | |||
29 | 6 | throw new Exceptions\CspValidationException($messages); |
|
30 | } |
||
31 | |||
32 | /** |
||
33 | * Validates a given default configuration. |
||
34 | * |
||
35 | * @param mixed $default |
||
36 | * @param array $messages |
||
37 | * |
||
38 | * @return void |
||
39 | */ |
||
40 | 5 | public static function validateProfilesDefault($default, array &$messages) |
|
41 | { |
||
42 | 5 | if (static::isNotArrayOrString($default)) { |
|
43 | 2 | $messages[] = 'Default profile configuration must be a string or array.'; |
|
44 | 2 | } |
|
45 | |||
46 | 5 | if (is_array($default) && count(array_filter($default, [__CLASS__, 'isNotString']))) { |
|
47 | 1 | $messages[] = 'Default profile configuration must contain only strings when defined as array.'; |
|
48 | 1 | } |
|
49 | 5 | } |
|
50 | |||
51 | /** |
||
52 | * Validates a given profiles configuration. |
||
53 | * |
||
54 | * @param mixed $profiles |
||
55 | * @param array $messages |
||
56 | * |
||
57 | * @return void |
||
58 | */ |
||
59 | 6 | public static function validateProfilesConfiguration($profiles, array &$messages) |
|
60 | { |
||
61 | 6 | if (static::isNotArray($profiles)) { |
|
62 | 1 | $messages[] = 'Profile configuration must be an array.'; |
|
63 | 1 | } else { |
|
64 | array_walk($profiles, function ($config, $profile) use (&$messages) { |
||
65 | 4 | if (static::isNotArray($config)) { |
|
66 | 1 | $messages[] = 'Profile configuration for "'.$profile.'" must be an array.'; |
|
67 | 1 | } else { |
|
68 | array_walk($config, function ($domains, $directive) use ($profile, &$messages) { |
||
69 | 2 | if (static::isNotArrayOrString($domains)) { |
|
70 | 1 | $messages[] = 'Directive configuration for "'.$profile.':'.$directive.'"'. |
|
71 | 1 | ' must be a string or an array.'; |
|
72 | 1 | } |
|
73 | 3 | }); |
|
74 | } |
||
75 | 5 | }); |
|
76 | } |
||
77 | 6 | } |
|
78 | |||
79 | /** |
||
80 | * Determines if given subject is not an array. |
||
81 | * |
||
82 | * @param mixed |
||
83 | * |
||
84 | * @return boolean |
||
85 | */ |
||
86 | 10 | protected static function isNotArray($subject) |
|
90 | |||
91 | /** |
||
92 | * Determines if given subject is not an array and not a string. |
||
93 | * |
||
94 | * @param mixed |
||
95 | * |
||
96 | * @return boolean |
||
97 | */ |
||
98 | 7 | protected static function isNotArrayOrString($subject) |
|
102 | |||
103 | /** |
||
104 | * Determines if given subject is not a string. |
||
105 | * |
||
106 | * @param mixed |
||
107 | * |
||
108 | * @return boolean |
||
109 | */ |
||
110 | 7 | protected static function isNotString($subject) |
|
114 | } |
||
115 |