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 |