PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.6.2
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.6.2
4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.5 3.6.6 3.7.0 3.7.1 3.8.0 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.2.0 4.2.1 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.3.10 4.3.11 4.3.12 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.7.1 4.3.8 4.3.9 4.3.9.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.7.0
cookiebot / src / lib / traits / Class_Constant_Override_Validator_Trait.php
cookiebot / src / lib / traits Last commit date
Class_Constant_Override_Validator_Trait.php 1 year ago Extra_Information_Trait.php 1 year ago
Class_Constant_Override_Validator_Trait.php
154 lines
1 <?php
2
3 namespace cybot\cookiebot\lib\traits;
4
5 use cybot\cookiebot\exceptions\InvalidClassConstantException;
6 use Exception;
7 use InvalidArgumentException;
8
9 trait Class_Constant_Override_Validator_Trait {
10
11
12 /**
13 * @param array $fixed_class_constant_names
14 *
15 * @throws Exception
16 */
17 protected function validate_fixed_class_constants( array $fixed_class_constant_names ) {
18 foreach ( $fixed_class_constant_names as $fixed_class_constant_name ) {
19 $this->validate_fixed_class_constant( $fixed_class_constant_name );
20 }
21 }
22
23 /**
24 * @param $fixed_class_constant_name
25 *
26 * @throws Exception
27 */
28 protected function validate_fixed_class_constant( $fixed_class_constant_name ) {
29 $value_self = constant( 'self::' . $fixed_class_constant_name );
30 $value_static = constant( 'static::' . $fixed_class_constant_name );
31
32 if ( $value_self !== $value_static ) {
33 throw new InvalidClassConstantException(
34 sprintf(
35 'Class constant "%s" should be changed by %s',
36 $fixed_class_constant_name,
37 static::class
38 )
39 );
40 }
41 }
42
43 /**
44 * @param array $required_string_constant_names
45 *
46 * @throws Exception
47 */
48 protected function validate_required_string_class_constants( array $required_string_constant_names ) {
49 foreach ( $required_string_constant_names as $required_string_constant_name ) {
50 $this->validate_required_string_class_constant( $required_string_constant_name );
51 }
52 }
53
54 /**
55 * @param string $required_string_constant_name
56 *
57 * @throws Exception
58 */
59 protected function validate_required_string_class_constant( $required_string_constant_name ) {
60 if ( ! is_string( $required_string_constant_name ) ) {
61 throw new InvalidArgumentException();
62 }
63 $value = constant( 'static::' . $required_string_constant_name );
64 if ( empty( $value ) || ! is_string( $value ) ) {
65 throw new InvalidClassConstantException(
66 sprintf(
67 'Class constant "%s" must be a non-empty string in %s',
68 $required_string_constant_name,
69 static::class
70 )
71 );
72 }
73 }
74
75 /**
76 * @param array $required_boolean_constant_names
77 *
78 * @throws Exception
79 */
80 protected function validate_required_boolean_class_constants( array $required_boolean_constant_names ) {
81 foreach ( $required_boolean_constant_names as $required_boolean_constant_name ) {
82 $this->validate_required_boolean_class_constant( $required_boolean_constant_name );
83 }
84 }
85
86 /**
87 * @param string $required_boolean_constant_name
88 *
89 * @throws Exception
90 */
91 protected function validate_required_boolean_class_constant( $required_boolean_constant_name ) {
92 if ( ! is_string( $required_boolean_constant_name ) ) {
93 throw new InvalidArgumentException();
94 }
95 $value = constant( 'static::' . $required_boolean_constant_name );
96 if ( ! is_bool( $value ) ) {
97 throw new InvalidClassConstantException(
98 sprintf(
99 'Class constant "%s" must be a boolean in %s',
100 $required_boolean_constant_name,
101 static::class
102 )
103 );
104 }
105 }
106
107 /**
108 * @param array $required_array_constant_names
109 *
110 * @throws Exception
111 */
112 protected function validate_required_array_class_constants( array $required_array_constant_names ) {
113 foreach ( $required_array_constant_names as $required_array_constant_name ) {
114 $this->validate_required_array_class_constant( $required_array_constant_name );
115 }
116 }
117
118 /**
119 * @param $required_array_constant_name
120 * @param array|null $allowed_item_values
121 *
122 * @throws Exception
123 */
124 protected function validate_required_array_class_constant( $required_array_constant_name, array $allowed_item_values = null ) {
125 if ( ! is_string( $required_array_constant_name ) ) {
126 throw new InvalidArgumentException();
127 }
128 $value = constant( 'static::' . $required_array_constant_name );
129 if ( empty( $value ) || ! is_array( $value ) ) {
130 throw new InvalidClassConstantException(
131 sprintf(
132 'Class constant "%s" must be an array in %s',
133 $required_array_constant_name,
134 static::class
135 )
136 );
137 }
138 if ( ! empty( $allowed_item_values ) ) {
139 foreach ( $value as $item ) {
140 if ( ! in_array( $item, $allowed_item_values, true ) ) {
141 throw new InvalidClassConstantException(
142 sprintf(
143 'Class constant "%s" array items should be one of "%s" in %s',
144 $required_array_constant_name,
145 implode( ', ', $allowed_item_values ),
146 static::class
147 )
148 );
149 }
150 }
151 }
152 }
153 }
154