Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| DatatypeValidationTrait | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
1190 | |
0.00% |
0 / 1 |
| validateFieldDatatype | |
0.00% |
0 / 48 |
|
0.00% |
0 / 1 |
1190 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace AutoModerator\Config\Validation; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | |
| 7 | trait DatatypeValidationTrait { |
| 8 | /** |
| 9 | * Validate field's datatype |
| 10 | * |
| 11 | * @param string $expectedType Unsupported datatype will throw |
| 12 | * @param mixed $value |
| 13 | * @return bool |
| 14 | * @throws InvalidArgumentException in case of unsupported datatype passed via $expectedType |
| 15 | */ |
| 16 | private function validateFieldDatatype( string $expectedType, $value ): bool { |
| 17 | switch ( $expectedType ) { |
| 18 | case 'bool': |
| 19 | return is_bool( $value ); |
| 20 | case 'int': |
| 21 | return is_int( $value ); |
| 22 | case '?int': |
| 23 | return $value === null || is_int( $value ); |
| 24 | case 'string': |
| 25 | return is_string( $value ); |
| 26 | case '?string': |
| 27 | return $value === null || is_string( $value ); |
| 28 | case 'int[]': |
| 29 | if ( !is_array( $value ) ) { |
| 30 | // If it is not an array, it cannot be an array of integers |
| 31 | return false; |
| 32 | } |
| 33 | foreach ( $value as $key => $item ) { |
| 34 | if ( !is_int( $item ) ) { |
| 35 | return false; |
| 36 | } |
| 37 | } |
| 38 | return true; |
| 39 | case 'array': |
| 40 | return is_array( $value ); |
| 41 | case '?array': |
| 42 | return $value === null || is_array( $value ); |
| 43 | case 'array<string,string>': |
| 44 | if ( !is_array( $value ) ) { |
| 45 | // If it is not an array, it cannot be an array of the intended format |
| 46 | return false; |
| 47 | } |
| 48 | foreach ( $value as $key => $item ) { |
| 49 | if ( !is_string( $key ) || !is_string( $item ) ) { |
| 50 | return false; |
| 51 | } |
| 52 | } |
| 53 | return true; |
| 54 | case 'array<int,array>': |
| 55 | if ( !is_array( $value ) ) { |
| 56 | // If it is not an array, it cannot be an array of the expected format |
| 57 | return false; |
| 58 | } |
| 59 | foreach ( $value as $key => $item ) { |
| 60 | if ( !is_int( $key ) || !is_array( $item ) ) { |
| 61 | return false; |
| 62 | } |
| 63 | } |
| 64 | return true; |
| 65 | case 'array<int,array<string,string>>': |
| 66 | if ( !is_array( $value ) ) { |
| 67 | // If it is not an array, it cannot be an array of the expected format |
| 68 | return false; |
| 69 | } |
| 70 | foreach ( $value as $key => $subarray ) { |
| 71 | if ( !is_int( $key ) || !is_array( $subarray ) ) { |
| 72 | return false; |
| 73 | } |
| 74 | foreach ( $subarray as $subkey => $item ) { |
| 75 | if ( !is_string( $subkey ) || !is_string( $item ) ) { |
| 76 | return false; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return true; |
| 81 | default: |
| 82 | // No validation branch was executed, unsupported datatype |
| 83 | throw new InvalidArgumentException( |
| 84 | "Unsupported datatype $expectedType passed to validateFieldDatatype" |
| 85 | ); |
| 86 | } |
| 87 | } |
| 88 | } |