vendor and env first commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace Proengsoft\JsValidation\Support;
|
||||
|
||||
use Closure;
|
||||
|
||||
trait AccessProtectedTrait
|
||||
{
|
||||
/**
|
||||
* Create closure to call inaccessible method.
|
||||
*
|
||||
* @param $instance
|
||||
* @return \Closure
|
||||
*/
|
||||
protected function createProtectedCaller($instance)
|
||||
{
|
||||
$closure = function ($method, $args) {
|
||||
$callable = [$this, $method];
|
||||
|
||||
return call_user_func_array($callable, $args);
|
||||
};
|
||||
|
||||
return $closure->bindTo($instance, $instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets inaccessible property.
|
||||
*
|
||||
* @param $instance
|
||||
* @param $property
|
||||
* @return \Closure
|
||||
*/
|
||||
protected function getProtected($instance, $property)
|
||||
{
|
||||
$closure = function ($property) {
|
||||
return $this->$property;
|
||||
};
|
||||
$callback = $closure->bindTo($instance, $instance);
|
||||
|
||||
return $callback($property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls inaccessible method.
|
||||
*
|
||||
* @param object|\Closure $instance
|
||||
* @param $method
|
||||
* @param $args
|
||||
* @return mixed
|
||||
*/
|
||||
protected function callProtected($instance, $method, $args = [])
|
||||
{
|
||||
if (! ($instance instanceof Closure)) {
|
||||
$instance = $this->createProtectedCaller($instance);
|
||||
}
|
||||
|
||||
return call_user_func($instance, $method, $args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
|
||||
namespace Proengsoft\JsValidation\Support;
|
||||
|
||||
use Illuminate\Validation\Validator as BaseValidator;
|
||||
|
||||
class DelegatedValidator
|
||||
{
|
||||
use AccessProtectedTrait;
|
||||
|
||||
/**
|
||||
* The Validator resolved instance.
|
||||
*
|
||||
* @var \Illuminate\Validation\Validator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* Validation rule parser instance.
|
||||
*
|
||||
* @var \Proengsoft\JsValidation\Support\ValidationRuleParserProxy
|
||||
*/
|
||||
protected $ruleParser;
|
||||
|
||||
/**
|
||||
* Closure to invoke non accessible Validator methods.
|
||||
*
|
||||
* @var \Closure
|
||||
*/
|
||||
protected $validatorMethod;
|
||||
|
||||
/**
|
||||
* DelegatedValidator constructor.
|
||||
*
|
||||
* @param \Illuminate\Validation\Validator $validator
|
||||
* @param \Proengsoft\JsValidation\Support\ValidationRuleParserProxy $ruleParser
|
||||
*/
|
||||
public function __construct(BaseValidator $validator, ValidationRuleParserProxy $ruleParser)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
$this->ruleParser = $ruleParser;
|
||||
$this->validatorMethod = $this->createProtectedCaller($validator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call validator method.
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $args
|
||||
* @return mixed
|
||||
*/
|
||||
private function callValidator($method, $args = [])
|
||||
{
|
||||
return $this->callProtected($this->validatorMethod, $method, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current \Illuminate\Validation\Validator instance.
|
||||
*
|
||||
* @return \Illuminate\Validation\Validator
|
||||
*/
|
||||
public function getValidator()
|
||||
{
|
||||
return $this->validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the data under validation.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getData()
|
||||
{
|
||||
return $this->validator->getData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the data under validation.
|
||||
*
|
||||
* @param array
|
||||
*/
|
||||
public function setData($data)
|
||||
{
|
||||
$rules = $this->validator->getRules();
|
||||
$this->validator->setData($data);
|
||||
if (is_array($rules)) {
|
||||
$this->validator->setRules($rules);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getRules()
|
||||
{
|
||||
return $this->validator->getRules();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if a given rule implies the attribute is required.
|
||||
*
|
||||
* @param string $rule
|
||||
* @return bool
|
||||
*/
|
||||
public function isImplicit($rule)
|
||||
{
|
||||
return $this->callValidator('isImplicit', [$rule]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace all error message place-holders with actual values.
|
||||
*
|
||||
* @param string $message
|
||||
* @param string $attribute
|
||||
* @param string $rule
|
||||
* @param array $parameters
|
||||
* @return string
|
||||
*/
|
||||
public function makeReplacements($message, $attribute, $rule, $parameters)
|
||||
{
|
||||
if (is_object($rule)) {
|
||||
$rule = get_class($rule);
|
||||
}
|
||||
|
||||
return $this->callValidator('makeReplacements', [$message, $attribute, $rule, $parameters]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given attribute has a rule in the given set.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string|array $rules
|
||||
* @return bool
|
||||
*/
|
||||
public function hasRule($attribute, $rules)
|
||||
{
|
||||
return $this->callValidator('hasRule', [$attribute, $rules]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation message for an attribute and rule.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string $rule
|
||||
* @return string
|
||||
*/
|
||||
public function getMessage($attribute, $rule)
|
||||
{
|
||||
if (is_object($rule)) {
|
||||
$rule = get_class($rule);
|
||||
}
|
||||
|
||||
return $this->callValidator('getMessage', [$attribute, $rule]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the rule name and parameters from a rule.
|
||||
*
|
||||
* @param array|string $rules
|
||||
* @return array
|
||||
*/
|
||||
public function parseRule($rules)
|
||||
{
|
||||
return $this->ruleParser->parse($rules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Explode the rules into an array of rules.
|
||||
*
|
||||
* @param string|array $rules
|
||||
* @return array
|
||||
*/
|
||||
public function explodeRules($rules)
|
||||
{
|
||||
return $this->ruleParser->explodeRules($rules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add conditions to a given field based on a Closure.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param string|array $rules
|
||||
* @param callable $callback
|
||||
* @return void
|
||||
*/
|
||||
public function sometimes($attribute, $rules, callable $callback)
|
||||
{
|
||||
$this->validator->sometimes($attribute, $rules, $callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate method calls to validator instance.
|
||||
*
|
||||
* @param $method
|
||||
* @param $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $params)
|
||||
{
|
||||
$arrCaller = [$this->validator, $method];
|
||||
|
||||
return call_user_func_array($arrCaller, $params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
<?php
|
||||
|
||||
namespace Proengsoft\JsValidation\Support;
|
||||
|
||||
use Proengsoft\JsValidation\Javascript\RuleParser;
|
||||
|
||||
trait RuleListTrait
|
||||
{
|
||||
/**
|
||||
* Rules validated with Javascript.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $clientRules = [
|
||||
'Accepted', 'After', 'AfterOrEqual', 'Alpha', 'AlphaDash',
|
||||
'AlphaNum', 'Array', 'Bail', 'Before', 'BeforeOrEqual', 'Between', 'Boolean', 'Confirmed', 'Date', 'Dimensions',
|
||||
'DateFormat', 'Different', 'Digits', 'DigitsBetween', 'Distinct', 'Email', 'File', 'Filled', 'Image',
|
||||
'In', 'InArray', 'Integer', 'Ip', 'Json', 'Max', 'Mimes', 'Mimetypes', 'Min', 'NotIn', 'Nullable',
|
||||
'Numeric', 'Regex', 'Required', 'RequiredIf', 'RequiredUnless', 'RequiredWith', 'RequiredWithAll',
|
||||
'RequiredWithout', 'RequiredWithoutAll', 'Same', 'Size', 'Sometimes',
|
||||
'String', 'Timezone', 'ProengsoftNoop',
|
||||
];
|
||||
|
||||
/**
|
||||
* Rules validated in Server-Side.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $serverRules = ['ActiveUrl', 'Exists', 'Unique', 'Url'];
|
||||
|
||||
/**
|
||||
* Rules applyed to files.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $fileRules = ['File', 'Image', 'Mimes', 'Mimetypes'];
|
||||
|
||||
/**
|
||||
* Rule used to disable validations.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $disableJsValidationRule = 'NoJsValidation';
|
||||
|
||||
/**
|
||||
* Returns if rule is validated using Javascript.
|
||||
*
|
||||
* @param $rule
|
||||
* @return bool
|
||||
*/
|
||||
protected function isImplemented($rule)
|
||||
{
|
||||
return in_array($rule, $this->clientRules) || in_array($rule, $this->serverRules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if rule must be validated in server-side.
|
||||
*
|
||||
* @param $rule
|
||||
* @return bool
|
||||
*/
|
||||
protected function isRemoteRule($rule)
|
||||
{
|
||||
return in_array($rule, $this->serverRules) ||
|
||||
! in_array($rule, $this->clientRules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Form request rule.
|
||||
*
|
||||
* @param string $rule
|
||||
* @return bool
|
||||
*/
|
||||
protected function isFormRequestRule($rule)
|
||||
{
|
||||
return $rule === RuleParser::FORM_REQUEST_RULE_NAME;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if rule disables rule processing.
|
||||
*
|
||||
* @param $rule
|
||||
* @return bool
|
||||
*/
|
||||
protected function isDisableRule($rule)
|
||||
{
|
||||
return $rule === $this->disableJsValidationRule;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if rules should be validated.
|
||||
*
|
||||
* @param $rules
|
||||
* @return bool
|
||||
*/
|
||||
protected function validationDisabled($rules)
|
||||
{
|
||||
$rules = (array) $rules;
|
||||
|
||||
return in_array($this->disableJsValidationRule, $rules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if rules is for input file type.
|
||||
*
|
||||
* @param $rule
|
||||
* @return bool
|
||||
*/
|
||||
protected function isFileRule($rule)
|
||||
{
|
||||
return in_array($rule, $this->fileRules);
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace Proengsoft\JsValidation\Support;
|
||||
|
||||
trait UseDelegatedValidatorTrait
|
||||
{
|
||||
/**
|
||||
* Delegated validator.
|
||||
*
|
||||
* @var \Proengsoft\JsValidation\Support\DelegatedValidator
|
||||
*/
|
||||
protected $validator;
|
||||
|
||||
/**
|
||||
* Sets delegated Validator instance.
|
||||
*
|
||||
* @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
|
||||
* @return void
|
||||
*/
|
||||
public function setDelegatedValidator(DelegatedValidator $validator)
|
||||
{
|
||||
$this->validator = $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets current DelegatedValidator instance.
|
||||
*
|
||||
* @return \Proengsoft\JsValidation\Support\DelegatedValidator
|
||||
*/
|
||||
public function getDelegatedValidator()
|
||||
{
|
||||
return $this->validator;
|
||||
}
|
||||
}
|
||||
+71
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Proengsoft\JsValidation\Support;
|
||||
|
||||
use Illuminate\Validation\ValidationRuleParser;
|
||||
|
||||
class ValidationRuleParserProxy
|
||||
{
|
||||
use AccessProtectedTrait;
|
||||
|
||||
/**
|
||||
* ValidationRuleParser instance.
|
||||
*
|
||||
* @var ValidationRuleParser
|
||||
*/
|
||||
protected $parser;
|
||||
|
||||
/**
|
||||
* Closure to invoke non accessible Validator methods.
|
||||
*
|
||||
* @var \Closure
|
||||
*/
|
||||
protected $parserMethod;
|
||||
|
||||
/**
|
||||
* ValidationRuleParserProxy constructor.
|
||||
*
|
||||
* @param array $data
|
||||
*/
|
||||
public function __construct($data = [])
|
||||
{
|
||||
$this->parser = new ValidationRuleParser((array) $data);
|
||||
$this->parserMethod = $this->createProtectedCaller($this->parser);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the rule name and parameters from a rule.
|
||||
*
|
||||
* @param array|string $rules
|
||||
* @return array
|
||||
*/
|
||||
public function parse($rules)
|
||||
{
|
||||
return $this->parser->parse($rules);
|
||||
}
|
||||
|
||||
/**
|
||||
* Explode the rules into an array of explicit rules.
|
||||
*
|
||||
* @param array $rules
|
||||
* @return mixed
|
||||
*/
|
||||
public function explodeRules($rules)
|
||||
{
|
||||
return $this->callProtected($this->parserMethod, 'explodeRules', [$rules]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delegate method calls to parser instance.
|
||||
*
|
||||
* @param string $method
|
||||
* @param mixed $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call($method, $params)
|
||||
{
|
||||
$arrCaller = [$this->parser, $method];
|
||||
|
||||
return call_user_func_array($arrCaller, $params);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user