Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Length in #152

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions src/Rules/Length.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rakit\Validation\Rules;

use Rakit\Validation\Rule;

class Length extends Rule
{
use Traits\SizeTrait;

/** @var string */
protected $message = "The :attribute must must have an exact length of :length";

/** @var array */
protected $fillableParams = ['length'];

/**
* Check the $value is valid
*
* @param mixed $value
* @return bool
*/
public function check($value): bool
{
$this->requireParameters($this->fillableParams);

$length = (int) $this->parameter('length');

return strlen((string) $value) == $length;
}
}
61 changes: 61 additions & 0 deletions src/Rules/LengthIn.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Rakit\Validation\Rules;

use Rakit\Validation\Helper;
use Rakit\Validation\Rule;
/**
* Check if attribute has length in allowed values
*
*
*/
class LengthIn extends Rule
{

/** @var string */
protected $message = "The :attribute must have length only :allowed_values";

/** @var bool */
protected $strict = false;

/**
* Given $params and assign the $this->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);
}
}
2 changes: 2 additions & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ protected function registerBaseValidators()
'defaults' => new Rules\Defaults,
'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) {
Expand Down