| @@ 7-38 (lines=32) @@ | ||
| 4 | ||
| 5 | use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
| 6 | ||
| 7 | class Any_Contain_Comparer extends Comparer { |
|
| 8 | ||
| 9 | /** |
|
| 10 | * Supported comparison signs |
|
| 11 | * |
|
| 12 | * @var array<string> |
|
| 13 | */ |
|
| 14 | protected $supported_comparison_operators = array( 'IN', 'NOT IN' ); |
|
| 15 | ||
| 16 | /** |
|
| 17 | * Check if comparison is true for $a and $b |
|
| 18 | * |
|
| 19 | * @param mixed $a |
|
| 20 | * @param string $comparison_operator |
|
| 21 | * @param mixed $b |
|
| 22 | * @return bool |
|
| 23 | */ |
|
| 24 | public function is_correct( $a, $comparison_operator, $b ) { |
|
| 25 | if ( ! is_array( $b ) ) { |
|
| 26 | Incorrect_Syntax_Exception::raise( 'Supplied comparison value is not an array: ' . print_r( $b, true ) ); |
|
| 27 | return false; |
|
| 28 | } |
|
| 29 | ||
| 30 | switch ( $comparison_operator ) { |
|
| 31 | case 'IN': |
|
| 32 | return ! empty( array_intersect( $a, $b ) ); |
|
| 33 | case 'NOT IN': |
|
| 34 | return empty( array_intersect( $a, $b ) ); |
|
| 35 | } |
|
| 36 | return false; |
|
| 37 | } |
|
| 38 | } |
|
| @@ 7-38 (lines=32) @@ | ||
| 4 | ||
| 5 | use Carbon_Fields\Exception\Incorrect_Syntax_Exception; |
|
| 6 | ||
| 7 | class Contain_Comparer extends Comparer { |
|
| 8 | ||
| 9 | /** |
|
| 10 | * Supported comparison signs |
|
| 11 | * |
|
| 12 | * @var array<string> |
|
| 13 | */ |
|
| 14 | protected $supported_comparison_operators = array( 'IN', 'NOT IN' ); |
|
| 15 | ||
| 16 | /** |
|
| 17 | * Check if comparison is true for $a and $b |
|
| 18 | * |
|
| 19 | * @param mixed $a |
|
| 20 | * @param string $comparison_operator |
|
| 21 | * @param mixed $b |
|
| 22 | * @return bool |
|
| 23 | */ |
|
| 24 | public function is_correct( $a, $comparison_operator, $b ) { |
|
| 25 | if ( ! is_array( $b ) ) { |
|
| 26 | Incorrect_Syntax_Exception::raise( 'Supplied comparison value is not an array: ' . print_r( $b, true ) ); |
|
| 27 | return false; |
|
| 28 | } |
|
| 29 | ||
| 30 | switch ( $comparison_operator ) { |
|
| 31 | case 'IN': |
|
| 32 | return in_array( $a, $b ); |
|
| 33 | case 'NOT IN': |
|
| 34 | return ! in_array( $a, $b ); |
|
| 35 | } |
|
| 36 | return false; |
|
| 37 | } |
|
| 38 | } |
|