| @@ -9,88 +9,86 @@ | ||
| 9 | 9 | */ |
| 10 | 10 | |
| 11 | 11 | class Rules { |
| 12 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 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 | - } | |
| 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 | 62 | |
| 63 | - public static function add( $rule, $field, $operator = 'and' ) { | |
| 64 | - if ( empty( $field['rules'] ) ) { | |
| 65 | - $field['rules'] = $rule; | |
| 66 | - } else { | |
| 67 | - if ( ! self::add_rule( $rule, $field['rules'] ) ) { | |
| 68 | - $field['rules'] = self::_logicalRule( [$field['rules'], $rule], $operator ); | |
| 69 | - } | |
| 70 | - } | |
| 71 | - return $field; | |
| 72 | - } | |
| 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 | + } | |
| 73 | 71 | |
| 74 | - // Function to recursively search for a given value | |
| 75 | - public static function add_rule( $search_value, $array ) { | |
| 72 | + // Function to recursively search for a given value | |
| 73 | + public static function add_rule( $search_value, $array ) { | |
| 76 | 74 | |
| 77 | - if ( is_array( $array ) && count( $array ) > 0 ) { | |
| 78 | - foreach ( $array as $key => $value ) { | |
| 79 | - if ( is_array( $value ) && count( $value ) > 0 ) { | |
| 80 | - $return = self::add_rule( $search_value, $value ); | |
| 81 | - if ( $return ) { | |
| 82 | - return true; | |
| 83 | - } | |
| 84 | - } else if ( is_a( $value, 'NotificationX\Core\Rule' ) && $value->can_add( $search_value ) ) { | |
| 85 | - $value->add_value( $search_value ); | |
| 86 | - return true; | |
| 87 | - } | |
| 88 | - } | |
| 89 | - } elseif ( is_a( $array, 'NotificationX\Core\Rule' ) && $array->can_add( $search_value ) ) { | |
| 90 | - $array->add_value( $search_value ); | |
| 91 | - return true; | |
| 92 | - } | |
| 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 | + } | |
| 93 | 91 | |
| 94 | - return false; | |
| 95 | - } | |
| 92 | + return false; | |
| 93 | + } | |
| 96 | 94 | } |