1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Awurth\SlimValidation; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Respect\Validation\Rules\AllOf; |
7
|
|
|
|
8
|
|
|
class Configuration |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
protected $message; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string[] |
17
|
|
|
*/ |
18
|
|
|
protected $messages; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var AllOf |
22
|
|
|
*/ |
23
|
|
|
protected $rules; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param AllOf|array $options |
29
|
|
|
*/ |
30
|
|
|
public function __construct($options) |
31
|
|
|
{ |
32
|
|
|
if ($options instanceof AllOf) { |
33
|
|
|
$this->rules = $options; |
34
|
|
|
} elseif (is_array($options)) { |
35
|
|
|
if (!isset($options['rules']) || !$options['rules'] instanceof AllOf) { |
36
|
|
|
throw new InvalidArgumentException('Validation rules are missing or invalid'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$this->message = $options['message'] ?? null; |
40
|
|
|
$this->messages = $options['messages'] ?? []; |
41
|
|
|
$this->rules = $options['rules']; |
42
|
|
|
} else { |
43
|
|
|
throw new InvalidArgumentException(sprintf('Options must be of type %s or array, %s given', AllOf::class, gettype($options))); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Gets the error message. |
49
|
|
|
* |
50
|
|
|
* @return string|null |
51
|
|
|
*/ |
52
|
|
|
public function getMessage() |
53
|
|
|
{ |
54
|
|
|
return $this->message; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Gets individual rules messages. |
59
|
|
|
* |
60
|
|
|
* @return string[] |
61
|
|
|
*/ |
62
|
|
|
public function getMessages() |
63
|
|
|
{ |
64
|
|
|
return $this->messages; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Gets the validation rules. |
69
|
|
|
* |
70
|
|
|
* @return AllOf |
71
|
|
|
*/ |
72
|
|
|
public function getValidationRules() |
73
|
|
|
{ |
74
|
|
|
return $this->rules; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Tells whether a single message has been set. |
79
|
|
|
* |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
|
|
public function hasMessage() |
83
|
|
|
{ |
84
|
|
|
return !empty($this->message); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Tells whether individual rules messages have been set. |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public function hasMessages() |
93
|
|
|
{ |
94
|
|
|
return !empty($this->messages); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets the error message. |
99
|
|
|
* |
100
|
|
|
* @param string $message |
101
|
|
|
*/ |
102
|
|
|
public function setMessage($message) |
103
|
|
|
{ |
104
|
|
|
$this->message = $message; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Sets individual rules messages. |
109
|
|
|
* |
110
|
|
|
* @param string[] $messages |
111
|
|
|
*/ |
112
|
|
|
public function setMessages(array $messages) |
113
|
|
|
{ |
114
|
|
|
$this->messages = $messages; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sets the validation rules. |
119
|
|
|
* |
120
|
|
|
* @param AllOf $rules |
121
|
|
|
*/ |
122
|
|
|
public function setValidationRules(AllOf $rules) |
123
|
|
|
{ |
124
|
|
|
$this->rules = $rules; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|