Passed
Push — main ( fac3c3...502827 )
by Stefan
02:24
created

FormFloat   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 20
dl 0
loc 59
rs 10
c 1
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A onParentSet() 0 13 5
A __construct() 0 4 1
A buildValue() 0 12 3
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Input field for float value.
8
 * - always right aligned
9
 * 
10
 * #### History
11
 * - *2021-01-30*   initial version
12
 *
13
 * @package Formgenerator
14
 * @version 1.1.0
15
 * @author Stefanius <[email protected]>
16
 * @copyright MIT License - see the LICENSE file for details
17
*/
18
class FormFloat extends FormInput
19
{
20
    /** @var string decimal point     */
21
    protected string $strDP = '.';
22
    /** @var string thousands separator     */
23
    protected string $strTS = ',';
24
    /** @var int decimal digits     */
25
    protected int $iDec = 2;
26
    /** @var bool empty entries allowed. If false, empty input is set to '0.0' */
27
    protected bool $bEmptyAllowed = false;
28
    
29
    /**
30
     * Creates input field for float values.
31
     * @param string $strName
32
     * @param int $iSize
33
     * @param int $wFlags    default value = 0
34
     */
35
    public function __construct(string $strName, int $iSize, int $iDecimalPoints, int $wFlags = 0) 
36
    {
37
        parent::__construct($strName, $iSize, $wFlags);
38
        $this->iDec = $iDecimalPoints;
39
    }
40
    
41
    /**
42
     * get format from configuration: <ul>
43
     * <li> right alignment (default: true) </li>
44
     * <li> decimal point and thousands separator (default: from locale settings) </li></ul>
45
     */
46
    protected function onParentSet() : void
47
    {
48
        if ($this->oFG->getConfig()->getBool('Float.RightAlign', true)) {
49
            $this->addFlags(FormFlags::ALIGN_RIGHT);
50
        }
51
        
52
        $li = localeconv();
53
        
54
        $this->strDP = $this->oFG->getConfig()->getString('Float.DecimalPoint', ($li['mon_decimal_point'] ?: '.'));
55
        $this->strTS = $this->oFG->getConfig()->getString('Float.ThousandsSep', ($li['mon_thousands_sep'] ?: ','));
56
        
57
        $this->addAttribute('data-validation', 'float:' . ($this->bEmptyAllowed ? 'e' : 'x') . $this->iDec . $this->strDP . $this->strTS);
58
        $this->setPlaceholder($this->oFG->getConfig()->getString('Float.Placeholder'));
59
    }
60
    
61
    /**
62
     * {@inheritDoc}
63
     * @see \SKien\Formgenerator\FormElement::buildValue()
64
     */
65
    protected function buildValue() : string
66
    {
67
        $fltValue = floatval($this->oFG->getData()->getValue($this->strName));
68
69
        if ($this->oFlags->isSet(FormFlags::NO_ZERO) && $fltValue == 0) {
70
            return '';
71
        }
72
        
73
        $strValue = number_format($fltValue, $this->iDec, $this->strDP, $this->strTS);
74
        $strHTML = ' value="' . $strValue . '"';
75
        
76
        return $strHTML;
77
    }
78
}
79