Completed
Pull Request — wip/steps (#85)
by Philippe
04:02
created

FieldCountValuesCondition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 3
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 Romm\Formz\Form\Definition\FormDefinition;
20
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
21
22
/**
23
 * This condition will match when a field has the given value.
24
 */
25
class FieldCountValuesCondition extends AbstractConditionItem
26
{
27
    const CONDITION_IDENTIFIER = 'fieldCountValues';
28
29
    /**
30
     * @inheritdoc
31
     * @var array
32
     */
33
    protected static $javaScriptFiles = [
34
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldCountValues.js'
35
    ];
36
37
    /**
38
     * @var string
39
     * @validate NotEmpty
40
     */
41
    protected $fieldName;
42
43
    /**
44
     * @var string
45
     */
46
    protected $minimum;
47
48
    /**
49
     * @var string
50
     */
51
    protected $maximum;
52
53
    /**
54
     * @param string $fieldName
55
     * @param string $minimum
56
     * @param string $maximum
57
     */
58
    public function __construct($fieldName, $minimum, $maximum)
59
    {
60
        $this->fieldName = $fieldName;
61
        $this->minimum = $minimum;
62
        $this->maximum = $maximum;
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function getJavaScriptResult()
69
    {
70
        return $this->getDefaultJavaScriptCall([
71
            'fieldName'  => $this->fieldName,
72
            'minimum' => $this->minimum,
73
            'maximum' => $this->maximum
74
        ]);
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function getPhpResult(PhpConditionDataObject $dataObject)
81
    {
82
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
83
84
        if (isset($this->minimum) && count($value) < $this->minimum) {
85
            return false;
86
        }
87
88
        if (isset($this->maximum) && count($value) > $this->maximum) {
89
            return false;
90
        }
91
92
        return true;
93
    }
94
95
    /**
96
     * Checks the condition configuration/options.
97
     *
98
     * If any syntax/configuration error is found, an exception of type
99
     * `InvalidConditionException` must be thrown.
100
     *
101
     * @param FormDefinition $formDefinition
102
     * @throws InvalidConditionException
103
     */
104
    protected function checkConditionConfiguration(FormDefinition $formDefinition)
105
    {
106
        if (false === $formDefinition->hasField($this->fieldName)) {
107
            throw InvalidConditionException::conditionFieldCountValuesFieldNotFound($this->fieldName);
108
        }
109
    }
110
111
    /**
112
     * @return string
113
     */
114
    public function getCssResult()
115
    {
116
        // TODO: Implement getCssResult() method.
117
    }
118
}
119