Completed
Push — wip/steps ( 995792...c65172 )
by Romain
04:50
created

FieldCountValuesCondition::getPhpResult()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 5
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\Condition\Exceptions\InvalidConditionException;
17
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
18
use Romm\Formz\Form\Definition\FormDefinition;
19
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
20
21
/**
22
 * This condition will match when a field has a number of selected items between
23
 * a given minimum and maximum.
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 int
45
     */
46
    protected $minimum;
47
48
    /**
49
     * @var int
50
     */
51
    protected $maximum;
52
53
    /**
54
     * @param string $fieldName
55
     * @param int $minimum
56
     * @param int $maximum
57
     */
58
    public function __construct($fieldName, $minimum = null, $maximum = null)
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
        return !($this->minimum && count($value) < (int)$this->minimum)
85
            && !($this->maximum && count($value) > (int)$this->maximum);
86
    }
87
88
    /**
89
     * @inheritdoc
90
     *
91
     * @throws InvalidConditionException
92
     */
93
    protected function checkConditionConfiguration(FormDefinition $formDefinition)
94
    {
95
        if (false === $formDefinition->hasField($this->fieldName)) {
96
            throw InvalidConditionException::conditionFieldCountValuesFieldNotFound($this->fieldName);
97
        }
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getCssResult()
104
    {
105
        return '';
106
    }
107
}
108