-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for Closure rules (#45)
* Support Closure rules * Test Closure rules * Bug fix * Fix test * Fix test
- Loading branch information
1 parent
1f0b7c9
commit 73675ac
Showing
2 changed files
with
52 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
use Closure; | ||
use Illuminate\Support\Arr; | ||
use Illuminate\Validation\ValidationRuleParser; | ||
use Illuminate\Validation\ClosureValidationRule; | ||
use InvalidArgumentException; | ||
|
||
class Sanitizer | ||
|
@@ -49,7 +50,7 @@ class Sanitizer | |
public function __construct(array $data, array $rules, array $customFilters = []) | ||
{ | ||
$this->data = $data; | ||
$this->rules = $this->parseRulesArray($rules); | ||
$this->rules = $this->parseRules($rules); | ||
$this->filters = array_merge($this->filters, $customFilters); | ||
} | ||
|
||
|
@@ -59,15 +60,15 @@ public function __construct(array $data, array $rules, array $customFilters = [] | |
* @param array $rules | ||
* @return array | ||
*/ | ||
protected function parseRulesArray(array $rules) | ||
protected function parseRules(array $rules) | ||
{ | ||
$parsedRules = []; | ||
|
||
$rawRules = (new ValidationRuleParser($this->data))->explode($rules); | ||
|
||
foreach ($rawRules->rules as $attribute => $attributeRules) { | ||
foreach ($attributeRules as $attributeRule) { | ||
$parsedRule = $this->parseRuleString($attributeRule); | ||
$parsedRule = $this->parseRule($attributeRule); | ||
if ($parsedRule) { | ||
$parsedRules[$attribute][] = $parsedRule; | ||
} | ||
|
@@ -77,6 +78,23 @@ protected function parseRulesArray(array $rules) | |
return $parsedRules; | ||
} | ||
|
||
/** | ||
* Parse a rule. | ||
* | ||
* @param string|Closure $rule | ||
* @return array|Closure | ||
*/ | ||
protected function parseRule($rule) | ||
{ | ||
if (is_string($rule)) { | ||
return $this->parseRuleString($rule); | ||
} elseif ($rule instanceof ClosureValidationRule) { | ||
return $rule->callback; | ||
} else { | ||
throw new InvalidArgumentException("Unsupported rule type."); | ||
} | ||
} | ||
|
||
/** | ||
* Parse a rule string formatted as filterName:option1, option2 into an array formatted as [name => filterName, options => [option1, option2]] | ||
* | ||
|
@@ -100,27 +118,36 @@ protected function parseRuleString($rule) | |
|
||
/** | ||
* Apply the given filter by its name | ||
* @param $name | ||
* | ||
* @param string|Closure $rule | ||
* @return Filter | ||
*/ | ||
protected function applyFilter($name, $value, $options = []) | ||
protected function applyFilter($rule, $value) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
marcosmartinezeu
|
||
{ | ||
if ($rule instanceof Closure) { | ||
return call_user_func($rule, $value); | ||
} | ||
|
||
$name = $rule['name']; | ||
$options = $rule['options']; | ||
|
||
// If the filter does not exist, throw an Exception: | ||
if (!isset($this->filters[$name])) { | ||
throw new InvalidArgumentException("No filter found by the name of $name"); | ||
} | ||
|
||
$filter = $this->filters[$name]; | ||
|
||
if ($filter instanceof Closure) { | ||
return call_user_func_array($filter, [$value, $options]); | ||
} else { | ||
This comment has been minimized.
Sorry, something went wrong. |
||
$filter = new $filter; | ||
return $filter->apply($value, $options); | ||
return (new $filter)->apply($value, $options); | ||
} | ||
} | ||
|
||
/** | ||
* Sanitize the given data | ||
* | ||
* @return array | ||
*/ | ||
public function sanitize() | ||
|
@@ -151,31 +178,4 @@ public function sanitize() | |
|
||
return $sanitized; | ||
} | ||
|
||
/** | ||
* Sanitize the given attribute | ||
* | ||
* @param string $attribute Attribute name | ||
* @param mixed $value Attribute value | ||
* @return mixed Sanitized value | ||
*/ | ||
protected function sanitizeAttribute($attribute, $value) | ||
{ | ||
if (isset($this->rules[$attribute])) { | ||
$sanitize = true; | ||
$original = $value; | ||
foreach ($this->rules[$attribute] as $rule) { | ||
if ($rule['name'] === 'filter_if') { | ||
$sanitize = $this->applyFilter($rule['name'], $value, $rule['options']); | ||
} else { | ||
$value = $this->applyFilter($rule['name'], $value, $rule['options']); | ||
} | ||
} | ||
|
||
if (!$sanitize) { | ||
return $original; | ||
} | ||
} | ||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Inconsistent change here: The signature of the function is
string|Closure $rule
but then$rule
is being treated as an array.