update inbox list
This commit is contained in:
250
vendor/prettus/laravel-validation/src/Prettus/Validator/AbstractValidator.php
vendored
Normal file
250
vendor/prettus/laravel-validation/src/Prettus/Validator/AbstractValidator.php
vendored
Normal file
@@ -0,0 +1,250 @@
|
||||
<?php namespace Prettus\Validator;
|
||||
|
||||
use Illuminate\Support\MessageBag;
|
||||
use Prettus\Validator\Contracts\ValidatorInterface;
|
||||
use Prettus\Validator\Exceptions\ValidatorException;
|
||||
|
||||
/**
|
||||
* Class AbstractValidator
|
||||
* @package Prettus\Validator
|
||||
* @author Anderson Andrade <contato@andersonandra.de>
|
||||
*/
|
||||
abstract class AbstractValidator implements ValidatorInterface
|
||||
{
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
protected $id = null;
|
||||
|
||||
/**
|
||||
* Validator
|
||||
*
|
||||
* @var object
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* Data to be validated
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $data = array();
|
||||
|
||||
/**
|
||||
* Validation Rules
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rules = array();
|
||||
|
||||
/**
|
||||
* Validation Custom Messages
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $messages = array();
|
||||
|
||||
/**
|
||||
* Validation Custom Attributes
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $attributes = array();
|
||||
|
||||
/**
|
||||
* Validation errors
|
||||
*
|
||||
* @var MessageBag
|
||||
*/
|
||||
protected $errors = array();
|
||||
|
||||
|
||||
/**
|
||||
* Set Id
|
||||
*
|
||||
* @param $id
|
||||
* @return $this
|
||||
*/
|
||||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set data to validate
|
||||
*
|
||||
* @param array $data
|
||||
* @return $this
|
||||
*/
|
||||
public function with(array $data)
|
||||
{
|
||||
$this->data = $data;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return errors
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function errors()
|
||||
{
|
||||
return $this->errorsBag()->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Errors
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function errorsBag()
|
||||
{
|
||||
return $this->errors;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass the data and the rules to the validator
|
||||
*
|
||||
* @param string $action
|
||||
* @return boolean
|
||||
*/
|
||||
abstract public function passes($action = null);
|
||||
|
||||
/**
|
||||
* Pass the data and the rules to the validator or throws ValidatorException
|
||||
*
|
||||
* @throws ValidatorException
|
||||
* @param string $action
|
||||
* @return boolean
|
||||
*/
|
||||
public function passesOrFail($action = null)
|
||||
{
|
||||
if (!$this->passes($action)) {
|
||||
throw new ValidatorException($this->errorsBag());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE
|
||||
*
|
||||
* Default rule: ValidatorInterface::RULE_CREATE
|
||||
*
|
||||
* @param null $action
|
||||
* @return array
|
||||
*/
|
||||
public function getRules($action = null)
|
||||
{
|
||||
$rules = $this->rules;
|
||||
|
||||
if (isset($this->rules[$action])) {
|
||||
$rules = $this->rules[$action];
|
||||
}
|
||||
|
||||
return $this->parserValidationRules($rules, $this->id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Rules for Validation
|
||||
*
|
||||
* @param array $rules
|
||||
* @return $this
|
||||
*/
|
||||
public function setRules(array $rules)
|
||||
{
|
||||
$this->rules = $rules;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Custom error messages for validation
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getMessages()
|
||||
{
|
||||
return $this->messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Custom error messages for Validation
|
||||
*
|
||||
* @param array $messages
|
||||
* @return $this
|
||||
*/
|
||||
public function setMessages(array $messages)
|
||||
{
|
||||
$this->messages = $messages;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Custom error attributes for validation
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getAttributes()
|
||||
{
|
||||
return $this->attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Custom error attributes for Validation
|
||||
*
|
||||
* @param array $attributes
|
||||
* @return $this
|
||||
*/
|
||||
public function setAttributes(array $attributes)
|
||||
{
|
||||
$this->attributes = $attributes;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parser Validation Rules
|
||||
*
|
||||
* @param $rules
|
||||
* @param null $id
|
||||
* @return array
|
||||
*/
|
||||
protected function parserValidationRules($rules, $id = null)
|
||||
{
|
||||
if (null === $id) {
|
||||
return $rules;
|
||||
}
|
||||
|
||||
array_walk($rules, function (&$rules, $field) use ($id) {
|
||||
if (!is_array($rules)) {
|
||||
$rules = explode("|", $rules);
|
||||
}
|
||||
|
||||
foreach ($rules as $ruleIdx => $rule) {
|
||||
// get name and parameters
|
||||
@list($name, $params) = array_pad(explode(":", $rule), 2, null);
|
||||
|
||||
// only do someting for the unique rule
|
||||
if (strtolower($name) != "unique") {
|
||||
continue; // continue in foreach loop, nothing left to do here
|
||||
}
|
||||
|
||||
$p = array_map("trim", explode(",", $params));
|
||||
|
||||
// set field name to rules key ($field) (laravel convention)
|
||||
if (!isset($p[1])) {
|
||||
$p[1] = $field;
|
||||
}
|
||||
|
||||
// set 3rd parameter to id given to getValidationRules()
|
||||
$p[2] = $id;
|
||||
|
||||
$params = implode(",", $p);
|
||||
$rules[$ruleIdx] = $name.":".$params;
|
||||
}
|
||||
});
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
80
vendor/prettus/laravel-validation/src/Prettus/Validator/Contracts/ValidatorInterface.php
vendored
Normal file
80
vendor/prettus/laravel-validation/src/Prettus/Validator/Contracts/ValidatorInterface.php
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php namespace Prettus\Validator\Contracts;
|
||||
|
||||
use Illuminate\Contracts\Support\MessageBag;
|
||||
use Prettus\Validator\Exceptions\ValidatorException;
|
||||
|
||||
/**
|
||||
* Interface ValidatorInterface
|
||||
* @package Prettus\Validator\Contracts
|
||||
* @author Anderson Andrade <contato@andersonandra.de>
|
||||
*/
|
||||
interface ValidatorInterface
|
||||
{
|
||||
const RULE_CREATE = 'create';
|
||||
const RULE_UPDATE = 'update';
|
||||
|
||||
/**
|
||||
* Set Id
|
||||
*
|
||||
* @param $id
|
||||
* @return $this
|
||||
*/
|
||||
public function setId($id);
|
||||
|
||||
/**
|
||||
* With
|
||||
*
|
||||
* @param array
|
||||
* @return $this
|
||||
*/
|
||||
public function with(array $input);
|
||||
|
||||
/**
|
||||
* Pass the data and the rules to the validator
|
||||
*
|
||||
* @param string $action
|
||||
* @return boolean
|
||||
*/
|
||||
public function passes($action = null);
|
||||
|
||||
/**
|
||||
* Pass the data and the rules to the validator or throws ValidatorException
|
||||
*
|
||||
* @throws ValidatorException
|
||||
* @param string $action
|
||||
* @return boolean
|
||||
*/
|
||||
public function passesOrFail($action = null);
|
||||
|
||||
/**
|
||||
* Errors
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function errors();
|
||||
|
||||
/**
|
||||
* Errors
|
||||
*
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function errorsBag();
|
||||
|
||||
/**
|
||||
* Set Rules for Validation
|
||||
*
|
||||
* @param array $rules
|
||||
* @return $this
|
||||
*/
|
||||
public function setRules(array $rules);
|
||||
|
||||
/**
|
||||
* Get rule for validation by action ValidatorInterface::RULE_CREATE or ValidatorInterface::RULE_UPDATE
|
||||
*
|
||||
* Default rule: ValidatorInterface::RULE_CREATE
|
||||
*
|
||||
* @param $action
|
||||
* @return array
|
||||
*/
|
||||
public function getRules($action = null);
|
||||
}
|
||||
58
vendor/prettus/laravel-validation/src/Prettus/Validator/Exceptions/ValidatorException.php
vendored
Normal file
58
vendor/prettus/laravel-validation/src/Prettus/Validator/Exceptions/ValidatorException.php
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php namespace Prettus\Validator\Exceptions;
|
||||
|
||||
use Illuminate\Contracts\Support\Arrayable;
|
||||
use Illuminate\Contracts\Support\Jsonable;
|
||||
use Illuminate\Support\MessageBag;
|
||||
|
||||
/**
|
||||
* Class ValidatorException
|
||||
* @package Prettus\Validator\Exceptions
|
||||
* @author Anderson Andrade <contato@andersonandra.de>
|
||||
*/
|
||||
class ValidatorException extends \Exception implements Jsonable, Arrayable
|
||||
{
|
||||
/**
|
||||
* @var MessageBag
|
||||
*/
|
||||
protected $messageBag;
|
||||
|
||||
/**
|
||||
* @param MessageBag $messageBag
|
||||
*/
|
||||
public function __construct(MessageBag $messageBag)
|
||||
{
|
||||
$this->messageBag = $messageBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MessageBag
|
||||
*/
|
||||
public function getMessageBag()
|
||||
{
|
||||
return $this->messageBag;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the instance as an array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function toArray()
|
||||
{
|
||||
return [
|
||||
'error'=>'validation_exception',
|
||||
'error_description'=>$this->getMessageBag()
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the object to its JSON representation.
|
||||
*
|
||||
* @param int $options
|
||||
* @return string
|
||||
*/
|
||||
public function toJson($options = 0)
|
||||
{
|
||||
return json_encode($this->toArray(), $options);
|
||||
}
|
||||
}
|
||||
49
vendor/prettus/laravel-validation/src/Prettus/Validator/LaravelValidator.php
vendored
Normal file
49
vendor/prettus/laravel-validation/src/Prettus/Validator/LaravelValidator.php
vendored
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php namespace Prettus\Validator;
|
||||
|
||||
use Illuminate\Contracts\Validation\Factory;
|
||||
|
||||
/**
|
||||
* Class LaravelValidator
|
||||
* @package Prettus\Validator
|
||||
* @author Anderson Andrade <contato@andersonandra.de>
|
||||
*/
|
||||
class LaravelValidator extends AbstractValidator
|
||||
{
|
||||
/**
|
||||
* Validator
|
||||
*
|
||||
* @var \Illuminate\Validation\Factory
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* Construct
|
||||
*
|
||||
* @param \Illuminate\Contracts\Validation\Factory $validator
|
||||
*/
|
||||
public function __construct(Factory $validator)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass the data and the rules to the validator
|
||||
*
|
||||
* @param string $action
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($action = null)
|
||||
{
|
||||
$rules = $this->getRules($action);
|
||||
$messages = $this->getMessages();
|
||||
$attributes = $this->getAttributes();
|
||||
$validator = $this->validator->make($this->data, $rules, $messages, $attributes);
|
||||
|
||||
if ($validator->fails()) {
|
||||
$this->errors = $validator->messages();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user