-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTester.php
More file actions
163 lines (147 loc) · 3.9 KB
/
Copy pathTester.php
File metadata and controls
163 lines (147 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* Contrôle de variables
*
* @author Adrien <aimbert@solire.fr>
* @license MIT http://mit-license.org/
*/
namespace Solire\Form;
/**
* Contrôle de variables
*
* @author Adrien <aimbert@solire.fr>
* @license MIT http://mit-license.org/
*/
class Tester
{
/**
* Variable
*
* @var mixed
*/
private $foo = null;
/**
* Charge une nouvelle variable
*
* @param mixed $param Valeur de la variable à tester
*/
public function __construct($param = null)
{
$this->foo = $param;
}
/**
* Retourne la valeur du paramètre.
*
* @return mixed
*/
public function get()
{
return $this->foo;
}
/**
* Renvoie le nom de la classe de test
*
* @param string $name Nom du test
* @param string $tool Nom du modèle de script à valider (sanitize, validate...)
*
* @return string
*/
protected function getClassName($name, $tool)
{
$className = __NAMESPACE__ . '\\Process\\' . ucfirst($name);
if (class_exists($className)) {
return $this->validateProcess($className, $tool);
}
if (class_exists($name)) {
return $this->validateProcess($name, $tool);
}
throw new Exception('Aucune classe trouvée pour __' . $name . '__');
}
/**
* Contrôle si la classe demandé implémente l'interface du type de script
* demandé
*
* @param string $className Nom de la classe
* @param string $tool Nom du type de script
*
* @return string
* @throws Exception si la classe demandée n'implémente pas l'interface
*/
protected function validateProcess($className, $tool)
{
$interfaces = class_implements($className);
if (!isset($interfaces[__NAMESPACE__ . '\\' . $tool . 'Interface'])) {
throw new Exception('_' . $className . '_ n\'implemente pas ' . $tool . 'Interface');
}
return $className;
}
/**
* Extrait les options du nom du traitement
*
* @param string $name Nom du traitement
*
* @return string[]
*/
protected function extractOptions($name)
{
if (strpos($name, ':') === false) {
return [$name, null];
}
$foo = explode(':', $name);
return $foo;
}
/**
* Execute un test sur la variable
*
* @param string $option Nom du test à effectuer
*
* @return boolean
*/
public function validate($option)
{
list($option, $param) = $this->extractOptions($option);
$className = $this->getClassName($option, 'Validate');
return $className::validate($this->foo, $param);
}
/**
* Execute un traitement sur la variable
*
* @param string $option Nom du traitement à effectuer
*
* @return boolean
*/
public function sanitize($option)
{
list($option, $param) = $this->extractOptions($option);
$className = $this->getClassName($option, 'Sanitize');
$this->foo = $className::sanitize($this->foo, $param);
return $this->foo;
}
/**
* Permet d'effectuer differents tests sur la variable
*
* @param array $tests Tableau de tests à effectuer
* @param array $sanitizes Tableau de nettoyages à effectuer
*
* @return boolean
*/
public function run($tests, $sanitizes = [])
{
if (!is_array($tests)) {
throw new Exception('$tests doit être un tableau');
}
foreach ($tests as $option) {
if ($this->validate($option) === true) {
continue;
}
return false;
}
if (!is_array($sanitizes)) {
throw new Exception('$sanitizes doit être un tableau');
}
foreach ($sanitizes as $option) {
$this->sanitize($option);
}
return true;
}
}