FormInt   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 27
dl 0
loc 88
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A buildValue() 0 9 3
A setMinMax() 0 7 3
A onParentSet() 0 12 4
A fromXML() 0 9 1
A readAdditionalXML() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Input element for integer value.
8
 * The default align of the input field is right - this can be overwritten
9
 * in the configuration.
10
 *
11
 * @package Formgenerator
12
 * @author Stefanius <[email protected]>
13
 * @copyright MIT License - see the LICENSE file for details
14
 */
15
class FormInt extends FormInput
16
{
17
    /** @var bool empty entries allowed. If false, empty input is set to '0' */
18
    protected bool $bEmptyAllowed = false;
19
20
    /**
21
     * @param string $strName       Name of input
22
     * @param int|string $iSize     integer value set the size-attribute, a string is used for the width attribute
23
     * @param int $wFlags           any combination of FormFlag constants
24
     */
25
    public function __construct(string $strName, $iSize, int $wFlags = 0)
26
    {
27
        parent::__construct($strName, $iSize, $wFlags);
28
    }
29
30
    /**
31
     * {@inheritDoc}
32
     * @see \SKien\Formgenerator\FormElement::fromXML()
33
     * @internal
34
     */
35
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
36
    {
37
        $strName = self::getAttribString($oXMLElement, 'name');
38
        $strSize = self::getAttribString($oXMLElement, 'size');
39
        $wFlags = self::getAttribFlags($oXMLElement);
40
        $oFormElement = new self($strName, $strSize, $wFlags);
41
        $oFormParent->add($oFormElement);
42
        $oFormElement->readAdditionalXML($oXMLElement);
43
        return $oFormElement;
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     * @see \SKien\Formgenerator\FormElement::readAdditionalXML()
49
     * @internal
50
     */
51
    public function readAdditionalXML(\DOMElement $oXMLElement) : void
52
    {
53
        parent::readAdditionalXML($oXMLElement);
54
        $this->setMinMax(self::getAttribInt($oXMLElement, 'min'), self::getAttribInt($oXMLElement, 'max'));
55
    }
56
57
    /**
58
     * We 'ask' the configuration for alignment.
59
     * $this->oFG is not available until the parent is set!
60
     */
61
    protected function onParentSet() : void
62
    {
63
        if ($this->oFG->getConfig()->getBool('Integer.RightAlign', true)) {
64
            $this->addFlags(FormFlags::ALIGN_RIGHT);
65
        }
66
67
        // if readonly, don't set the 'number' type...
68
        if (!$this->oFlags->isSet(FormFlags::READ_ONLY)) {
69
            $this->strType = 'number';
70
            $this->addStyle('width', $this->size . 'em');
71
        }
72
        $this->addAttribute('data-validation', 'integer:' . ($this->bEmptyAllowed ? 'e' : 'x'));
73
    }
74
75
    /**
76
     * Set min/max value accepted by input field.
77
     * @param int $iMin - set to null, if no limitation wanted
78
     * @param int $iMax - set to null, if no limitation wanted
79
     */
80
    public function setMinMax(?int $iMin, ?int $iMax) : void
81
    {
82
        if ($iMin !== null) {
83
            $this->addAttribute('min', (string)$iMin);
84
        }
85
        if ($iMax !== null) {
86
            $this->addAttribute('max', (string)$iMax);
87
        }
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     * @see \SKien\Formgenerator\FormElement::buildValue()
93
     */
94
    protected function buildValue() : string
95
    {
96
        $iValue = intval($this->oFG->getData()->getValue($this->strName));
97
        if ($this->oFlags->isSet(FormFlags::NO_ZERO) && $iValue == 0) {
98
            return '';
99
        }
100
        $strHTML = ' value="' . $iValue . '"';
101
102
        return $strHTML;
103
    }
104
}
105