Update v1.0.6
This commit is contained in:
@@ -1,127 +0,0 @@
|
||||
<?php namespace Illuminate\Validation;
|
||||
|
||||
use Illuminate\Database\ConnectionResolverInterface;
|
||||
|
||||
class DatabasePresenceVerifier implements PresenceVerifierInterface {
|
||||
|
||||
/**
|
||||
* The database connection instance.
|
||||
*
|
||||
* @var \Illuminate\Database\ConnectionResolverInterface
|
||||
*/
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* The database connection to use.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $connection = null;
|
||||
|
||||
/**
|
||||
* Create a new database presence verifier.
|
||||
*
|
||||
* @param \Illuminate\Database\ConnectionResolverInterface $db
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(ConnectionResolverInterface $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of objects in a collection having the given value.
|
||||
*
|
||||
* @param string $collection
|
||||
* @param string $column
|
||||
* @param string $value
|
||||
* @param int $excludeId
|
||||
* @param string $idColumn
|
||||
* @param array $extra
|
||||
* @return int
|
||||
*/
|
||||
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = array())
|
||||
{
|
||||
$query = $this->table($collection)->where($column, '=', $value);
|
||||
|
||||
if ( ! is_null($excludeId) && $excludeId != 'NULL')
|
||||
{
|
||||
$query->where($idColumn ?: 'id', '<>', $excludeId);
|
||||
}
|
||||
|
||||
foreach ($extra as $key => $extraValue)
|
||||
{
|
||||
$this->addWhere($query, $key, $extraValue);
|
||||
}
|
||||
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of objects in a collection with the given values.
|
||||
*
|
||||
* @param string $collection
|
||||
* @param string $column
|
||||
* @param array $values
|
||||
* @param array $extra
|
||||
* @return int
|
||||
*/
|
||||
public function getMultiCount($collection, $column, array $values, array $extra = array())
|
||||
{
|
||||
$query = $this->table($collection)->whereIn($column, $values);
|
||||
|
||||
foreach ($extra as $key => $extraValue)
|
||||
{
|
||||
$this->addWhere($query, $key, $extraValue);
|
||||
}
|
||||
|
||||
return $query->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a "where" clause to the given query.
|
||||
*
|
||||
* @param \Illuminate\Database\Query\Builder $query
|
||||
* @param string $key
|
||||
* @param string $extraValue
|
||||
* @return void
|
||||
*/
|
||||
protected function addWhere($query, $key, $extraValue)
|
||||
{
|
||||
if ($extraValue === 'NULL')
|
||||
{
|
||||
$query->whereNull($key);
|
||||
}
|
||||
elseif ($extraValue === 'NOT_NULL')
|
||||
{
|
||||
$query->whereNotNull($key);
|
||||
}
|
||||
else
|
||||
{
|
||||
$query->where($key, $extraValue);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a query builder for the given table.
|
||||
*
|
||||
* @param string $table
|
||||
* @return \Illuminate\Database\Query\Builder
|
||||
*/
|
||||
protected function table($table)
|
||||
{
|
||||
return $this->db->connection($this->connection)->table($table);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the connection to be used.
|
||||
*
|
||||
* @param string $connection
|
||||
* @return void
|
||||
*/
|
||||
public function setConnection($connection)
|
||||
{
|
||||
$this->connection = $connection;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,238 +0,0 @@
|
||||
<?php namespace Illuminate\Validation;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Contracts\Container\Container;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Illuminate\Contracts\Validation\Factory as FactoryContract;
|
||||
|
||||
class Factory implements FactoryContract {
|
||||
|
||||
/**
|
||||
* The Translator implementation.
|
||||
*
|
||||
* @var \Symfony\Component\Translation\TranslatorInterface
|
||||
*/
|
||||
protected $translator;
|
||||
|
||||
/**
|
||||
* The Presence Verifier implementation.
|
||||
*
|
||||
* @var \Illuminate\Validation\PresenceVerifierInterface
|
||||
*/
|
||||
protected $verifier;
|
||||
|
||||
/**
|
||||
* The IoC container instance.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Container\Container
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* All of the custom validator extensions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $extensions = array();
|
||||
|
||||
/**
|
||||
* All of the custom implicit validator extensions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $implicitExtensions = array();
|
||||
|
||||
/**
|
||||
* All of the custom validator message replacers.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $replacers = array();
|
||||
|
||||
/**
|
||||
* All of the fallback messages for custom rules.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fallbackMessages = array();
|
||||
|
||||
/**
|
||||
* The Validator resolver instance.
|
||||
*
|
||||
* @var Closure
|
||||
*/
|
||||
protected $resolver;
|
||||
|
||||
/**
|
||||
* Create a new Validator factory instance.
|
||||
*
|
||||
* @param \Symfony\Component\Translation\TranslatorInterface $translator
|
||||
* @param \Illuminate\Contracts\Container\Container $container
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(TranslatorInterface $translator, Container $container = null)
|
||||
{
|
||||
$this->container = $container;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Validator instance.
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $rules
|
||||
* @param array $messages
|
||||
* @param array $customAttributes
|
||||
* @return \Illuminate\Validation\Validator
|
||||
*/
|
||||
public function make(array $data, array $rules, array $messages = array(), array $customAttributes = array())
|
||||
{
|
||||
// The presence verifier is responsible for checking the unique and exists data
|
||||
// for the validator. It is behind an interface so that multiple versions of
|
||||
// it may be written besides database. We'll inject it into the validator.
|
||||
$validator = $this->resolve($data, $rules, $messages, $customAttributes);
|
||||
|
||||
if ( ! is_null($this->verifier))
|
||||
{
|
||||
$validator->setPresenceVerifier($this->verifier);
|
||||
}
|
||||
|
||||
// Next we'll set the IoC container instance of the validator, which is used to
|
||||
// resolve out class based validator extensions. If it is not set then these
|
||||
// types of extensions will not be possible on these validation instances.
|
||||
if ( ! is_null($this->container))
|
||||
{
|
||||
$validator->setContainer($this->container);
|
||||
}
|
||||
|
||||
$this->addExtensions($validator);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the extensions to a validator instance.
|
||||
*
|
||||
* @param \Illuminate\Validation\Validator $validator
|
||||
* @return void
|
||||
*/
|
||||
protected function addExtensions(Validator $validator)
|
||||
{
|
||||
$validator->addExtensions($this->extensions);
|
||||
|
||||
// Next, we will add the implicit extensions, which are similar to the required
|
||||
// and accepted rule in that they are run even if the attributes is not in a
|
||||
// array of data that is given to a validator instances via instantiation.
|
||||
$implicit = $this->implicitExtensions;
|
||||
|
||||
$validator->addImplicitExtensions($implicit);
|
||||
|
||||
$validator->addReplacers($this->replacers);
|
||||
|
||||
$validator->setFallbackMessages($this->fallbackMessages);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a new Validator instance.
|
||||
*
|
||||
* @param array $data
|
||||
* @param array $rules
|
||||
* @param array $messages
|
||||
* @param array $customAttributes
|
||||
* @return \Illuminate\Validation\Validator
|
||||
*/
|
||||
protected function resolve(array $data, array $rules, array $messages, array $customAttributes)
|
||||
{
|
||||
if (is_null($this->resolver))
|
||||
{
|
||||
return new Validator($this->translator, $data, $rules, $messages, $customAttributes);
|
||||
}
|
||||
|
||||
return call_user_func($this->resolver, $this->translator, $data, $rules, $messages, $customAttributes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom validator extension.
|
||||
*
|
||||
* @param string $rule
|
||||
* @param \Closure|string $extension
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
public function extend($rule, $extension, $message = null)
|
||||
{
|
||||
$this->extensions[$rule] = $extension;
|
||||
|
||||
if ($message) $this->fallbackMessages[snake_case($rule)] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom implicit validator extension.
|
||||
*
|
||||
* @param string $rule
|
||||
* @param \Closure|string $extension
|
||||
* @param string $message
|
||||
* @return void
|
||||
*/
|
||||
public function extendImplicit($rule, $extension, $message = null)
|
||||
{
|
||||
$this->implicitExtensions[$rule] = $extension;
|
||||
|
||||
if ($message) $this->fallbackMessages[snake_case($rule)] = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a custom implicit validator message replacer.
|
||||
*
|
||||
* @param string $rule
|
||||
* @param \Closure|string $replacer
|
||||
* @return void
|
||||
*/
|
||||
public function replacer($rule, $replacer)
|
||||
{
|
||||
$this->replacers[$rule] = $replacer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Validator instance resolver.
|
||||
*
|
||||
* @param \Closure $resolver
|
||||
* @return void
|
||||
*/
|
||||
public function resolver(Closure $resolver)
|
||||
{
|
||||
$this->resolver = $resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Translator implementation.
|
||||
*
|
||||
* @return \Symfony\Component\Translation\TranslatorInterface
|
||||
*/
|
||||
public function getTranslator()
|
||||
{
|
||||
return $this->translator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Presence Verifier implementation.
|
||||
*
|
||||
* @return \Illuminate\Validation\PresenceVerifierInterface
|
||||
*/
|
||||
public function getPresenceVerifier()
|
||||
{
|
||||
return $this->verifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Presence Verifier implementation.
|
||||
*
|
||||
* @param \Illuminate\Validation\PresenceVerifierInterface $presenceVerifier
|
||||
* @return void
|
||||
*/
|
||||
public function setPresenceVerifier(PresenceVerifierInterface $presenceVerifier)
|
||||
{
|
||||
$this->verifier = $presenceVerifier;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php namespace Illuminate\Validation;
|
||||
|
||||
interface PresenceVerifierInterface {
|
||||
|
||||
/**
|
||||
* Count the number of objects in a collection having the given value.
|
||||
*
|
||||
* @param string $collection
|
||||
* @param string $column
|
||||
* @param string $value
|
||||
* @param int $excludeId
|
||||
* @param string $idColumn
|
||||
* @param array $extra
|
||||
* @return int
|
||||
*/
|
||||
public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = array());
|
||||
|
||||
/**
|
||||
* Count the number of objects in a collection with the given values.
|
||||
*
|
||||
* @param string $collection
|
||||
* @param string $column
|
||||
* @param array $values
|
||||
* @param array $extra
|
||||
* @return int
|
||||
*/
|
||||
public function getMultiCount($collection, $column, array $values, array $extra = array());
|
||||
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
<?php namespace Illuminate\Validation;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationException;
|
||||
use Illuminate\Contracts\Validation\UnauthorizedException;
|
||||
|
||||
/**
|
||||
* Provides default implementation of ValidatesWhenResolved contract.
|
||||
*/
|
||||
trait ValidatesWhenResolvedTrait {
|
||||
|
||||
/**
|
||||
* Validate the class instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function validate()
|
||||
{
|
||||
$instance = $this->getValidatorInstance();
|
||||
|
||||
if ( ! $this->passesAuthorization())
|
||||
{
|
||||
$this->failedAuthorization();
|
||||
}
|
||||
elseif ( ! $instance->passes())
|
||||
{
|
||||
$this->failedValidation($instance);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validator instance for the request.
|
||||
*
|
||||
* @return \Illuminate\Validation\Validator
|
||||
*/
|
||||
protected function getValidatorInstance()
|
||||
{
|
||||
return $this->validator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a failed validation attempt.
|
||||
*
|
||||
* @param \Illuminate\Validation\Validator $validator
|
||||
* @return mixed
|
||||
*/
|
||||
protected function failedValidation(Validator $validator)
|
||||
{
|
||||
throw new ValidationException($validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deteremine if the request passes the authorization check.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function passesAuthorization()
|
||||
{
|
||||
if (method_exists($this, 'authorize'))
|
||||
{
|
||||
return $this->authorize();
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a failed authorization attempt.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function failedAuthorization()
|
||||
{
|
||||
throw new UnauthorizedException;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
<?php namespace Illuminate\Validation;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Contracts\Validation\ValidatesWhenResolved;
|
||||
|
||||
class ValidationServiceProvider extends ServiceProvider {
|
||||
|
||||
/**
|
||||
* Register the service provider.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->registerValidationResolverHook();
|
||||
|
||||
$this->registerPresenceVerifier();
|
||||
|
||||
$this->registerValidationFactory();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the "ValidatesWhenResolved" container hook.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerValidationResolverHook()
|
||||
{
|
||||
$this->app->afterResolving(function(ValidatesWhenResolved $resolved)
|
||||
{
|
||||
$resolved->validate();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the validation factory.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerValidationFactory()
|
||||
{
|
||||
$this->app->singleton('validator', function($app)
|
||||
{
|
||||
$validator = new Factory($app['translator'], $app);
|
||||
|
||||
// The validation presence verifier is responsible for determining the existence
|
||||
// of values in a given data collection, typically a relational database or
|
||||
// other persistent data stores. And it is used to check for uniqueness.
|
||||
if (isset($app['validation.presence']))
|
||||
{
|
||||
$validator->setPresenceVerifier($app['validation.presence']);
|
||||
}
|
||||
|
||||
return $validator;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the database presence verifier.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerPresenceVerifier()
|
||||
{
|
||||
$this->app->singleton('validation.presence', function($app)
|
||||
{
|
||||
return new DatabasePresenceVerifier($app['db']);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,38 +0,0 @@
|
||||
{
|
||||
"name": "illuminate/validation",
|
||||
"description": "The Illuminate Validation package.",
|
||||
"license": "MIT",
|
||||
"homepage": "http://laravel.com",
|
||||
"support": {
|
||||
"issues": "https://github.com/laravel/framework/issues",
|
||||
"source": "https://github.com/laravel/framework"
|
||||
},
|
||||
"authors": [
|
||||
{
|
||||
"name": "Taylor Otwell",
|
||||
"email": "taylorotwell@gmail.com"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"php": ">=5.4.0",
|
||||
"illuminate/container": "5.0.*",
|
||||
"illuminate/contracts": "5.0.*",
|
||||
"illuminate/support": "5.0.*",
|
||||
"symfony/http-foundation": "2.6.*",
|
||||
"symfony/translation": "2.6.*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Illuminate\\Validation\\": ""
|
||||
}
|
||||
},
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "5.0-dev"
|
||||
}
|
||||
},
|
||||
"suggest": {
|
||||
"illuminate/database": "Required to use the database presence verifier (5.0.*)."
|
||||
},
|
||||
"minimum-stability": "dev"
|
||||
}
|
||||
Reference in New Issue
Block a user