Skip to content

Commit

Permalink
Merge pull request #8124 from kenjis/fix-traditional-validation-rule-…
Browse files Browse the repository at this point in the history
…param-types-2

fix: remove traditional validation rule param types (2/2)
  • Loading branch information
kenjis authored Nov 3, 2023
2 parents 4e57cee + 8a3a9a0 commit ea357ba
Showing 1 changed file with 84 additions and 12 deletions.
96 changes: 84 additions & 12 deletions system/Validation/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,30 @@ public function differs(?string $str, string $field, array $data): bool

/**
* Equals the static value provided.
*
* @param string|null $str
*/
public function equals(?string $str, string $val): bool
public function equals($str, string $val): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

return $str === $val;
}

/**
* Returns true if $str is $val characters long.
* $val = "5" (one) | "5,8,12" (multiple values)
*
* @param string|null $str
*/
public function exact_length(?string $str, string $val): bool
public function exact_length($str, string $val): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

$val = explode(',', $val);

foreach ($val as $tmp) {
Expand All @@ -62,17 +74,29 @@ public function exact_length(?string $str, string $val): bool

/**
* Greater than
*
* @param string|null $str
*/
public function greater_than(?string $str, string $min): bool
public function greater_than($str, string $min): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

return is_numeric($str) && $str > $min;
}

/**
* Equal to or Greater than
*
* @param string|null $str
*/
public function greater_than_equal_to(?string $str, string $min): bool
public function greater_than_equal_to($str, string $min): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

return is_numeric($str) && $str >= $min;
}

Expand All @@ -84,9 +108,15 @@ public function greater_than_equal_to(?string $str, string $min): bool
* Example:
* is_not_unique[table.field,where_field,where_value]
* is_not_unique[menu.id,active,1]
*
* @param string|null $str
*/
public function is_not_unique(?string $str, string $field, array $data): bool
public function is_not_unique($str, string $field, array $data): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

// Grab any data for exclusion of a single row.
[$field, $whereField, $whereValue] = array_pad(
explode(',', $field),
Expand Down Expand Up @@ -115,9 +145,15 @@ public function is_not_unique(?string $str, string $field, array $data): bool

/**
* Value should be within an array of values
*
* @param string|null $value
*/
public function in_list(?string $value, string $list): bool
public function in_list($value, string $list): bool
{
if (! is_string($value) && $value !== null) {
$value = (string) $value;
}

$list = array_map('trim', explode(',', $list));

return in_array($value, $list, true);
Expand Down Expand Up @@ -166,17 +202,29 @@ public function is_unique($str, string $field, array $data): bool

/**
* Less than
*
* @param string|null $str
*/
public function less_than(?string $str, string $max): bool
public function less_than($str, string $max): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

return is_numeric($str) && $str < $max;
}

/**
* Equal to or Less than
*
* @param string|null $str
*/
public function less_than_equal_to(?string $str, string $max): bool
public function less_than_equal_to($str, string $max): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

return is_numeric($str) && $str <= $max;
}

Expand All @@ -196,33 +244,57 @@ public function matches(?string $str, string $field, array $data): bool

/**
* Returns true if $str is $val or fewer characters in length.
*
* @param string|null $str
*/
public function max_length(?string $str, string $val): bool
public function max_length($str, string $val): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

return is_numeric($val) && $val >= mb_strlen($str ?? '');
}

/**
* Returns true if $str is at least $val length.
*
* @param string|null $str
*/
public function min_length(?string $str, string $val): bool
public function min_length($str, string $val): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

return is_numeric($val) && $val <= mb_strlen($str ?? '');
}

/**
* Does not equal the static value provided.
*
* @param string|null $str
*/
public function not_equals(?string $str, string $val): bool
public function not_equals($str, string $val): bool
{
if (! is_string($str) && $str !== null) {
$str = (string) $str;
}

return $str !== $val;
}

/**
* Value should not be within an array of values.
*
* @param string|null $value
*/
public function not_in_list(?string $value, string $list): bool
public function not_in_list($value, string $list): bool
{
if (! is_string($value) && $value !== null) {
$value = (string) $value;
}

return ! $this->in_list($value, $list);
}

Expand Down

0 comments on commit ea357ba

Please sign in to comment.