1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* 2017 Romain CANON <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This file is part of the TYPO3 FormZ project. |
6
|
|
|
* It is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU General Public License, either |
8
|
|
|
* version 3 of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, see: |
11
|
|
|
* http://www.gnu.org/licenses/gpl-3.0.html |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Romm\Formz\Validation\Validator\Internal; |
15
|
|
|
|
16
|
|
|
use Romm\Formz\Middleware\Item\Begin\BeginSignal; |
17
|
|
|
use Romm\Formz\Middleware\Item\End\EndSignal; |
18
|
|
|
use Romm\Formz\Middleware\MiddlewareInterface; |
19
|
|
|
use Romm\Formz\Middleware\Signal\After; |
20
|
|
|
use Romm\Formz\Middleware\Signal\Before; |
21
|
|
|
use Romm\Formz\Middleware\Signal\MiddlewareSignal; |
22
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator; |
23
|
|
|
|
24
|
|
|
class MiddlewareIsValidValidator extends AbstractValidator |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param string $middleware |
28
|
|
|
*/ |
29
|
|
|
public function isValid($middleware) |
30
|
|
|
{ |
31
|
|
|
$this->checkImplementation($middleware); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $middleware |
36
|
|
|
*/ |
37
|
|
|
protected function checkImplementation($middleware) |
38
|
|
|
{ |
39
|
|
|
if (false === class_exists($middleware)) { |
40
|
|
|
$this->addError( |
41
|
|
|
'Class name given was not found: "%s".', |
42
|
|
|
1489070202, |
43
|
|
|
[$middleware] |
44
|
|
|
); |
45
|
|
|
} else { |
46
|
|
|
$interfaces = class_implements($middleware); |
47
|
|
|
|
48
|
|
|
if (false === in_array(MiddlewareInterface::class, $interfaces)) { |
49
|
|
|
$this->addError( |
50
|
|
|
'Class "%s" must implement "%s".', |
51
|
|
|
1489070282, |
52
|
|
|
[$middleware, MiddlewareInterface::class] |
53
|
|
|
); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$signalsFound = []; |
57
|
|
|
foreach ($interfaces as $interface) { |
58
|
|
|
if (in_array(MiddlewareSignal::class, class_implements($interface))) { |
59
|
|
|
$signalsFound[] = $interface; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (empty($signalsFound)) { |
64
|
|
|
$this->addError( |
65
|
|
|
'Class "%s" must implement one interface that extends "%s".', |
66
|
|
|
1489074248, |
67
|
|
|
[$middleware, MiddlewareSignal::class] |
68
|
|
|
); |
69
|
|
|
} elseif (count($signalsFound) > 1) { |
70
|
|
|
$this->addError( |
71
|
|
|
'Class "%s" must implement only one interface that extends "%s"; %s were found: "%s"', |
72
|
|
|
1489074852, |
73
|
|
|
[ |
74
|
|
|
$middleware, |
75
|
|
|
MiddlewareSignal::class, |
76
|
|
|
count($signalsFound), |
77
|
|
|
implode('", "', $signalsFound) |
78
|
|
|
] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if (false === in_array(Before::class, $interfaces) |
83
|
|
|
&& false === in_array(After::class, $interfaces) |
84
|
|
|
) { |
85
|
|
|
$this->addError( |
86
|
|
|
'Class "%s" must implement at least one of these interfaces: "%s", "%s".', |
87
|
|
|
1489074986, |
88
|
|
|
[ |
89
|
|
|
$middleware, |
90
|
|
|
Before::class, |
91
|
|
|
After::class |
92
|
|
|
] |
93
|
|
|
); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (false === $this->result->hasErrors()) { |
97
|
|
|
if (in_array(BeginSignal::class, $interfaces) |
98
|
|
|
&& in_array(Before::class, $interfaces) |
99
|
|
|
) { |
100
|
|
|
$this->addError( |
101
|
|
|
'Class "%s" implements interfaces "%s" and "%s", but the signal "before beginning" is (obviously) invalid. Please remove "%s" dependency.', |
102
|
|
|
1489075185, |
103
|
|
|
[ |
104
|
|
|
$middleware, |
105
|
|
|
Before::class, |
106
|
|
|
BeginSignal::class, |
107
|
|
|
Before::class, |
108
|
|
|
] |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (in_array(EndSignal::class, $interfaces) |
113
|
|
|
&& in_array(After::class, $interfaces) |
114
|
|
|
) { |
115
|
|
|
$this->addError( |
116
|
|
|
'Class "%s" implements interfaces "%s" and "%s", but the signal "after the end" is (obviously) invalid. Please remove "%s" dependency.', |
117
|
|
|
1489075242, |
118
|
|
|
[ |
119
|
|
|
$middleware, |
120
|
|
|
After::class, |
121
|
|
|
EndSignal::class, |
122
|
|
|
After::class, |
123
|
|
|
] |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|