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

[11.x] Fluent numeric validation #54425

Merged
merged 3 commits into from
Feb 5, 2025
Merged
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
11 changes: 11 additions & 0 deletions src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Illuminate\Validation\Rules\ImageFile;
use Illuminate\Validation\Rules\In;
use Illuminate\Validation\Rules\NotIn;
use Illuminate\Validation\Rules\Numeric;
use Illuminate\Validation\Rules\ProhibitedIf;
use Illuminate\Validation\Rules\RequiredIf;
use Illuminate\Validation\Rules\Unique;
Expand Down Expand Up @@ -232,4 +233,14 @@ public static function dimensions(array $constraints = [])
{
return new Dimensions($constraints);
}

/**
* Get a numeric rule builder instance.
*
* @return \Illuminate\Validation\Rules\Numeric
*/
public static function numeric()
{
return new Numeric;
}
}
230 changes: 230 additions & 0 deletions src/Illuminate/Validation/Rules/Numeric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
<?php

namespace Illuminate\Validation\Rules;

use Illuminate\Support\Arr;
use Illuminate\Support\Traits\Conditionable;
use Stringable;

class Numeric implements Stringable
{
use Conditionable;

/**
* The constraints for the number rule.
*/
protected array $constraints = ['numeric'];

/**
* The field under validation must have a size between the given min and max (inclusive).
*
* @param int|float $min
* @param int|float $max
* @return $this
*/
public function between(int|float $min, int|float $max): Numeric
{
return $this->addRule('between:'.$min.','.$max);
}

/**
* The field under validation must contain the specified number of decimal places.
*
* @param int $min
* @param int|null $max
* @return $this
*/
public function decimal(int $min, ?int $max = null): Numeric
{
$rule = 'decimal:'.$min;

if ($max !== null) {
$rule .= ','.$max;
}

return $this->addRule($rule);
}

/**
* The field under validation must have a different value than field.
*
* @param string $field
* @return $this
*/
public function different(string $field): Numeric
{
return $this->addRule('different:'.$field);
}

/**
* The integer under validation must have an exact number of digits.
*
* @param int $length
* @return $this
*/
public function digits(int $length): Numeric
{
return $this->integer()->addRule('digits:'.$length);
}

/**
* The integer under validation must between the given min and max number of digits.
*
* @param int $min
* @param int $max
* @return $this
*/
public function digitsBetween(int $min, int $max): Numeric
{
return $this->integer()->addRule('digits_between:'.$min.','.$max);
}

/**
* The field under validation must be greater than the given field or value.
*
* @param string $field
* @return $this
*/
public function greaterThan(string $field): Numeric
{
return $this->addRule('gt:'.$field);
}

/**
* The field under validation must be greater than or equal to the given field or value.
*
* @param string $field
* @return $this
*/
public function greaterThanOrEqualTo(string $field): Numeric
{
return $this->addRule('gte:'.$field);
}

/**
* The field under validation must be an integer.
*
* @return $this
*/
public function integer(): Numeric
{
return $this->addRule('integer');
}

/**
* The field under validation must be less than the given field.
*
* @param string $field
* @return $this
*/
public function lessThan(string $field): Numeric
{
return $this->addRule('lt:'.$field);
}

/**
* The field under validation must be less than or equal to the given field.
*
* @param string $field
* @return $this
*/
public function lessThanOrEqualTo(string $field): Numeric
{
return $this->addRule('lte:'.$field);
}

/**
* The field under validation must be less than or equal to a maximum value.
*
* @param int|float $value
* @return $this
*/
public function max(int|float $value): Numeric
{
return $this->addRule('max:'.$value);
}

/**
* The integer under validation must have a maximum number of digits.
*
* @param int $value
* @return $this
*/
public function maxDigits(int $value): Numeric
{
return $this->addRule('max_digits:'.$value);
}

/**
* The field under validation must have a minimum value.
*
* @param int|float $value
* @return $this
*/
public function min(int|float $value): Numeric
{
return $this->addRule('min:'.$value);
}

/**
* The integer under validation must have a minimum number of digits.
*
* @param int $value
* @return $this
*/
public function minDigits(int $value): Numeric
{
return $this->addRule('min_digits:'.$value);
}

/**
* The field under validation must be a multiple of the given value.
*
* @param int|float $value
* @return $this
*/
public function multipleOf(int|float $value): Numeric
{
return $this->addRule('multiple_of:'.$value);
}

/**
* The given field must match the field under validation.
*
* @param string $field
* @return $this
*/
public function same(string $field): Numeric
{
return $this->addRule('same:'.$field);
}

/**
* The field under validation must match the given value.
*
* @param int $value
* @return $this
*/
public function exactly(int $value): Numeric
{
return $this->integer()->addRule('size:'.$value);
}

/**
* Convert the rule to a validation string.
*/
public function __toString(): string
{
return implode('|', array_unique($this->constraints));
}

/**
* Add custom rules to the validation rules array.
*/
protected function addRule(array|string $rules): Numeric
{
$this->constraints = array_merge($this->constraints, Arr::wrap($rules));

return $this;
}
}
Loading
Loading