Completed
Push — feature/middleware-tmp ( 697e27...e92e53 )
by Romain
01:57
created

FormValidationMiddleware::injectCurrentStep()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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\FormValidation;
15
16
use Romm\Formz\Core\Core;
17
use Romm\Formz\Form\Definition\Field\Field;
18
use Romm\Formz\Middleware\Item\FieldValidation\FieldValidationArguments;
19
use Romm\Formz\Middleware\Item\FieldValidation\FieldValidationSignal;
20
use Romm\Formz\Middleware\Item\OnBeginMiddleware;
21
use Romm\Formz\Middleware\Processor\PresetMiddlewareInterface;
22
use Romm\Formz\Middleware\Processor\RemoveFromSingleFieldValidationContext;
23
use Romm\Formz\Middleware\Signal\SendsMiddlewareSignal;
24
use Romm\Formz\Validation\Validator\Form\AbstractFormValidator;
25
26
/**
27
 * This middleware takes care of validating the form instance, with a proper
28
 * form validator instance.
29
 *
30
 * You can bind middlewares to the signal `FormValidationSignal`, which will be
31
 * dispatched if and only if the form was submitted by the user.
32
 *
33
 * Please note that this middleware will not be called when being in a "single
34
 * field validation context".
35
 *
36
 * @see \Romm\Formz\Middleware\Processor\RemoveFromSingleFieldValidationContext
37
 */
38
class FormValidationMiddleware extends OnBeginMiddleware implements PresetMiddlewareInterface, SendsMiddlewareSignal, RemoveFromSingleFieldValidationContext
39
{
40
    /**
41
     * @var \Romm\Formz\Middleware\Item\FormValidation\FormValidationMiddlewareOption
42
     */
43
    protected $options;
44
45
    /**
46
     * @var AbstractFormValidator
47
     */
48
    protected $validator;
49
50
    /**
51
     * @see FormValidationMiddleware
52
     */
53
    protected function process()
54
    {
55
        $formObject = $this->getFormObject();
56
57
        $this->beforeSignal(FormValidationSignal::class)->dispatch();
58
59
        if ($formObject->hasForm()
60
            && $formObject->formWasSubmitted()
61
        ) {
62
            $this->validator = Core::instantiate($this->options->getFormValidatorClassName());
63
64
            $this->injectFieldValidationCallback();
65
66
            $this->validator->validate($formObject->getForm());
67
68
            $this->afterSignal(FormValidationSignal::class)->dispatch();
69
        }
70
    }
71
72
    /**
73
     * Injects a callback in the form validator data object. The callback will
74
     * be called each time a field is validated by the validator, and will allow
75
     * to dispatch a field validation signal to other middlewares.
76
     */
77
    protected function injectFieldValidationCallback()
78
    {
79
        $callback = function (Field $field) {
80
            $this->afterSignal(FieldValidationSignal::class)
81
                ->withArguments(new FieldValidationArguments($field))
82
                ->dispatch();
83
        };
84
85
        $this->validator->getDataObject()->addFieldValidationCallback($callback);
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function getAllowedSignals()
92
    {
93
        return [FormValidationSignal::class, FieldValidationSignal::class];
94
    }
95
}
96