PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.3.2
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.3.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 3 years ago Extra_Information_Trait.php 4 years ago
Class_Constant_Override_Validator_Trait.php
153 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 * @param array $fixed_class_constant_names
13 *
14 * @throws Exception
15 */
16 protected function validate_fixed_class_constants( array $fixed_class_constant_names ) {
17 foreach ( $fixed_class_constant_names as $fixed_class_constant_name ) {
18 $this->validate_fixed_class_constant( $fixed_class_constant_name );
19 }
20 }
21
22 /**
23 * @param $fixed_class_constant_name
24 *
25 * @throws Exception
26 */
27 protected function validate_fixed_class_constant( $fixed_class_constant_name ) {
28 $value_self = constant( 'self::' . $fixed_class_constant_name );
29 $value_static = constant( 'static::' . $fixed_class_constant_name );
30
31 if ( $value_self !== $value_static ) {
32 throw new InvalidClassConstantException(
33 sprintf(
34 'Class constant "%s" should be changed by %s',
35 $fixed_class_constant_name,
36 static::class
37 )
38 );
39 }
40 }
41
42 /**
43 * @param array $required_string_constant_names
44 *
45 * @throws Exception
46 */
47 protected function validate_required_string_class_constants( array $required_string_constant_names ) {
48 foreach ( $required_string_constant_names as $required_string_constant_name ) {
49 $this->validate_required_string_class_constant( $required_string_constant_name );
50 }
51 }
52
53 /**
54 * @param string $required_string_constant_name
55 *
56 * @throws Exception
57 */
58 protected function validate_required_string_class_constant( $required_string_constant_name ) {
59 if ( ! is_string( $required_string_constant_name ) ) {
60 throw new InvalidArgumentException();
61 }
62 $value = constant( 'static::' . $required_string_constant_name );
63 if ( empty( $value ) || ! is_string( $value ) ) {
64 throw new InvalidClassConstantException(
65 sprintf(
66 'Class constant "%s" must be a non-empty string in %s',
67 $required_string_constant_name,
68 static::class
69 )
70 );
71 }
72 }
73
74 /**
75 * @param array $required_boolean_constant_names
76 *
77 * @throws Exception
78 */
79 protected function validate_required_boolean_class_constants( array $required_boolean_constant_names ) {
80 foreach ( $required_boolean_constant_names as $required_boolean_constant_name ) {
81 $this->validate_required_boolean_class_constant( $required_boolean_constant_name );
82 }
83 }
84
85 /**
86 * @param string $required_boolean_constant_name
87 *
88 * @throws Exception
89 */
90 protected function validate_required_boolean_class_constant( $required_boolean_constant_name ) {
91 if ( ! is_string( $required_boolean_constant_name ) ) {
92 throw new InvalidArgumentException();
93 }
94 $value = constant( 'static::' . $required_boolean_constant_name );
95 if ( ! is_bool( $value ) ) {
96 throw new InvalidClassConstantException(
97 sprintf(
98 'Class constant "%s" must be a boolean in %s',
99 $required_boolean_constant_name,
100 static::class
101 )
102 );
103 }
104 }
105
106 /**
107 * @param array $required_array_constant_names
108 *
109 * @throws Exception
110 */
111 protected function validate_required_array_class_constants( array $required_array_constant_names ) {
112 foreach ( $required_array_constant_names as $required_array_constant_name ) {
113 $this->validate_required_array_class_constant( $required_array_constant_name );
114 }
115 }
116
117 /**
118 * @param $required_array_constant_name
119 * @param array|null $allowed_item_values
120 *
121 * @throws Exception
122 */
123 protected function validate_required_array_class_constant( $required_array_constant_name, array $allowed_item_values = null ) {
124 if ( ! is_string( $required_array_constant_name ) ) {
125 throw new InvalidArgumentException();
126 }
127 $value = constant( 'static::' . $required_array_constant_name );
128 if ( empty( $value ) || ! is_array( $value ) ) {
129 throw new InvalidClassConstantException(
130 sprintf(
131 'Class constant "%s" must be an array in %s',
132 $required_array_constant_name,
133 static::class
134 )
135 );
136 }
137 if ( ! empty( $allowed_item_values ) ) {
138 foreach ( $value as $item ) {
139 if ( ! in_array( $item, $allowed_item_values, true ) ) {
140 throw new InvalidClassConstantException(
141 sprintf(
142 'Class constant "%s" array items should be one of "%s" in %s',
143 $required_array_constant_name,
144 implode( ', ', $allowed_item_values ),
145 static::class
146 )
147 );
148 }
149 }
150 }
151 }
152 }
153