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