|
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\Middleware\Item\Begin; |
|
15
|
|
|
|
|
16
|
|
|
use Romm\Formz\Form\FormInterface; |
|
17
|
|
|
use Romm\Formz\Form\FormObject\FormObjectFactory; |
|
18
|
|
|
use Romm\Formz\Middleware\BasicMiddlewareInterface; |
|
19
|
|
|
use Romm\Formz\Middleware\Processor\MiddlewareProcessor; |
|
20
|
|
|
use Romm\Formz\Middleware\Signal\After; |
|
21
|
|
|
use Romm\Formz\Middleware\Signal\SignalObject; |
|
22
|
|
|
|
|
23
|
|
|
final class BeginMiddleware implements BasicMiddlewareInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var MiddlewareProcessor |
|
27
|
|
|
*/ |
|
28
|
|
|
private $processor; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Initialization of this middleware. |
|
32
|
|
|
*/ |
|
33
|
|
|
public function initialize() |
|
34
|
|
|
{ |
|
35
|
|
|
$this->checkFormSubmission(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* This is the first middleware being called, it will send a signal to all |
|
40
|
|
|
* middlewares that depend on it. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function execute() |
|
43
|
|
|
{ |
|
44
|
|
|
$signalObject = new SignalObject($this->processor, BeginSignal::class, After::class); |
|
45
|
|
|
$signalObject->dispatch(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Will check if the current form was submitted by the user. If it is found, |
|
50
|
|
|
* the form instance is injected in the form object. |
|
51
|
|
|
*/ |
|
52
|
|
|
protected function checkFormSubmission() |
|
53
|
|
|
{ |
|
54
|
|
|
if ($this->processor->inSingleFieldValidationContext()) { |
|
55
|
|
|
/* |
|
56
|
|
|
* In "single field validation context", there is no need to check |
|
57
|
|
|
* for the form submission. |
|
58
|
|
|
*/ |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$request = $this->processor->getRequest(); |
|
63
|
|
|
$formObject = $this->processor->getFormObject(); |
|
64
|
|
|
$formName = $formObject->getName(); |
|
65
|
|
|
|
|
66
|
|
|
if ($request->getMethod() === 'POST' |
|
67
|
|
|
&& $this->processor->getRequestArguments()->hasArgument($formName) |
|
68
|
|
|
) { |
|
69
|
|
|
if (false === $request->hasArgument('formzData')) { |
|
70
|
|
|
throw new \Exception('todo'); // @todo |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$form = $this->getFormInstance(); |
|
74
|
|
|
|
|
75
|
|
|
$formObject->setForm($form); |
|
76
|
|
|
|
|
77
|
|
|
$formzData = $request->getArgument('formzData'); |
|
78
|
|
|
$formObject->getRequestData()->fillFromHash($formzData); |
|
79
|
|
|
|
|
80
|
|
|
$proxy = FormObjectFactory::get()->getProxy($form); |
|
|
|
|
|
|
81
|
|
|
$proxy->markFormAsSubmitted(); |
|
82
|
|
|
|
|
83
|
|
|
$this->injectFormHashInProxy(); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Fetches the form hash from the request data that has been submitted with |
|
89
|
|
|
* the form, and injects it in the form proxy. |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function injectFormHashInProxy() |
|
92
|
|
|
{ |
|
93
|
|
|
$formObject = $this->processor->getFormObject(); |
|
94
|
|
|
$hash = $formObject->getRequestData()->getFormHash(); |
|
95
|
|
|
|
|
96
|
|
|
$proxy = FormObjectFactory::get()->getProxy($formObject->getForm()); |
|
|
|
|
|
|
97
|
|
|
$proxy->setFormHash($hash); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return FormInterface |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getFormInstance() |
|
104
|
|
|
{ |
|
105
|
|
|
$formName = $this->processor->getFormObject()->getName(); |
|
106
|
|
|
$formArray = $this->processor->getRequest()->getArgument($formName); |
|
107
|
|
|
$argument = $this->processor->getRequestArguments()->getArgument($formName); |
|
108
|
|
|
|
|
109
|
|
|
return $argument->setValue($formArray)->getValue(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param MiddlewareProcessor $middlewareProcessor |
|
114
|
|
|
*/ |
|
115
|
|
|
final public function bindMiddlewareProcessor(MiddlewareProcessor $middlewareProcessor) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->processor = $middlewareProcessor; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.