|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheSupportGroup\Common\ValidationAdaptor; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use TheSupportGroup\Common\ValidationInterop\ValidationProviderInterface; |
|
7
|
|
|
|
|
8
|
|
|
class ValidationAdaptor implements validationProviderInterface |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* Holds the rule name in operation. |
|
12
|
|
|
*/ |
|
13
|
|
|
private $ruleName; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Holds the arguments for the rule. |
|
17
|
|
|
*/ |
|
18
|
|
|
private $arguments; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Validator object. |
|
22
|
|
|
*/ |
|
23
|
|
|
private $validator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Validation mapping. |
|
27
|
|
|
*/ |
|
28
|
|
|
private static $validatorMapping = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Set the validator object. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function __construct($validator) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->validator = $validator; |
|
36
|
|
|
|
|
37
|
|
|
// Import mapping only once. |
|
38
|
|
|
if (!self::$validatorMapping) { |
|
39
|
|
|
self::$validatorMapping = require __DIR__ . '/Mapping/Mapping.php'; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param string $ruleName The rule name. |
|
45
|
|
|
* @param array $arguments The arguments for the rule. |
|
46
|
|
|
*/ |
|
47
|
|
|
public function rule($ruleName, array $arguments = []) |
|
48
|
|
|
{ |
|
49
|
|
|
$this->ruleName = $ruleName; |
|
50
|
|
|
$this->arguments = $arguments; |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param mixed $value |
|
57
|
|
|
*/ |
|
58
|
|
|
public function validate($value) |
|
59
|
|
|
{ |
|
60
|
|
|
$rule = $this->validator->buildRule($this->ruleName, $this->arguments); |
|
61
|
|
|
|
|
62
|
|
|
// If validate returns a positive result it means all is well. |
|
63
|
|
|
if ($rule->validate($value)) { |
|
64
|
|
|
return true; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get mapped method. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $method The validation method to map. |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
|
|
public function getMappedMethod($method) |
|
78
|
|
|
{ |
|
79
|
|
|
// Check if the method called is provided in the mapping. |
|
80
|
|
|
if (!array_key_exists($method, self::$validatorMapping)) { |
|
81
|
|
|
throw new Exception(sprintf( |
|
82
|
|
|
'Mapping for method "%s" not found, make sure it exists in the mapping file.', |
|
83
|
|
|
$method |
|
84
|
|
|
)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return self::$validatorMapping[$method]; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|