1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File copied from Waavi/Sanitizer https://github.com/waavi/sanitizer |
4
|
|
|
* Sanitization functionality to be customized within this project before a 1.0 release. |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace PerfectOblivion\Valid\Sanitizer\Laravel; |
8
|
|
|
|
9
|
|
|
use Closure; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
use PerfectOblivion\Valid\Sanitizer\Sanitizer; |
12
|
|
|
use PerfectOblivion\Valid\Sanitizer\Contracts\Filter; |
13
|
|
|
|
14
|
|
|
class Factory |
15
|
|
|
{ |
16
|
|
|
/** @var array */ |
17
|
|
|
protected $customFilters; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a new Sanitizer factory instance. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
$this->customFilters = []; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create a new Sanitizer instance. |
29
|
|
|
* |
30
|
|
|
* @param array $data Data to be sanitized |
31
|
|
|
* @param array $rules Filters to be applied to the given data |
32
|
|
|
* |
33
|
|
|
* @return Sanitizer |
34
|
|
|
*/ |
35
|
|
|
public function make(array $data, array $rules) |
36
|
|
|
{ |
37
|
|
|
$sanitizer = new Sanitizer($data, $rules, $this->customFilters); |
38
|
|
|
|
39
|
|
|
return $sanitizer; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Add a custom filters to all Sanitizers created with this Factory. |
44
|
|
|
* |
45
|
|
|
* @param string $name Name of the filter |
46
|
|
|
* @param mixed $extension Either the full class name of a Filter class implementing the Filter contract, or a Closure. |
|
|
|
|
47
|
|
|
* |
48
|
|
|
* @throws InvalidArgumentException |
49
|
|
|
*/ |
50
|
|
|
public function extend($name, $customFilter) |
51
|
|
|
{ |
52
|
|
|
if (empty($name) || ! is_string($name)) { |
53
|
|
|
throw new InvalidArgumentException('The Sanitizer filter name must be a non-empty string.'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (! ($customFilter instanceof Closure) && ! in_array(Filter::class, class_implements($customFilter))) { |
57
|
|
|
throw new InvalidArgumentException('Custom filter must be a Closure or a class implementing the PerfectOblivion\Valid\Sanitizer\Contracts\Filter interface.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->customFilters[$name] = $customFilter; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.