From cdf2656f49aa32caac3b3f1b0230e9dca062c4dc Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 13 Jun 2022 23:34:29 +0300 Subject: [PATCH 1/4] Add length rule: "The :attribute must must have an exact length of :length" --- src/Rules/Length.php | 33 +++++++++++++++++++++++++++++++++ src/Validator.php | 1 + 2 files changed, 34 insertions(+) create mode 100644 src/Rules/Length.php diff --git a/src/Rules/Length.php b/src/Rules/Length.php new file mode 100644 index 0000000..66365e7 --- /dev/null +++ b/src/Rules/Length.php @@ -0,0 +1,33 @@ +requireParameters($this->fillableParams); + + $this->requireParameters($this->fillableParams); + + $length = (int) $this->parameter('length'); + + return strlen((string) $value) == $length; + } +} diff --git a/src/Validator.php b/src/Validator.php index dfa19c5..71e65ce 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -162,6 +162,7 @@ protected function registerBaseValidators() 'defaults' => new Rules\Defaults, 'default' => new Rules\Defaults, // alias of defaults 'nullable' => new Rules\Nullable, + 'length' => new Rules\Length, ]; foreach ($baseValidator as $key => $validator) { From 8a7fe27159fa4c8a742e0171be25d76a9b9e40c2 Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 13 Jun 2022 23:46:23 +0300 Subject: [PATCH 2/4] There can be several rules with the same name for a field --- src/Attribute.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Attribute.php b/src/Attribute.php index ebd7a11..c3626a1 100644 --- a/src/Attribute.php +++ b/src/Attribute.php @@ -47,8 +47,10 @@ public function __construct( $this->validation = $validation; $this->alias = $alias; $this->key = $key; + $rule_number = 0; foreach ($rules as $rule) { - $this->addRule($rule); + $this->addRule($rule, $rule_number); + $rule_number++; } } @@ -125,11 +127,11 @@ public function getOtherAttributes(): array * @param \Rakit\Validation\Rule $rule * @return void */ - public function addRule(Rule $rule) + public function addRule(Rule $rule, int $rule_number) { $rule->setAttribute($this); $rule->setValidation($this->validation); - $this->rules[$rule->getKey()] = $rule; + $this->rules[$rule->getKey() . (string)$rule_number] = $rule; } /** From 9b8eef4c6fc2b4830de215ef5869bb877bd1e5af Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 27 Jun 2022 09:36:21 +0300 Subject: [PATCH 3/4] remove repeating string --- src/Rules/Length.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Rules/Length.php b/src/Rules/Length.php index 66365e7..ebe3457 100644 --- a/src/Rules/Length.php +++ b/src/Rules/Length.php @@ -24,8 +24,6 @@ public function check($value): bool { $this->requireParameters($this->fillableParams); - $this->requireParameters($this->fillableParams); - $length = (int) $this->parameter('length'); return strlen((string) $value) == $length; From 94f129473431531006aa6991a49873ac18600cca Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 27 Jun 2022 09:39:18 +0300 Subject: [PATCH 4/4] Add length in rule: "The :attribute must have length only :allowed_values" where a field can be several length values --- src/Rules/LengthIn.php | 61 ++++++++++++++++++++++++++++++++++++++++++ src/Validator.php | 1 + 2 files changed, 62 insertions(+) create mode 100644 src/Rules/LengthIn.php diff --git a/src/Rules/LengthIn.php b/src/Rules/LengthIn.php new file mode 100644 index 0000000..ad30131 --- /dev/null +++ b/src/Rules/LengthIn.php @@ -0,0 +1,61 @@ +params + * + * @param array $params + * @return self + */ + public function fillParameters(array $params): Rule + { + if (count($params) == 1 && is_array($params[0])) { + $params = $params[0]; + } + $this->params['allowed_values'] = $params; + return $this; + } + + /** + * Set strict value + * + * @param bool $strict + * @return void + */ + public function strict(bool $strict = true) + { + $this->strict = $strict; + } + + /** + * Check $value is existed + * + * @param mixed $value + * @return bool + */ + public function check($value): bool + { + $this->requireParameters(['allowed_values']); + + $allowedValues = $this->parameter('allowed_values'); + + return in_array( strlen((string)$value), $allowedValues, $this->strict); + } +} diff --git a/src/Validator.php b/src/Validator.php index 71e65ce..0a87d01 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -163,6 +163,7 @@ protected function registerBaseValidators() 'default' => new Rules\Defaults, // alias of defaults 'nullable' => new Rules\Nullable, 'length' => new Rules\Length, + 'length_in' => new Rules\LengthIn, ]; foreach ($baseValidator as $key => $validator) {