| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\Admin\Builder; |
| 4 |
|
| 5 |
/** |
| 6 |
* Rule |
| 7 |
* |
| 8 |
* @package NotificationX\Core |
| 9 |
*/ |
| 10 |
|
| 11 |
class Rules { |
| 12 |
|
| 13 |
public static function isOfType( $key, $value = false, $_not = false, $field = null ) { |
| 14 |
$rule = self::_isOfType( $key, $value, $_not ); |
| 15 |
return $field ? self::add( $rule, $field ) : $rule; |
| 16 |
} |
| 17 |
|
| 18 |
public static function _isOfType( $key, $value = false, $_not = false ) { |
| 19 |
return $_not ? new Rule( '!isOfType', $key, $value ) : new Rule( 'isOfType', $key, $value ); |
| 20 |
} |
| 21 |
|
| 22 |
public static function is( $key, $value = false, $_not = false, $field = null ) { |
| 23 |
$rule = self::_is( $key, $value, $_not ); |
| 24 |
return $field ? self::add( $rule, $field ) : $rule; |
| 25 |
} |
| 26 |
|
| 27 |
public static function includes( $key, $value = false, $_not = false, $field = null ) { |
| 28 |
if ( ! is_array( $value ) ) { |
| 29 |
$value = [ $value ]; |
| 30 |
} |
| 31 |
$rule = self::_includes( $key, $value, $_not ); |
| 32 |
return $field ? self::add( $rule, $field ) : $rule; |
| 33 |
} |
| 34 |
|
| 35 |
public static function _is( $key, $value = false, $_not = false ) { |
| 36 |
return $_not ? new Rule( '!is', $key, $value ) : new Rule( 'is', $key, $value ); |
| 37 |
} |
| 38 |
|
| 39 |
public static function _includes( $key, $value = false, $_not = false ) { |
| 40 |
return $_not ? new Rule( '!includes', $key, $value ) : new Rule( 'includes', $key, $value ); |
| 41 |
} |
| 42 |
|
| 43 |
public static function logicalRule( $rules, $operator = 'and', $field = null ) { |
| 44 |
if ( ! empty( $field ) ) { |
| 45 |
foreach ( $rules as $key => $rule ) { |
| 46 |
$field = self::add( $rule, $field, $operator ); |
| 47 |
} |
| 48 |
return $field; |
| 49 |
} |
| 50 |
return self::_logicalRule( $rules, $operator ); |
| 51 |
} |
| 52 |
|
| 53 |
public static function _logicalRule( $rules, $operator = 'and' ) { |
| 54 |
// if operator already exist then then append the rest rules in it. |
| 55 |
if ( ! empty( $rules[0] ) && is_array( $rules[0] ) && ! empty( $rules[0][0] ) && $rules[0][0] == $operator ) { |
| 56 |
$first = $rules[0]; |
| 57 |
unset( $rules[0] ); |
| 58 |
return array_merge( $first, $rules ); |
| 59 |
} |
| 60 |
return array_merge( [ $operator ], $rules ); |
| 61 |
} |
| 62 |
|
| 63 |
public static function add( $rule, $field, $operator = 'and' ) { |
| 64 |
if ( empty( $field['rules'] ) ) { |
| 65 |
$field['rules'] = $rule; |
| 66 |
} elseif ( ! self::add_rule( $rule, $field['rules'] ) ) { |
| 67 |
$field['rules'] = self::_logicalRule( [ $field['rules'], $rule ], $operator ); |
| 68 |
} |
| 69 |
return $field; |
| 70 |
} |
| 71 |
|
| 72 |
// Function to recursively search for a given value |
| 73 |
public static function add_rule( $search_value, $array ) { |
| 74 |
|
| 75 |
if ( is_array( $array ) && count( $array ) > 0 ) { |
| 76 |
foreach ( $array as $key => $value ) { |
| 77 |
if ( is_array( $value ) && count( $value ) > 0 ) { |
| 78 |
$return = self::add_rule( $search_value, $value ); |
| 79 |
if ( $return ) { |
| 80 |
return true; |
| 81 |
} |
| 82 |
} elseif ( is_a( $value, 'NotificationX\Core\Rule' ) && $value->can_add( $search_value ) ) { |
| 83 |
$value->add_value( $search_value ); |
| 84 |
return true; |
| 85 |
} |
| 86 |
} |
| 87 |
} elseif ( is_a( $array, 'NotificationX\Core\Rule' ) && $array->can_add( $search_value ) ) { |
| 88 |
$array->add_value( $search_value ); |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
return false; |
| 93 |
} |
| 94 |
} |
| 95 |
|