Completed
Push — feature/version-2 ( a1aace...47461d )
by Romain
03:02
created

FieldIsValidCondition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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\Condition\Items;
15
16
use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler;
17
use Romm\Formz\Condition\Exceptions\InvalidConditionException;
18
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
19
use Romm\Formz\Form\Definition\FormDefinition;
20
21
/**
22
 * This condition will match when a field is valid (its validation returned no
23
 * error).
24
 */
25
class FieldIsValidCondition extends AbstractConditionItem
26
{
27
    const CONDITION_IDENTIFIER = 'fieldIsValid';
28
29
    /**
30
     * @inheritdoc
31
     * @var array
32
     */
33
    protected static $javaScriptFiles = [
34
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsValid.js'
35
    ];
36
37
    /**
38
     * @var string
39
     * @validate NotEmpty
40
     */
41
    protected $fieldName;
42
43
    /**
44
     * @param string $fieldName
45
     */
46
    public function __construct($fieldName)
47
    {
48
        $this->fieldName = $fieldName;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function getCssResult()
55
    {
56
        return '[' . DataAttributesAssetHandler::getFieldDataValidKey($this->fieldName) . '="1"]';
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getJavaScriptResult()
63
    {
64
        return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]);
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70
    public function getPhpResult(PhpConditionDataObject $dataObject)
71
    {
72
        $formValidator = $dataObject->getFormValidator();
73
        $field = $this->formObject->getDefinition()->getField($this->fieldName);
74
        $formValidator->validateField($field);
75
        $result = $formValidator->getResult();
76
77
        return false === $result->forProperty($this->fieldName)->hasErrors()
78
            && false === $result->fieldIsDeactivated($field);
79
    }
80
81
    /**
82
     * Checks the condition configuration/options.
83
     *
84
     * If any syntax/configuration error is found, an exception of type
85
     * `InvalidConditionException` must be thrown.
86
     *
87
     * @param FormDefinition $formDefinition
88
     * @throws InvalidConditionException
89
     */
90
    protected function checkConditionConfiguration(FormDefinition $formDefinition)
91
    {
92
        if (false === $formDefinition->hasField($this->fieldName)) {
93
            throw InvalidConditionException::conditionFieldIsValidFieldNotFound($this->fieldName);
94
        }
95
    }
96
}
97