Completed
Push — development ( e4a9bd...c22d5a )
by Romain
01:58
created

FieldCountValuesCondition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
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\Condition\Exceptions\InvalidConditionException;
17
use Romm\Formz\Condition\Processor\DataObject\PhpConditionDataObject;
18
use TYPO3\CMS\Extbase\Reflection\ObjectAccess;
19
20
/**
21
 * This condition will match when a field has a number of selected items between
22
 * a given minimum and maximum.
23
 */
24
class FieldCountValuesCondition extends AbstractConditionItem
25
{
26
    const CONDITION_IDENTIFIER = 'fieldCountValues';
27
28
    /**
29
     * @inheritdoc
30
     * @var array
31
     */
32
    protected static $javaScriptFiles = [
33
        'EXT:formz/Resources/Public/JavaScript/Conditions/Formz.Condition.FieldCountValues.js'
34
    ];
35
36
    /**
37
     * @var string
38
     * @validate NotEmpty
39
     */
40
    protected $fieldName;
41
42
    /**
43
     * @var int
44
     */
45
    protected $minimum;
46
47
    /**
48
     * @var int
49
     */
50
    protected $maximum;
51
52
    /**
53
     * @param string $fieldName
54
     * @param int $minimum
55
     * @param int $maximum
56
     */
57
    public function __construct($fieldName, $minimum = null, $maximum = null)
58
    {
59
        $this->fieldName = $fieldName;
60
        $this->minimum = $minimum;
61
        $this->maximum = $maximum;
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function getJavaScriptResult()
68
    {
69
        return $this->getDefaultJavaScriptCall([
70
            'fieldName'  => $this->fieldName,
71
            'minimum' => $this->minimum,
72
            'maximum' => $this->maximum
73
        ]);
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function getPhpResult(PhpConditionDataObject $dataObject)
80
    {
81
        $value = ObjectAccess::getProperty($dataObject->getForm(), $this->fieldName);
82
83
        return !($this->minimum && count($value) < (int)$this->minimum)
84
            && !($this->maximum && count($value) > (int)$this->maximum);
85
    }
86
87
    /**
88
     * @inheritdoc
89
     *
90
     * @throws InvalidConditionException
91
     */
92
    protected function checkConditionConfiguration()
93
    {
94
        $configuration = $this->formObject->getConfiguration();
95
96
        if (false === $configuration->hasField($this->fieldName)) {
97
            throw InvalidConditionException::conditionFieldCountValuesFieldNotFound($this->fieldName);
98
        }
99
    }
100
101
    /**
102
     * @return string
103
     */
104
    public function getCssResult()
105
    {
106
        return '';
107
    }
108
}
109