FormFloat::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 2
b 1
f 0
cc 1
nc 1
nop 4
1
<?php
2
declare(strict_types=1);
3
4
namespace SKien\Formgenerator;
5
6
/**
7
 * Input field for float value.
8
 * By default the separators are tried to get from the local environment, but
9
 * they can be specified in the configuration.
10
 * The default align of the input field is right - this also can be overwritten
11
 * in the configuration.
12
 *
13
 * @package Formgenerator
14
 * @author Stefanius <[email protected]>
15
 * @copyright MIT License - see the LICENSE file for details
16
*/
17
class FormFloat extends FormInput
18
{
19
    /** @var string decimal point     */
20
    protected string $strDP = '.';
21
    /** @var string thousands separator     */
22
    protected string $strTS = ',';
23
    /** @var int decimal digits     */
24
    protected int $iDec = 2;
25
    /** @var bool empty entries allowed. If false, empty input is set to '0.0' */
26
    protected bool $bEmptyAllowed = false;
27
28
    /**
29
     * Creates input field for float values.
30
     * @param string $strName   Name (if no ID specified, name is used also as ID)
31
     * @param int|string $size  number set the size-attribute, a string is used for the width attribute
32
     * @param int $wFlags       any combination of FormFlag constants
33
     */
34
    public function __construct(string $strName, $size, int $iDecimalPoints, int $wFlags = 0)
35
    {
36
        parent::__construct($strName, $size, $wFlags);
37
        $this->iDec = $iDecimalPoints;
38
    }
39
40
    /**
41
     * {@inheritDoc}
42
     * @see \SKien\Formgenerator\FormElement::fromXML()
43
     * @internal
44
     */
45
    static public function fromXML(\DOMElement $oXMLElement, FormCollection $oFormParent) : ?FormElement
46
    {
47
        $strName = self::getAttribString($oXMLElement, 'name');
48
        $strSize = self::getAttribString($oXMLElement, 'size');
49
        $iDecimalPoints = self::getAttribInt($oXMLElement, 'digits', 1);
50
        $wFlags = self::getAttribFlags($oXMLElement);
51
        $oFormElement = new self($strName, $strSize, $iDecimalPoints, $wFlags);
52
        $oFormParent->add($oFormElement);
53
        $oFormElement->readAdditionalXML($oXMLElement);
54
        return $oFormElement;
55
    }
56
57
    /**
58
     * get format from configuration: <ul>
59
     * <li> right alignment (default: true) </li>
60
     * <li> decimal point and thousands separator (default: from locale settings) </li></ul>
61
     */
62
    protected function onParentSet() : void
63
    {
64
        if ($this->oFG->getConfig()->getBool('Float.RightAlign', true)) {
65
            $this->addFlags(FormFlags::ALIGN_RIGHT);
66
        }
67
68
        $li = localeconv();
69
70
        $this->strDP = $this->oFG->getConfig()->getString('Float.DecimalPoint', ($li['mon_decimal_point'] ?: '.'));
71
        $this->strTS = $this->oFG->getConfig()->getString('Float.ThousandsSep', ($li['mon_thousands_sep'] ?: ','));
72
73
        $this->addAttribute('data-validation', 'float:' . ($this->bEmptyAllowed ? 'e' : 'x') . $this->iDec . $this->strDP . $this->strTS);
74
        $this->setPlaceholder($this->oFG->getConfig()->getString('Float.Placeholder'));
75
    }
76
77
    /**
78
     * {@inheritDoc}
79
     * @see \SKien\Formgenerator\FormElement::buildValue()
80
     */
81
    protected function buildValue() : string
82
    {
83
        $fltValue = floatval($this->oFG->getData()->getValue($this->strName));
84
85
        if ($this->oFlags->isSet(FormFlags::NO_ZERO) && $fltValue == 0) {
86
            return '';
87
        }
88
89
        $strValue = number_format($fltValue, $this->iDec, $this->strDP, $this->strTS);
90
        $strHTML = ' value="' . $strValue . '"';
91
92
        return $strHTML;
93
    }
94
}
95