| 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 |
esc_html( $fixed_class_constant_name ), |
| 37 |
esc_html( 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 |
esc_html( $required_string_constant_name ), |
| 69 |
esc_html( 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 |
esc_html( $required_boolean_constant_name ), |
| 101 |
esc_html( 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 |
esc_html( $required_array_constant_name ), |
| 134 |
esc_html( 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 |
esc_html( $required_array_constant_name ), |
| 145 |
esc_html( implode( ', ', $allowed_item_values ) ), |
| 146 |
esc_html( static::class ) |
| 147 |
) |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|