|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @author: Abdul Qureshi. <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* This file has been modified from the original source. |
|
7
|
|
|
* See original here: |
|
8
|
|
|
* |
|
9
|
|
|
* @link: https://github.com/progsmile/request-validator |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace TheSupportGroup\Common\Validator\Helpers; |
|
12
|
|
|
|
|
13
|
|
|
use TheSupportGroup\Common\ValidationInterop\ValidationProviderInterface; |
|
14
|
|
|
use TheSupportGroup\Common\Validator\Contracts\Helpers\FieldsErrorBagInterface; |
|
15
|
|
|
use TheSupportGroup\Common\Validator\Contracts\Helpers\ValidationResultProcessorInterface; |
|
16
|
|
|
use TheSupportGroup\Common\Validator\Rules\BaseRule; |
|
17
|
|
|
use TheSupportGroup\Common\Validator\Validator; |
|
18
|
|
|
|
|
19
|
|
|
class ValidationResultProcessor implements ValidationResultProcessorInterface |
|
20
|
|
|
{ |
|
21
|
|
|
/** @var array|null all errors bag */ |
|
22
|
|
|
public $fieldsErrorBag = null; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param array $userMessages |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __construct( |
|
28
|
|
|
FieldsErrorBagInterface $fieldsErrorBag, |
|
29
|
|
|
array $userMessages = [] |
|
30
|
|
|
) { |
|
31
|
|
|
$this->fieldsErrorBag = $fieldsErrorBag; |
|
|
|
|
|
|
32
|
|
|
$this->fieldsErrorBag->setUserMessages($userMessages); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Returns messages count. |
|
37
|
|
|
* |
|
38
|
|
|
* @return int |
|
39
|
|
|
*/ |
|
40
|
|
|
public function count() |
|
41
|
|
|
{ |
|
42
|
|
|
return count($this->fieldsErrorBag->getErrorMessages()); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* If such $field contains in. |
|
47
|
|
|
* |
|
48
|
|
|
* @param $fieldName |
|
49
|
|
|
* |
|
50
|
|
|
* @return bool |
|
51
|
|
|
*/ |
|
52
|
|
|
public function hasErrors($fieldName) |
|
53
|
|
|
{ |
|
54
|
|
|
return (bool) count($this->fieldsErrorBag->getErrorMessages($fieldName)); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get flat messages array, or all messages from field. |
|
59
|
|
|
* |
|
60
|
|
|
* @param string $field |
|
61
|
|
|
* |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function getErrors($field = '') |
|
65
|
|
|
{ |
|
66
|
|
|
if ($field) { |
|
67
|
|
|
return isset($this->fieldsErrorBag->getErrorMessages()[$field]) ? $this->fieldsErrorBag->getErrorMessages()[$field] : []; |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$messages = []; |
|
71
|
|
|
|
|
72
|
|
|
// Pass in the variable. |
|
73
|
|
|
$errorMessages = $this->fieldsErrorBag->getErrorMessages(); |
|
|
|
|
|
|
74
|
|
|
array_walk_recursive($errorMessages, function ($message) use (&$messages) { |
|
75
|
|
|
$messages[] = $message; |
|
76
|
|
|
}); |
|
77
|
|
|
|
|
78
|
|
|
return $messages; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get 2d array with fields and messages. |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getRawErrors() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->fieldsErrorBag->getErrorMessages(); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* For each rule get it's first message. |
|
93
|
|
|
* |
|
94
|
|
|
* @return array |
|
95
|
|
|
*/ |
|
96
|
|
|
public function firsts() |
|
97
|
|
|
{ |
|
98
|
|
|
$messages = []; |
|
99
|
|
|
foreach ($this->fieldsErrorBag->getErrorMessages() as $fieldsMessages) { |
|
|
|
|
|
|
100
|
|
|
foreach ($fieldsMessages as $fieldMessage) { |
|
101
|
|
|
$messages[] = $fieldMessage; |
|
102
|
|
|
break; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return $messages; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns first message from $field or error messages array. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $field |
|
113
|
|
|
* |
|
114
|
|
|
* @return mixed |
|
115
|
|
|
*/ |
|
116
|
|
|
public function first($field = '') |
|
117
|
|
|
{ |
|
118
|
|
|
if (isset($this->fieldsErrorBag->getErrorMessages()[$field])) { |
|
|
|
|
|
|
119
|
|
|
$message = reset($this->fieldsErrorBag->getErrorMessages()[$field]); |
|
|
|
|
|
|
120
|
|
|
} else { |
|
121
|
|
|
$firstMessages = $this->firsts(); |
|
122
|
|
|
$message = reset($firstMessages); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
return $message; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Choosing error message: custom or default. |
|
130
|
|
|
* |
|
131
|
|
|
* @param $instance |
|
132
|
|
|
* |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
|
|
public function chooseErrorMessage(BaseRule $instance) |
|
136
|
|
|
{ |
|
137
|
|
|
list($fieldName, $ruleValue, $ruleParams) = $instance->getParams(); |
|
138
|
|
|
$ruleErrorFormat = $fieldName.'.'.lcfirst($instance->getRuleName()); |
|
139
|
|
|
|
|
140
|
|
|
if (isset($this->fieldsErrorBag->getUserMessages()[$ruleErrorFormat])) { |
|
|
|
|
|
|
141
|
|
|
$ruleErrorMessage = $this->fieldsErrorBag->getUserMessages()[$ruleErrorFormat]; |
|
|
|
|
|
|
142
|
|
|
} else { |
|
143
|
|
|
$ruleErrorMessage = $instance->getMessage(); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
$this->fieldsErrorBag->add( |
|
|
|
|
|
|
147
|
|
|
$fieldName, |
|
148
|
|
|
strtr( |
|
149
|
|
|
$ruleErrorMessage, |
|
150
|
|
|
[ |
|
151
|
|
|
':field:' => $fieldName, |
|
152
|
|
|
':rule:' => $ruleValue, |
|
153
|
|
|
':param:' => $ruleParams, |
|
154
|
|
|
] |
|
155
|
|
|
) |
|
156
|
|
|
); |
|
157
|
|
|
|
|
158
|
|
|
return $this; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Get messages. |
|
163
|
|
|
* |
|
164
|
|
|
* @param $fieldName |
|
165
|
|
|
* |
|
166
|
|
|
* @return FieldsErrorBag |
|
167
|
|
|
*/ |
|
168
|
|
|
public function __get($fieldName) |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->fieldsErrorBag->setField($fieldName); |
|
|
|
|
|
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..