|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace voku\HtmlFormValidator; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\CssSelector\Exception\SyntaxErrorException; |
|
8
|
|
|
use voku\helper\HtmlDomParser; |
|
9
|
|
|
|
|
10
|
|
|
class ValidatorResult |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* List of errors from validation |
|
14
|
|
|
* |
|
15
|
|
|
* @var string[] |
|
16
|
|
|
*/ |
|
17
|
|
|
private $errors = []; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $values = []; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var HtmlDomParser |
|
26
|
|
|
*/ |
|
27
|
|
|
private $formDocument; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* ValidatorResult constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param HtmlDomParser $formDocument |
|
33
|
|
|
*/ |
|
34
|
19 |
|
public function __construct($formDocument) |
|
35
|
|
|
{ |
|
36
|
19 |
|
$this->formDocument = clone $formDocument; |
|
37
|
19 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return int |
|
41
|
|
|
*/ |
|
42
|
7 |
|
public function countErrors(): int |
|
43
|
|
|
{ |
|
44
|
7 |
|
return \count($this->errors); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return array |
|
49
|
|
|
*/ |
|
50
|
13 |
|
public function getErrorMessages(): array |
|
51
|
|
|
{ |
|
52
|
13 |
|
return $this->errors; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
2 |
|
public function getHtml(): string |
|
59
|
|
|
{ |
|
60
|
2 |
|
return $this->formDocument->html(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
2 |
|
public function getValues(): array |
|
67
|
|
|
{ |
|
68
|
2 |
|
return $this->values; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return bool |
|
73
|
|
|
*/ |
|
74
|
7 |
|
public function isSuccess(): bool |
|
75
|
|
|
{ |
|
76
|
7 |
|
return ($this->countErrors() === 0); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $field |
|
81
|
|
|
* @param string|string[] $errorMsg |
|
82
|
|
|
* |
|
83
|
|
|
* @return ValidatorResult |
|
84
|
|
|
*/ |
|
85
|
12 |
|
public function setError(string $field, $errorMsg): ValidatorResult |
|
86
|
|
|
{ |
|
87
|
|
|
try { |
|
88
|
12 |
|
$inputTag = $this->formDocument->find('[name=' . $field . ']', 0); |
|
89
|
3 |
|
} catch (SyntaxErrorException $syntaxErrorException) { |
|
90
|
3 |
|
$inputTag = null; |
|
91
|
|
|
// TODO@me -> can the symfony CssSelectorConverter use array-name-attributes? |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
12 |
|
if ($inputTag) { |
|
95
|
9 |
|
$inputTag->setAttribute('aria-invalid', 'true'); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
12 |
|
if (\is_array($errorMsg) === true) { |
|
99
|
|
|
foreach ($errorMsg as $errorMsgSingle) { |
|
100
|
|
|
$this->errors[$field][] = $errorMsgSingle; |
|
101
|
|
|
} |
|
102
|
|
|
} else { |
|
103
|
12 |
|
$this->errors[$field][] = $errorMsg; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
12 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
18 |
|
public function setValues(string $field, $value): ValidatorResult |
|
110
|
|
|
{ |
|
111
|
18 |
|
$this->values[$field] = $value; |
|
112
|
|
|
|
|
113
|
18 |
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: