Completed
Push — wip/steps ( bd4be0...a4f896 )
by Romain
02:56
created

FieldIsFilledCondition   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 70
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCssResult() 0 6 1
A getJavaScriptResult() 0 4 1
A getPhpResult() 0 6 1
A checkConditionConfiguration() 0 6 2
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 Romm\Formz\Form\Definition\FormDefinition;
21
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
22
23
/**
24
 * This condition will match when a field is filled with any value.
25
 */
26
class FieldIsFilledCondition extends AbstractConditionItem
27
{
28
    const CONDITION_IDENTIFIER = 'fieldIsFilled';
29
30
    /**
31
     * @inheritdoc
32
     * @var array
33
     */
34
    protected static $javaScriptFiles = [
35
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsFilled.js'
36
    ];
37
38
    /**
39
     * @var string
40
     * @validate NotEmpty
41
     */
42
    protected $fieldName;
43
44
    /**
45
     * @param string $fieldName
46
     */
47
    public function __construct($fieldName)
48
    {
49
        $this->fieldName = $fieldName;
50
    }
51
52
    /**
53
     * @inheritdoc
54
     */
55
    public function getCssResult()
56
    {
57
        $valueKey = DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName);
58
59
        return '[' . $valueKey . ']:not([' . $valueKey . '=""])';
60
    }
61
62
    /**
63
     * @inheritdoc
64
     */
65
    public function getJavaScriptResult()
66
    {
67
        return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]);
68
    }
69
70
    /**
71
     * @inheritdoc
72
     */
73
    public function getPhpResult(PhpConditionDataObject $dataObject)
74
    {
75
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
76
77
        return !empty($value);
78
    }
79
80
    /**
81
     * Checks the condition configuration/options.
82
     *
83
     * If any syntax/configuration error is found, an exception of type
84
     * `InvalidConditionException` must be thrown.
85
     *
86
     * @param FormDefinition $formDefinition
87
     * @throws InvalidConditionException
88
     */
89
    protected function checkConditionConfiguration(FormDefinition $formDefinition)
90
    {
91
        if (false === $formDefinition->hasField($this->fieldName)) {
92
            throw InvalidConditionException::conditionFieldIsFilledFieldNotFound($this->fieldName);
93
        }
94
    }
95
}
96