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

FieldIsEmptyCondition::__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
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
21
22
/**
23
 * This condition will match when a field is valid (its validation returned no
24
 * error).
25
 */
26
class FieldIsEmptyCondition extends AbstractConditionItem
27
{
28
    const CONDITION_IDENTIFIER = 'fieldIsEmpty';
29
30
    /**
31
     * @inheritdoc
32
     * @var array
33
     */
34
    protected static $javaScriptFiles = [
35
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsEmpty.js'
36
    ];
37
38
    /**
39
     * @var string
40
     * @validate NotEmpty
41
     */
42
    protected $fieldName;
43
44
    /**
45
     * @param string $fieldName
46
     */
47
    public function __construct($fieldName)
48
    {
49
        $this->fieldName = $fieldName;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function getCssResult()
56
    {
57
        return '[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '=""]';
58
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63
    public function getJavaScriptResult()
64
    {
65
        return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function getPhpResult(PhpConditionDataObject $dataObject)
72
    {
73
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
74
75
        return empty($value);
76
    }
77
78
    /**
79
     * Checks the condition configuration/options.
80
     *
81
     * If any syntax/configuration error is found, an exception of type
82
     * `InvalidConditionException` must be thrown.
83
     *
84
     * @param FormDefinition $formDefinition
85
     * @throws InvalidConditionException
86
     */
87
    protected function checkConditionConfiguration(FormDefinition $formDefinition)
88
    {
89
        if (false === $formDefinition->hasField($this->fieldName)) {
90
            throw InvalidConditionException::conditionFieldIsEmptyFieldNotFound($this->fieldName);
91
        }
92
    }
93
}
94