NotIn.php
TLDR
This file contains the NotIn
class, which is used for creating a "not in" validation rule. It checks if a value is not in a specified list of values.
Class
NotIn
The NotIn
class implements the Stringable
interface. It represents a "not in" validation rule and has the following properties and methods:
-
Properties:
-
$rule
: The name of the rule (string) -
$values
: The accepted values (array)
-
-
Method:
-
__construct($values)
: Constructs a new "not in" rule instance. The$values
parameter can be anArrayable
, an array or a string. -
__toString()
: Converts the rule to a validation string. The return value is a string representation of the rule, including the rule name and the list of values.
-
<?php
namespace Illuminate\Validation\Rules;
use BackedEnum;
use Illuminate\Contracts\Support\Arrayable;
use Stringable;
use UnitEnum;
class NotIn implements Stringable
{
/**
* The name of the rule.
*
* @var string
*/
protected $rule = 'not_in';
/**
* The accepted values.
*
* @var array
*/
protected $values;
/**
* Create a new "not in" rule instance.
*
* @param \Illuminate\Contracts\Support\Arrayable|array|string $values
* @return void
*/
public function __construct($values)
{
if ($values instanceof Arrayable) {
$values = $values->toArray();
}
$this->values = is_array($values) ? $values : func_get_args();
}
/**
* Convert the rule to a validation string.
*
* @return string
*/
public function __toString()
{
$values = array_map(function ($value) {
$value = match (true) {
$value instanceof BackedEnum => $value->value,
$value instanceof UnitEnum => $value->name,
default => $value,
};
return '"'.str_replace('"', '""', $value).'"';
}, $this->values);
return $this->rule.':'.implode(',', $values);
}
}