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

fix: remove traditional validation rule param types (2/2) #8124

Merged
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
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
Loading