Completed
Push — unit-tests-conditions ( dc9eee...e8c03e )
by Romain
02:37
created

FieldHasValueCondition::setFieldName()   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
c 0
b 0
f 0
rs 10
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 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
     * @inheritdoc
49
     */
50
    public function getCssResult()
51
    {
52
        if ($this->fieldValue == '') {
53
            return '[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '="' . $this->fieldValue . '"]';
54
        } else {
55
            return '[' . DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName) . '~="' . $this->fieldValue . '"]';
56
        }
57
    }
58
59
    /**
60
     * @inheritdoc
61
     */
62
    public function getJavaScriptResult()
63
    {
64
        return $this->getDefaultJavaScriptCall(
65
            [
66
                'fieldName'  => $this->fieldName,
67
                'fieldValue' => $this->fieldValue
68
            ]
69
        );
70
    }
71
72
    /**
73
     * @inheritdoc
74
     */
75
    public function getPhpResult(PhpConditionDataObject $dataObject)
76
    {
77
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
78
79
        return (is_array($value))
80
            ? (true === in_array($this->fieldValue, $value))
81
            : ($value == $this->fieldValue);
82
    }
83
84
    /**
85
     * @see validateConditionConfiguration()
86
     * @throws InvalidConditionException
87
     * @return bool
88
     */
89
    protected function checkConditionConfiguration()
90
    {
91
        $configuration = $this->formObject->getConfiguration();
92
93
        if (false === $configuration->hasField($this->fieldName)) {
94
            throw new InvalidConditionException(
95
                'The field "' . $this->fieldName . '" does not exist.',
96
                1488192031
97
            );
98
        }
99
100
        return true;
101
    }
102
103
    /**
104
     * @return string
105
     */
106
    public function getFieldName()
107
    {
108
        return $this->fieldName;
109
    }
110
111
    /**
112
     * @param string $fieldName
113
     */
114
    public function setFieldName($fieldName)
115
    {
116
        $this->fieldName = $fieldName;
117
    }
118
119
    /**
120
     * @return mixed
121
     */
122
    public function getFieldValue()
123
    {
124
        return $this->fieldValue;
125
    }
126
127
    /**
128
     * @param string $fieldValue
129
     */
130
    public function setFieldValue($fieldValue)
131
    {
132
        $this->fieldValue = $fieldValue;
133
    }
134
}
135