1
|
|
|
<?php namespace Arcanedev\Sanitizer; |
2
|
|
|
|
3
|
|
|
use Arcanedev\Sanitizer\Contracts\Filterable; |
4
|
|
|
use Closure; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class Factory |
8
|
|
|
* |
9
|
|
|
* @package Arcanedev\Sanitizer |
10
|
|
|
* @author ARCANEDEV <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class Factory |
13
|
|
|
{ |
14
|
|
|
/* ------------------------------------------------------------------------------------------------ |
15
|
|
|
| Properties |
16
|
|
|
| ------------------------------------------------------------------------------------------------ |
17
|
|
|
*/ |
18
|
|
|
/** |
19
|
|
|
* List of custom filters |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
protected $filters = []; |
24
|
|
|
|
25
|
|
|
/* ------------------------------------------------------------------------------------------------ |
26
|
|
|
| Constructor |
27
|
|
|
| ------------------------------------------------------------------------------------------------ |
28
|
|
|
*/ |
29
|
|
|
/** |
30
|
|
|
* Create a new instance. |
31
|
|
|
* |
32
|
|
|
* @param array $filters |
33
|
|
|
*/ |
34
|
90 |
|
public function __construct(array $filters = []) |
35
|
|
|
{ |
36
|
90 |
|
$this->filters = $filters; |
37
|
90 |
|
} |
38
|
|
|
|
39
|
|
|
/* ------------------------------------------------------------------------------------------------ |
40
|
|
|
| Main Functions |
41
|
|
|
| ------------------------------------------------------------------------------------------------ |
42
|
|
|
*/ |
43
|
|
|
/** |
44
|
|
|
* Make a sanitizer |
45
|
|
|
* |
46
|
|
|
* @param array $data |
47
|
|
|
* @param array $rules |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
36 |
|
public function make(array $data, array $rules) |
52
|
|
|
{ |
53
|
36 |
|
return (new Sanitizer($this->filters))->sanitize($data, $rules); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Add a custom filters to all Sanitizers created with this Factory. |
58
|
|
|
* |
59
|
|
|
* @param string $name |
60
|
|
|
* @param \Closure|Filterable|string $filter |
61
|
|
|
* |
62
|
|
|
* @throws \Arcanedev\Sanitizer\Exceptions\InvalidFilterException |
63
|
|
|
*/ |
64
|
54 |
|
public function extend($name, $filter) |
65
|
|
|
{ |
66
|
54 |
|
$this->checkName($name); |
67
|
|
|
|
68
|
36 |
|
if ( ! $filter instanceof Closure) { |
69
|
27 |
|
$this->isFilterable($filter); |
70
|
3 |
|
} |
71
|
|
|
|
72
|
18 |
|
$this->filters[$name] = $filter; |
73
|
18 |
|
} |
74
|
|
|
|
75
|
|
|
/* ------------------------------------------------------------------------------------------------ |
76
|
|
|
| Check Functions |
77
|
|
|
| ------------------------------------------------------------------------------------------------ |
78
|
|
|
*/ |
79
|
|
|
/** |
80
|
|
|
* Check the filter name. |
81
|
|
|
* |
82
|
|
|
* @param string $name |
83
|
|
|
* |
84
|
|
|
* @throws \Arcanedev\Sanitizer\Exceptions\InvalidFilterException |
85
|
|
|
*/ |
86
|
54 |
|
private function checkName($name) |
87
|
|
|
{ |
88
|
54 |
|
if (empty($name) || ! is_string($name)) { |
89
|
18 |
|
throw new Exceptions\InvalidFilterException( |
90
|
12 |
|
'The Sanitizer filter name must be a non empty string.' |
91
|
6 |
|
); |
92
|
|
|
} |
93
|
36 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if filter is filterable. |
97
|
|
|
* |
98
|
|
|
* @param mixed $filter |
99
|
|
|
* |
100
|
|
|
* @throws \Arcanedev\Sanitizer\Exceptions\InvalidFilterException |
101
|
|
|
*/ |
102
|
27 |
|
private function isFilterable($filter) |
103
|
|
|
{ |
104
|
27 |
|
if (is_string($filter) && ! class_exists($filter)) { |
105
|
9 |
|
throw new Exceptions\InvalidFilterException( |
106
|
9 |
|
"The [$filter] class does not exits." |
107
|
3 |
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
18 |
|
if ( ! in_array(Filterable::class, class_implements($filter))) { |
111
|
9 |
|
throw new Exceptions\InvalidFilterException( |
112
|
6 |
|
'The filter must be a Closure or a class implementing the Filterable interface.' |
113
|
3 |
|
); |
114
|
|
|
} |
115
|
9 |
|
} |
116
|
|
|
} |
117
|
|
|
|