Completed
Pull Request — development (#84)
by
unknown
02:39
created

FieldIsNotEmptyCondition::getJavaScriptResult()   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 0
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 valid (its validation returned no
25
 * error).
26
 */
27
class FieldIsNotEmptyCondition extends AbstractConditionItem
28
{
29
    const CONDITION_IDENTIFIER = 'fieldIsNotEmpty';
30
31
    /**
32
     * @inheritdoc
33
     * @var array
34
     */
35
    protected static $javaScriptFiles = [
36
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldIsNotEmpty.js'
37
    ];
38
39
    /**
40
     * @var string
41
     * @validate NotEmpty
42
     */
43
    protected $fieldName;
44
45
    /**
46
     * @param string $fieldName
47
     */
48
    public function __construct($fieldName)
49
    {
50
        $this->fieldName = $fieldName;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56
    public function getCssResult()
57
    {
58
        $valueKey = DataAttributesAssetHandler::getFieldDataValueKey($this->fieldName);
59
60
        return '[' . $valueKey . ']:not([' . $valueKey . '=""])';
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function getJavaScriptResult()
67
    {
68
        return $this->getDefaultJavaScriptCall(['fieldName' => $this->fieldName]);
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function getPhpResult(PhpConditionDataObject $dataObject)
75
    {
76
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
77
78
        return !empty($value);
79
    }
80
81
    /**
82
     * Checks the condition configuration/options.
83
     *
84
     * If any syntax/configuration error is found, an exception of type
85
     * `InvalidConditionException` must be thrown.
86
     *
87
     * @param FormDefinition $formDefinition
88
     * @throws InvalidConditionException
89
     */
90
    protected function checkConditionConfiguration(FormDefinition $formDefinition)
91
    {
92
        if (false === $formDefinition->hasField($this->fieldName)) {
93
            throw InvalidConditionException::conditionFieldIsNotEmptyFieldNotFound($this->fieldName);
0 ignored issues
show
Bug introduced by
The method conditionFieldIsNotEmptyFieldNotFound() does not exist on Romm\Formz\Condition\Exc...validConditionException. Did you maybe mean conditionFieldIsEmptyFieldNotFound()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
94
        }
95
    }
96
}
97