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

FieldHasValueCondition::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 has the given value.
23
 */
24
class FieldHasValueCondition extends AbstractConditionItem
25
{
26
    const CONDITION_NAME = 'fieldHasValue';
27
28
    /**
29
     * @inheritdoc
30
     * @var array
31
     */
32
    protected static $javaScriptFiles = [
33
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldHasValue.js'
34
    ];
35
36
    /**
37
     * @var string
38
     * @validate NotEmpty
39
     */
40
    protected $fieldName;
41
42
    /**
43
     * @var string
44
     */
45
    protected $fieldValue;
46
47
    /**
48
     * @return string
49
     */
50
    public function getFieldName()
51
    {
52
        return $this->fieldName;
53
    }
54
55
    /**
56
     * @return mixed
57
     */
58
    public function getFieldValue()
59
    {
60
        return $this->fieldValue;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function getCssResult()
67
    {
68
        if ($this->fieldValue == '') {
69
            return '[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '="' . $this->fieldValue . '"]';
70
        } else {
71
            return '[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '~="' . $this->fieldValue . '"]';
72
        }
73
    }
74
75
    /**
76
     * @inheritdoc
77
     */
78
    public function getJavaScriptResult()
79
    {
80
        return $this->getDefaultJavaScriptCall(
81
            [
82
                'fieldName'  => $this->fieldName,
83
                'fieldValue' => $this->fieldValue
84
            ]
85
        );
86
    }
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function getPhpResult(PhpConditionDataObject $dataObject)
92
    {
93
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
94
95
        return (is_array($value))
96
            ? (true === in_array($this->fieldValue, $value))
97
            : ($value == $this->fieldValue);
98
    }
99
100
    /**
101
     * @see validateConditionConfiguration()
102
     * @throws InvalidConditionException
103
     * @return bool
104
     */
105
    protected function checkConditionConfiguration()
106
    {
107
        $configuration = $this->formObject->getConfiguration();
108
109
        if (false === $configuration->hasField($this->fieldName)) {
110
            throw new InvalidConditionException(
111
                'The field "' . $this->fieldName . '" does not exist.',
112
                1488192031
113
            );
114
        }
115
116
        return true;
117
    }
118
}
119