Class_Constant_Override_Validator_Trait.php
120 lines
| 1 | <?php |
| 2 | |
| 3 | namespace cybot\cookiebot\lib\traits; |
| 4 | |
| 5 | use Exception; |
| 6 | use InvalidArgumentException; |
| 7 | |
| 8 | trait Class_Constant_Override_Validator_Trait { |
| 9 | |
| 10 | /** |
| 11 | * @param array $fixed_class_constant_names |
| 12 | * |
| 13 | * @throws Exception |
| 14 | */ |
| 15 | protected function validate_fixed_class_constants( array $fixed_class_constant_names ) { |
| 16 | foreach ( $fixed_class_constant_names as $fixed_class_constant_name ) { |
| 17 | $this->validate_fixed_class_constant( $fixed_class_constant_name ); |
| 18 | } |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * @param $fixed_class_constant_name |
| 23 | * |
| 24 | * @throws Exception |
| 25 | */ |
| 26 | protected function validate_fixed_class_constant( $fixed_class_constant_name ) { |
| 27 | $value_self = constant( 'self::' . $fixed_class_constant_name ); |
| 28 | $value_static = constant( 'static::' . $fixed_class_constant_name ); |
| 29 | if ( $value_self !== $value_static ) { |
| 30 | throw new Exception( 'Class constant "' . $fixed_class_constant_name . '" should be changed by ' . static::class ); |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @param array $required_string_constant_names |
| 36 | * |
| 37 | * @throws Exception |
| 38 | */ |
| 39 | protected function validate_required_string_class_constants( array $required_string_constant_names ) { |
| 40 | foreach ( $required_string_constant_names as $required_string_constant_name ) { |
| 41 | $this->validate_required_string_class_constant( $required_string_constant_name ); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @param string $required_string_constant_name |
| 47 | * |
| 48 | * @throws Exception |
| 49 | */ |
| 50 | protected function validate_required_string_class_constant( $required_string_constant_name ) { |
| 51 | if ( ! is_string( $required_string_constant_name ) ) { |
| 52 | throw new InvalidArgumentException(); |
| 53 | } |
| 54 | $value = constant( 'static::' . $required_string_constant_name ); |
| 55 | if ( empty( $value ) || ! is_string( $value ) ) { |
| 56 | throw new Exception( 'Class constant "' . $required_string_constant_name . '" must be a non-empty string in ' . static::class ); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @param array $required_boolean_constant_names |
| 62 | * |
| 63 | * @throws Exception |
| 64 | */ |
| 65 | protected function validate_required_boolean_class_constants( array $required_boolean_constant_names ) { |
| 66 | foreach ( $required_boolean_constant_names as $required_boolean_constant_name ) { |
| 67 | $this->validate_required_boolean_class_constant( $required_boolean_constant_name ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param string $required_boolean_constant_name |
| 73 | * |
| 74 | * @throws Exception |
| 75 | */ |
| 76 | protected function validate_required_boolean_class_constant( $required_boolean_constant_name ) { |
| 77 | if ( ! is_string( $required_boolean_constant_name ) ) { |
| 78 | throw new InvalidArgumentException(); |
| 79 | } |
| 80 | $value = constant( 'static::' . $required_boolean_constant_name ); |
| 81 | if ( ! is_bool( $value ) ) { |
| 82 | throw new Exception( 'Class constant "' . $required_boolean_constant_name . '" must be a boolean in ' . static::class ); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param array $required_array_constant_names |
| 88 | * |
| 89 | * @throws Exception |
| 90 | */ |
| 91 | protected function validate_required_array_class_constants( array $required_array_constant_names ) { |
| 92 | foreach ( $required_array_constant_names as $required_array_constant_name ) { |
| 93 | $this->validate_required_array_class_constant( $required_array_constant_name ); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * @param $required_array_constant_name |
| 99 | * @param array|null $allowed_item_values |
| 100 | * |
| 101 | * @throws Exception |
| 102 | */ |
| 103 | protected function validate_required_array_class_constant( $required_array_constant_name, array $allowed_item_values = null ) { |
| 104 | if ( ! is_string( $required_array_constant_name ) ) { |
| 105 | throw new InvalidArgumentException(); |
| 106 | } |
| 107 | $value = constant( 'static::' . $required_array_constant_name ); |
| 108 | if ( empty( $value ) || ! is_array( $value ) ) { |
| 109 | throw new Exception( 'Class constant "' . $required_array_constant_name . '" must be an array in ' . static::class ); |
| 110 | } |
| 111 | if ( ! empty( $allowed_item_values ) ) { |
| 112 | foreach ( $value as $item ) { |
| 113 | if ( ! in_array( $item, $allowed_item_values, true ) ) { |
| 114 | throw new Exception( 'Class constant "' . $required_array_constant_name . '" array items should be one of "' . implode( ', ', $allowed_item_values ) . '" in ' . static::class ); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 |