Completed
Push — unit-tests-conditions ( 195e3f...91d90b )
by Romain
02:19
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
            'fieldName'  => $this->fieldName,
66
            'fieldValue' => $this->fieldValue
67
        ]);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function getPhpResult(PhpConditionDataObject $dataObject)
74
    {
75
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
76
77
        return (is_array($value))
78
            ? (true === in_array($this->fieldValue, $value))
79
            : ($value == $this->fieldValue);
80
    }
81
82
    /**
83
     * @see validateConditionConfiguration()
84
     * @throws InvalidConditionException
85
     * @return bool
86
     */
87
    protected function checkConditionConfiguration()
88
    {
89
        $configuration = $this->formObject->getConfiguration();
90
91
        if (false === $configuration->hasField($this->fieldName)) {
92
            throw InvalidConditionException::conditionFieldHasValueFieldNotFound($this->fieldName);
93
        }
94
95
        return true;
96
    }
97
}
98