Completed
Push — feature/version-2 ( 4671c0...cfd51e )
by Romain
02:15
created

validateFieldsConditions()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
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\Validation\Validator\Internal;
15
16
use Romm\Formz\Condition\Exceptions\InvalidConditionException;
17
use Romm\Formz\Form\Definition\FormDefinition;
18
use TYPO3\CMS\Extbase\Error\Error;
19
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
20
21
class FormDefinitionValidator extends AbstractValidator
22
{
23
    /**
24
     * @var FormDefinition
25
     */
26
    protected $definition;
27
28
    /**
29
     * @param FormDefinition $definition
30
     */
31
    public function isValid($definition)
32
    {
33
        $this->definition = $definition;
34
35
        $this->validateConditionList();
36
        $this->validateFieldsConditions();
37
        $this->validateFieldsValidationConditions();
38
    }
39
40
    /**
41
     * Validates each condition of the list, at the root of the form definition.
42
     */
43
    protected function validateConditionList()
44
    {
45
        foreach ($this->definition->getConditionList() as $conditionName => $condition) {
46
            try {
47
                $condition->validateConditionConfiguration($this->definition);
48
            } catch (InvalidConditionException $exception) {
49
                $property = "conditionList.{$conditionName}";
50
                $this->addPropertyError($property, $exception->getMessage(), $exception->getCode());
51
            }
52
        }
53
    }
54
55
    /**
56
     * Loops on each field, and validates every condition.
57
     */
58
    protected function validateFieldsConditions()
59
    {
60
        foreach ($this->definition->getFields() as $field) {
61
            foreach ($field->getActivation()->getConditions() as $conditionName => $condition) {
62
                try {
63
                    $condition->validateConditionConfiguration($this->definition);
64
                } catch (InvalidConditionException $exception) {
65
                    $property = "fields.{$field->getName()}.activation.conditions.{$conditionName}";
66
                    $this->addPropertyError($property, $exception->getMessage(), $exception->getCode());
67
                }
68
            }
69
        }
70
    }
71
72
    /**
73
     * Loops on each field validation, and validates every condition.
74
     */
75
    protected function validateFieldsValidationConditions()
76
    {
77
        foreach ($this->definition->getFields() as $field) {
78
            foreach ($field->getValidation() as $validation) {
79
                foreach ($validation->getActivation()->getConditions() as $conditionName => $condition) {
80
                    try {
81
                        $condition->validateConditionConfiguration($this->definition);
82
                    } catch (InvalidConditionException $exception) {
83
                        $property = "fields.{$field->getName()}.validation.{$validation->getName()}.activation.conditions.{$conditionName}";
84
                        $this->addPropertyError($property, $exception->getMessage(), $exception->getCode());
85
                    }
86
                }
87
            }
88
        }
89
    }
90
91
    /**
92
     * @param string $property
93
     * @param string $message
94
     * @param int    $code
95
     */
96
    protected function addPropertyError($property, $message, $code)
97
    {
98
        $error = new Error($message, $code);
99
        $this->result->forProperty($property)->addError($error);
100
    }
101
}
102