Passed
Push — main ( 650a85...fac3c3 )
by Stefan
02:13
created

FormCur::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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