Completed
Push — development ( b44b0e...e4a9bd )
by Romain
02:43
created

FieldIsFilledCondition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * 2018 Abdeljabar SAID <[email protected]
4
 * 2017 Romain CANON <[email protected]>
5
 *
6
 * This file is part of the TYPO3 FormZ project.
7
 * It is free software; you can redistribute it and/or modify it
8
 * under the terms of the GNU General Public License, either
9
 * version 3 of the License, or any later version.
10
 *
11
 * For the full copyright and license information, see:
12
 * http://www.gnu.org/licenses/gpl-3.0.html
13
 */
14
15
namespace Romm\Formz\Condition\Items;
16
17
use Romm\Formz\AssetHandler\Html\DataAttributesAssetHandler;
18
use Romm\Formz\Condition\Exceptions\InvalidConditionException;
19
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
20
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
21
22
/**
23
 * This condition will match when a field is filled with any value.
24
 */
25
class FieldIsFilledCondition extends AbstractConditionItem
26
{
27
    const CONDITION_IDENTIFIER = 'fieldIsFilled';
28
29
    /**
30
     * @inheritdoc
31
     * @var array
32
     */
33
    protected static $javaScriptFiles = [
34
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsFilled.js'
35
    ];
36
37
    /**
38
     * @var string
39
     * @validate NotEmpty
40
     */
41
    protected $fieldName;
42
43
    /**
44
     * @param string $fieldName
45
     */
46
    public function __construct($fieldName)
47
    {
48
        $this->fieldName = $fieldName;
49
    }
50
51
    /**
52
     * @inheritdoc
53
     */
54
    public function getCssResult()
55
    {
56
        $valueKey = DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName);
57
58
        return '[' . $valueKey . ']:not([' . $valueKey . '=""])';
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64
    public function getJavaScriptResult()
65
    {
66
        return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]);
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function getPhpResult(PhpConditionDataObject $dataObject)
73
    {
74
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
75
76
        return !empty($value);
77
    }
78
79
    /**
80
     * Checks the condition configuration/options.
81
     *
82
     * If any syntax/configuration error is found, an exception of type
83
     * `InvalidConditionException` must be thrown.
84
     *
85
     * @throws InvalidConditionException
86
     */
87
    protected function checkConditionConfiguration()
88
    {
89
        $configuration = $this->formObject->getConfiguration();
90
91
        if (false === $configuration->hasField($this->fieldName)) {
92
            throw InvalidConditionException::conditionFieldIsFilledFieldNotFound($this->fieldName);
93
        }
94
    }
95
}
96