/**
* Validate an attribute is contained within a list of values.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
protected function validateIn($attribute, $value, $parameters)
{
if (is_array($value) && $this->hasRule($attribute, 'Array')) {
return count(array_diff($value, $parameters)) == 0;
}// if it is value and this get the count
return ! is_array($value) && in_array((string) $value, $parameters);
}//validate an attribute is contained within a list of values
// i like this two way , just return this result
/**
* Validate an attribute is not contained within a list of values.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
protected function validateNotIn($attribute, $value, $parameters)
{
return ! $this->validateIn($attribute, $value, $parameters);// return a wrap function
}//validate an attribute is not contained within a list of values
/**
* Validate the uniqueness of an attribute value on a given database table.
*
* If a database column is not specified, the attribute will be used.
*
* @param string $attribute
* @param mixed $value
* @param array $parameters
* @return bool
*/
// validate the uniqueness of an attribute value on a given database table.
// if a database column is not specified, the attribute will be used.
protected function validateUnique($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'unique');//get this function
list($connection, $table) = $this->parseTable($parameters[0]);// list some method
// The second parameter position holds the name of the column that needs to
// be verified as unique. If this parameter isn't specified we will just
// assume that this column to be verified shares the attribute's name.
$column = isset($parameters[1]) ? $parameters[1] : $attribute;
// The second parameter position holds the name of the column that needs to
// be verified as unique. If this parameter isn't specified we will just
// assume that this column to be verified shares the attribute's name.
list($idColumn, $id) = [null, null];// init this parameter
if (isset($parameters[2])) {
list($idColumn, $id) = $this->getUniqueIds($parameters);//a good method
if (strtolower($id) == 'null') {
$id = null;
}// set the number
}
// The presence verifier is responsible for counting rows within this store
// mechanism which might be a relational database or any other permanent
// data store like Redis, etc. We will use it to determine uniqueness.
$verifier = $this->getPresenceVerifier();//get Presence Verifier
$verifier->setConnection($connection);// set Connection
$extra = $this->getUniqueExtra($parameters);//get Unique Extra
return $verifier->getCount(
$table, $column, $value, $id, $idColumn, $extra
) == 0;// get Count
}// so powerfull
/**
* Parse the connection / table for the unique / exists rules.
*
* @param string $table
* @return array
*/
protected function parseTable($table)
{
return Str::contains($table, '.') ? explode('.', $table, 2) : [null, $table];
}//Parse the connection table for the unique and exists rules.
[李景山php]每天laravel-20161008|Validator.php-8
最新推荐文章于 2021-03-11 06:19:26 发布