Completed
Push — unit-tests-validation ( 34c29c...a12b55 )
by Romain
02:46
created

checkConditionConfiguration()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 7
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\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 TYPO3\CMS\Extbase\Reflection\ObjectAccess;
20
21
/**
22
 * This condition will match when a field is valid (its validation returned no
23
 * error).
24
 */
25
class FieldIsEmptyCondition extends AbstractConditionItem
26
{
27
    const CONDITION_NAME = 'fieldIsEmpty';
28
29
    /**
30
     * @inheritdoc
31
     * @var array
32
     */
33
    protected static $javaScriptFiles = [
34
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsEmpty.js'
35
    ];
36
37
    /**
38
     * @var string
39
     * @validate NotEmpty
40
     */
41
    protected $fieldName;
42
43
    /**
44
     * @inheritdoc
45
     */
46
    public function getCssResult()
47
    {
48
        return '[' . DataAttributesAssetHandler::getFieldDataValidKey($this->fieldName) . '=""]';
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function getJavaScriptResult()
55
    {
56
        return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]);
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getPhpResult(PhpConditionDataObject $dataObject)
63
    {
64
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
65
66
        return empty($value);
67
    }
68
69
    /**
70
     * @see validateConditionConfiguration()
71
     * @throws InvalidConditionException
72
     * @return bool
73
     */
74
    protected function checkConditionConfiguration()
75
    {
76
        $configuration = $this->formObject->getConfiguration();
77
78
        if (false === $configuration->hasField($this->fieldName)) {
79
            throw new InvalidConditionException(
80
                'The field "' . $this->fieldName . '" does not exist.',
81
                1488191994
82
            );
83
        }
84
85
        return true;
86
    }
87
88
    /**
89
     * @return string
90
     */
91
    public function getFieldName()
92
    {
93
        return $this->fieldName;
94
    }
95
}
96