//2016-08-16
/**
* Parse a parameter list.
*
* @param string $rule
* @param string $parameter
* @return array
*/
protected function parseParameters($rule, $parameter)
{//Parse a parameter list
if (strtolower($rule) == 'regex') {
return [$parameter];
}// if rule == regex ,just return it
return str_getcsv($parameter);// else return other type
}
/**
* Normalizes a rule so that we can accept short types.
*
* @param string $rule
* @return string
*/
protected function normalizeRule($rule)
{
switch ($rule) {
case 'Int':
return 'Integer';// return like break
case 'Bool':
return 'Boolean';// return
default:
return $rule;//normal
}
}// use this method we can do some short rule
/**
* Determine if the given rule depends on other fields.
*
* @param string $rule
* @return bool
*/
protected function dependsOnOtherFields($rule)
{
return in_array($rule, $this->dependentRules);
}// determine if the given rule depends on other fields.
/**
* Get the numeric keys from an attribute flattened with dot notation.
*
* E.g. 'foo.1.bar.2.baz' -> [1, 2]
*
* @param string $attribute
* @return array
*/
protected function getNumericKeys($attribute)
{
if (preg_match_all('/\.(\d+)\./', $attribute, $keys)) {
return $keys[1];
}
return [];
}// from a special string to get the numeric array
/**
* Replace each field parameter which has asterisks with the given numeric keys.
*
* @param array $parameters
* @param array $keys
* @return array
*/
protected function replaceAsterisksInParameters(array $parameters, array $keys)
{
return array_map(function ($field) use ($keys) {
return $this->replaceAsterisksWithKeys($field, $keys);
}, $parameters);
}//more change this parameter has asterisks
/**
* Replace asterisks with numeric keys.
*
* E.g. 'foo.*.bar.*.baz', [1, 2] -> foo.1.bar.2.baz
*
* @param string $field
* @param array $keys
* @return string
*/
protected function replaceAsterisksWithKeys($field, array $keys)
{
return vsprintf(str_replace('*', '%d', $field), $keys);
}//replace asterisks with numeric keys
/**
* Get the array of custom validator extensions.
*
* @return array
*/
public function getExtensions()
{
return $this->extensions;
}// Get the array of custom validator extensions.
/**
* Register an array of custom validator extensions.
*
* @param array $extensions
* @return void
*/
public function addExtensions(array $extensions)
{
if ($extensions) {
$keys = array_map('\Illuminate\Support\Str::snake', array_keys($extensions));
$extensions = array_combine($keys, array_values($extensions));
}//extensions
// get array_combine
$this->extensions = array_merge($this->extensions, $extensions);
}//register an array of custom validator extensions.
/**
* Register an array of custom implicit validator extensions.
*
* @param array $extensions
* @return void
*/
public function addImplicitExtensions(array $extensions)
{
$this->addExtensions($extensions);//add Extensions
foreach ($extensions as $rule => $extension) {// loop extensions
$this->implicitRules[] = Str::studly($rule);
}
}// register an array of custom implicit validator extensions
/**
* Register a custom validator extension.
*
* @param string $rule
* @param \Closure|string $extension
* @return void
*/
public function addExtension($rule, $extension)
{
$this->extensions[Str::snake($rule)] = $extension;
}// register a custom validator extension
/**
* Register a custom implicit validator extension.
*
* @param string $rule
* @param \Closure|string $extension
* @return void
*/
public function addImplicitExtension($rule, $extension)
{
$this->addExtension($rule, $extension);
$this->implicitRules[] = Str::studly($rule);
}// register a custom implicit validator extension
/**
* Get the array of custom validator message replacers.
*
* @return array
*/
public function getReplacers()
{
return $this->replacers;
}// get the repalce
/**
* Register an array of custom validator message replacers.
*
* @param array $replacers
* @return void
*/
public function addReplacers(array $replacers)
{
if ($replacers) {
$keys = array_map('\Illuminate\Support\Str::snake', array_keys($replacers));
$replacers = array_combine($keys, array_values($replacers));
}// repalce
$this->replacers = array_merge($this->replacers, $replacers);
}//add Replacers
/**
* Register a custom validator message replacer.
*
* @param string $rule
* @param \Closure|string $replacer
* @return void
*/
public function addReplacer($rule, $replacer)
{
$this->replacers[Str::snake($rule)] = $replacer;
}// add Replace
/**
* Get the data under validation.
*
* @return array
*/
public function getData()
{
return $this->data;
}// get Data
/**
* Set the data under validation.
*
* @param array $data
* @return void
*/
public function setData(array $data)
{
$this->data = $this->parseData($data);
}// set Data
/**
* Get the validation rules.
*
* @return array
*/
public function getRules()
{
return $this->rules;
}//get Rules
/**
* Set the validation rules.
*
* @param array $rules
* @return $this
*/
public function setRules(array $rules)
{
$this->rules = $this->explodeRules($rules);
return $this;
}//set Rules
/**
* Set the custom attributes on the validator.
*
* @param array $attributes
* @return $this
*/
public function setAttributeNames(array $attributes)
{
$this->customAttributes = $attributes;
return $this;
}
/**
* Set the custom values on the validator.
*
* @param array $values
* @return $this
*/
public function setValueNames(array $values)
{
$this->customValues = $values;
return $this;
}
/**
* Get the files under validation.
*
* @return array
*/
public function getFiles()
{
return $this->files;
}
/**
* Set the files under validation.
*
* @param array $files
* @return $this
*/
public function setFiles(array $files)
{
$this->files = $files;
return $this;
}
/**
* Get the Presence Verifier implementation.
*
* @return \Illuminate\Validation\PresenceVerifierInterface
*
* @throws \RuntimeException
*/
public function getPresenceVerifier()
{
if (! isset($this->presenceVerifier)) {
throw new RuntimeException('Presence verifier has not been set.');
}
return $this->presenceVerifier;
}
/**
* Set the Presence Verifier implementation.
*
* @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier
* @return void
*/
public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
{
$this->presenceVerifier = $presenceVerifier;
}
/**
* Get the Translator implementation.
*
* @return \Symfony\Component\Translation\TranslatorInterface
*/
public function getTranslator()
{
return $this->translator;
}
/**
* Set the Translator implementation.
*
* @param \Symfony\Component\Translation\TranslatorInterface $translator
* @return void
*/
public function setTranslator(TranslatorInterface $translator)
{
$this->translator = $translator;
}
/**
* Get the custom messages for the validator.
*
* @return array
*/
public function getCustomMessages()
{
return $this->customMessages;
}
/**
* Set the custom messages for the validator.
*
* @param array $messages
* @return void
*/
public function setCustomMessages(array $messages)
{
$this->customMessages = array_merge($this->customMessages, $messages);
}
/**
* Get the custom attributes used by the validator.
*
* @return array
*/
public function getCustomAttributes()
{
return $this->customAttributes;
}
/**
* Add custom attributes to the validator.
*
* @param array $customAttributes
* @return $this
*/
public function addCustomAttributes(array $customAttributes)
{
$this->customAttributes = array_merge($this->customAttributes, $customAttributes);
return $this;
}//add
/**
* Get the custom values for the validator.
*
* @return array
*/
public function getCustomValues()
{
return $this->customValues;
}//get
/**
* Add the custom values for the validator.
*
* @param array $customValues
* @return $this
*/
public function addCustomValues(array $customValues)
{
$this->customValues = array_merge($this->customValues, $customValues);
return $this;
}//add
/**
* Get the fallback messages for the validator.
*
* @return array
*/
public function getFallbackMessages()
{
return $this->fallbackMessages;
}
/**
* Set the fallback messages for the validator.
*
* @param array $messages
* @return void
*/
public function setFallbackMessages(array $messages)
{
$this->fallbackMessages = $messages;
}
/**
* Get the failed validation rules.
*
* @return array
*/
public function failed()
{
return $this->failedRules;
}
/**
* Get the message container for the validator.
*
* @return \Illuminate\Support\MessageBag
*/
public function messages()
{
if (! $this->messages) {
$this->passes();
}
return $this->messages;
}
/**
* An alternative more semantic shortcut to the message container.
*
* @return \Illuminate\Support\MessageBag
*/
public function errors()
{
return $this->messages();
}//get
/**
* Get the messages for the instance.
*
* @return \Illuminate\Support\MessageBag
*/
public function getMessageBag()
{
return $this->messages();
}
/**
* Set the IoC container instance.
*
* @param \Illuminate\Contracts\Container\Container $container
* @return void
*/
public function setContainer(Container $container)
{
$this->container = $container;
}//add
/**
* Call a custom validator extension.
*
* @param string $rule
* @param array $parameters
* @return bool|null
*/
protected function callExtension($rule, $parameters)
{
$callback = $this->extensions[$rule];
if ($callback instanceof Closure) {
return call_user_func_array($callback, $parameters);
} elseif (is_string($callback)) {
return $this->callClassBasedExtension($callback, $parameters);
}
}// call function
/**
* Call a class based validator extension.
*
* @param string $callback
* @param array $parameters
* @return bool
*/
protected function callClassBasedExtension($callback, $parameters)
{
list($class, $method) = explode('@', $callback);
return call_user_func_array([$this->container->make($class), $method], $parameters);
}//call user func array
/**
* Call a custom validator message replacer.
*
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string|null
*/
protected function callReplacer($message, $attribute, $rule, $parameters)
{
$callback = $this->replacers[$rule];
if ($callback instanceof Closure) {
return call_user_func_array($callback, func_get_args());
} elseif (is_string($callback)) {
return $this->callClassBasedReplacer($callback, $message, $attribute, $rule, $parameters);
}
}//call replace
/**
* Call a class based validator message replacer.
*
* @param string $callback
* @param string $message
* @param string $attribute
* @param string $rule
* @param array $parameters
* @return string
*/
protected function callClassBasedReplacer($callback, $message, $attribute, $rule, $parameters)
{
list($class, $method) = explode('@', $callback);
return call_user_func_array([$this->container->make($class), $method], array_slice(func_get_args(), 1));
}//call class
/**
* Require a certain number of parameters to be present.
*
* @param int $count
* @param array $parameters
* @param string $rule
* @return void
*
* @throws \InvalidArgumentException
*/
protected function requireParameterCount($count, $parameters, $rule)
{
if (count($parameters) < $count) {
throw new InvalidArgumentException("Validation rule $rule requires at least $count parameters.");
}
}// require a certain
/**
* Handle dynamic calls to class methods.
*
* @param string $method
* @param array $parameters
* @return mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, $parameters)
{
$rule = Str::snake(substr($method, 8));
if (isset($this->extensions[$rule])) {
return $this->callExtension($rule, $parameters);
}
throw new BadMethodCallException("Method [$method] does not exist.");
}// a __call
}
本文出自 “专注php 群号:414194301” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1839433