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
MinRule.php
74 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Rule to ensure the minium value. |
| 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\Validation\Constants\Validation; |
| 14 | use function Kirki\Framework\message; |
| 15 | class MinRule extends BaseRule |
| 16 | { |
| 17 | /** |
| 18 | * Check if the value is valid. |
| 19 | * |
| 20 | * @return bool |
| 21 | * |
| 22 | * @since 1.0.0 |
| 23 | */ |
| 24 | public function validate_rule() |
| 25 | { |
| 26 | if ($this->is_string_value()) { |
| 27 | return \strlen($this->value) >= $this->rule_value; |
| 28 | } elseif ($this->is_array_value()) { |
| 29 | return \count($this->value) >= $this->rule_value; |
| 30 | } |
| 31 | return $this->value >= $this->rule_value; |
| 32 | } |
| 33 | /** |
| 34 | * Get the error message if the value is less than the min value. |
| 35 | * |
| 36 | * @return string |
| 37 | * |
| 38 | * @since 1.0.0 |
| 39 | */ |
| 40 | public function get_error_message() |
| 41 | { |
| 42 | if ($this->is_string_value()) { |
| 43 | return message('validator.min.characters', $this->last_key_segment(), $this->rule_value); |
| 44 | } elseif ($this->is_array_value()) { |
| 45 | return message('validator.min.items', $this->last_key_segment(), $this->rule_value); |
| 46 | } |
| 47 | return message('validator.min.numeric', $this->last_key_segment(), $this->rule_value); |
| 48 | } |
| 49 | /** |
| 50 | * Determine whether the string value. |
| 51 | * |
| 52 | * @return bool |
| 53 | * |
| 54 | * @since 1.0.0 |
| 55 | */ |
| 56 | protected function is_string_value() |
| 57 | { |
| 58 | $is_string = \in_array('string', $this->all_applied_rules, \true) || \in_array(Validation::RULE_MAP['string'], $this->all_applied_rules, \true); |
| 59 | return $is_string || \is_string($this->value) && !\is_numeric($this->value); |
| 60 | } |
| 61 | /** |
| 62 | * Determine whether the array value. |
| 63 | * |
| 64 | * @return bool |
| 65 | * |
| 66 | * @since 1.0.0 |
| 67 | */ |
| 68 | protected function is_array_value() |
| 69 | { |
| 70 | $is_array = \in_array('array', $this->all_applied_rules, \true) || \in_array(Validation::RULE_MAP['array'], $this->all_applied_rules, \true); |
| 71 | return $is_array || \is_array($this->value); |
| 72 | } |
| 73 | } |
| 74 |