AfterRule.php
3 weeks ago
ArrayRule.php
3 weeks ago
BaseRule.php
3 weeks ago
BooleanRule.php
3 weeks ago
DateFormatRule.php
3 weeks ago
DateRule.php
3 weeks ago
DateTimeRule.php
3 weeks ago
EmailRule.php
3 weeks ago
EmailUniqueRule.php
3 weeks ago
ExistsRule.php
3 weeks ago
FloatRule.php
3 weeks ago
GreaterThanEqualRule.php
3 weeks ago
GreaterThanRule.php
3 weeks ago
InRule.php
3 weeks ago
IntegerRule.php
3 weeks ago
IsValidImageIdRule.php
3 weeks ago
LessThanEqualRule.php
3 weeks ago
LessThanRule.php
3 weeks ago
MaxRule.php
3 weeks ago
MinRule.php
3 weeks ago
NotInRule.php
3 weeks ago
NullableRule.php
3 weeks ago
NumberRule.php
3 weeks ago
ObjectRule.php
3 weeks ago
ProhibitedIfRule.php
3 weeks ago
ProhibitedRule.php
3 weeks ago
RegexRule.php
3 weeks ago
RequiredIfExists.php
3 weeks ago
RequiredIfRule.php
3 weeks ago
RequiredIfSiblingRule.php
3 weeks ago
RequiredRule.php
3 weeks ago
SameAsRule.php
3 weeks ago
Sanitizer.php
3 weeks ago
StringRule.php
3 weeks ago
UniqueRule.php
3 weeks ago
UrlRule.php
3 weeks ago
UserExists.php
3 weeks ago
BaseRule.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Abstract base class for validation rules. |
| 5 | * |
| 6 | * @package Framework |
| 7 | * @subpackage Validation\Rules |
| 8 | * @since 1.0.0 |
| 9 | */ |
| 10 | namespace Kirki\Framework\Validation\Rules; |
| 11 | |
| 12 | \defined('ABSPATH') || exit; |
| 13 | use Kirki\Framework\Contracts\Rule; |
| 14 | use function Kirki\Framework\message; |
| 15 | abstract class BaseRule implements Rule |
| 16 | { |
| 17 | /** |
| 18 | * Check for strict data type |
| 19 | * |
| 20 | * @var bool |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | protected $check_strict_data_type = \false; |
| 25 | /** |
| 26 | * The field key being validated. |
| 27 | * |
| 28 | * @var string|null |
| 29 | * |
| 30 | * @since 1.0.0 |
| 31 | */ |
| 32 | protected $key; |
| 33 | /** |
| 34 | * The value to validate. |
| 35 | * |
| 36 | * @var mixed |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | protected $value; |
| 41 | /** |
| 42 | * The value/values for specific rule. |
| 43 | * |
| 44 | * @var mixed |
| 45 | * |
| 46 | * @since 1.0.0 |
| 47 | */ |
| 48 | protected $rule_value; |
| 49 | /** |
| 50 | * All applied rules for validation. |
| 51 | * |
| 52 | * @var mixed |
| 53 | * |
| 54 | * @since 1.0.0 |
| 55 | */ |
| 56 | protected $all_applied_rules; |
| 57 | /** |
| 58 | * The input data. |
| 59 | * |
| 60 | * @var array |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | */ |
| 64 | protected $data; |
| 65 | /** |
| 66 | * Create a new rule instance. |
| 67 | * |
| 68 | * @param string|null $key The field name. |
| 69 | * @param mixed $value The value to validate. |
| 70 | * @param mixed $rule_value The value to that can be passed to the rule. |
| 71 | * @param array $data The input data. |
| 72 | * @param array $all_applied_rules All applied rules |
| 73 | * |
| 74 | * @return void |
| 75 | * |
| 76 | * @since 1.0.0 |
| 77 | */ |
| 78 | public function __construct($key = null, $value = null, $rule_value = null, $data = [], $all_applied_rules = []) |
| 79 | { |
| 80 | $this->key = $key; |
| 81 | $this->value = $value; |
| 82 | $this->rule_value = $rule_value; |
| 83 | $this->data = $data; |
| 84 | $this->all_applied_rules = $all_applied_rules; |
| 85 | } |
| 86 | /** |
| 87 | * Set the field key. |
| 88 | * |
| 89 | * @param string $key The key. |
| 90 | * |
| 91 | * @return void |
| 92 | * |
| 93 | * @since 1.0.0 |
| 94 | */ |
| 95 | public function set_key(string $key) |
| 96 | { |
| 97 | $this->key = $key; |
| 98 | } |
| 99 | /** |
| 100 | * Set the value to validate. |
| 101 | * |
| 102 | * @param mixed $value The value. |
| 103 | * |
| 104 | * @return void |
| 105 | * |
| 106 | * @since 1.0.0 |
| 107 | */ |
| 108 | public function set_value($value) |
| 109 | { |
| 110 | $this->value = $value; |
| 111 | } |
| 112 | /** |
| 113 | * Get the value. |
| 114 | * |
| 115 | * @return string |
| 116 | * |
| 117 | * @since 1.0.0 |
| 118 | */ |
| 119 | public function get_value() |
| 120 | { |
| 121 | return $this->value; |
| 122 | } |
| 123 | /** |
| 124 | * Get the value for the rule. |
| 125 | * |
| 126 | * @return mixed |
| 127 | * |
| 128 | * @since 1.0.0 |
| 129 | */ |
| 130 | public function value() |
| 131 | { |
| 132 | return $this->value; |
| 133 | } |
| 134 | /** |
| 135 | * Check if the rule is for a specific data type. |
| 136 | * |
| 137 | * @return bool |
| 138 | * |
| 139 | * @since 1.0.0 |
| 140 | */ |
| 141 | public function is_check_strict_data_type() |
| 142 | { |
| 143 | return $this->check_strict_data_type; |
| 144 | } |
| 145 | /** |
| 146 | * Determine if the value is valid. |
| 147 | * |
| 148 | * @return bool |
| 149 | * |
| 150 | * @since 1.0.0 |
| 151 | */ |
| 152 | public function is_valid() |
| 153 | { |
| 154 | if ($this->ignore_rule_check()) { |
| 155 | return \true; |
| 156 | } |
| 157 | return $this->validate_rule(); |
| 158 | } |
| 159 | /** |
| 160 | * Ignore rule check if value is empty |
| 161 | * |
| 162 | * @return bool |
| 163 | * |
| 164 | * @since 1.0.0 |
| 165 | */ |
| 166 | protected function ignore_rule_check() |
| 167 | { |
| 168 | return $this->value === null; |
| 169 | } |
| 170 | /** |
| 171 | * Get the last key segment. |
| 172 | * |
| 173 | * @return string |
| 174 | * |
| 175 | * @since 1.0.0 |
| 176 | */ |
| 177 | protected function last_key_segment() |
| 178 | { |
| 179 | $parts = \explode('.', $this->key); |
| 180 | return $parts[\count($parts) - 1]; |
| 181 | } |
| 182 | /** |
| 183 | * Get the value for the rule. |
| 184 | * |
| 185 | * @return mixed |
| 186 | * |
| 187 | * @since 1.0.0 |
| 188 | */ |
| 189 | public function rule_value() |
| 190 | { |
| 191 | return $this->rule_value; |
| 192 | } |
| 193 | /** |
| 194 | * Validate the rule with desired value. |
| 195 | * |
| 196 | * @return bool |
| 197 | * |
| 198 | * @since 1.0.0 |
| 199 | */ |
| 200 | public abstract function validate_rule(); |
| 201 | /** |
| 202 | * Get the error message for this rule. |
| 203 | * |
| 204 | * @return string |
| 205 | * |
| 206 | * @since 1.0.0 |
| 207 | */ |
| 208 | public function get_error_message() |
| 209 | { |
| 210 | return message('validator.invalid', $this->last_key_segment()); |
| 211 | } |
| 212 | } |
| 213 |