Passed
Push — main ( e2b5f1...353913 )
by Stefan
02:09
created

FormInt::buildValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 0
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Input element for integer value.
8
 *
9
 * @package Formgenerator
10
 * @author Stefanius <[email protected]>
11
 * @copyright MIT License - see the LICENSE file for details
12
 */
13
class FormInt extends FormInput
14
{
15
    /** @var bool empty entries allowed. If false, empty input is set to '0' */
16
    protected bool $bEmptyAllowed = false;
17
    
18
    /**
19
     * @param string $strName   name of input
20
     * @param int $iSize     size in digits
21
     * @param int $wFlags    flags (default = 0)
22
     */
23
    public function __construct(string $strName, int $iSize, int $wFlags = 0) 
24
    {
25
        parent::__construct($strName, $iSize, $wFlags);
26
    }
27
    
28
    /**
29
     * We 'ask' the configuration for alignment.
30
     * $this->oFG is not available until the parent is set!
31
     */
32
    protected function onParentSet() : void
33
    {
34
        if ($this->oFG->getConfig()->getBool('Integer.RightAlign', true)) {
35
            $this->addFlags(FormFlags::ALIGN_RIGHT);
36
        }
37
        
38
        // if readonly, don't set the 'number' type...
39
        if (!$this->oFlags->isSet(FormFlags::READ_ONLY)) {
40
            $this->strType = 'number';
41
            $this->addStyle('width', $this->size . 'em');
42
        }
43
        $this->addAttribute('data-validation', 'integer:' . ($this->bEmptyAllowed ? 'e' : 'x'));
44
    }
45
    
46
    /**
47
     * Set min/max value accepted by input field. 
48
     * @param int $iMin - set to null, if no limitation wanted
49
     * @param int $iMax - set to null, if no limitation wanted
50
     */
51
    public function setMinMax(?int $iMin, ?int $iMax) : void 
52
    {
53
        if ($iMin !== null) {
54
            $this->addAttribute('min', (string)$iMin);
55
        }
56
        if ($iMax !== null) {
57
            $this->addAttribute('max', (string)$iMax);
58
        }
59
    }
60
    
61
    /**
62
     * {@inheritDoc}
63
     * @see \SKien\Formgenerator\FormElement::buildValue()
64
     */
65
    protected function buildValue() : string
66
    {
67
        $iValue = intval($this->oFG->getData()->getValue($this->strName));
68
        if ($this->oFlags->isSet(FormFlags::NO_ZERO) && $iValue == 0) {
69
            return '';
70
        }
71
        $strHTML = ' value="' . $iValue . '"';
72
        
73
        return $strHTML;
74
    }
75
}
76