Passed
Push — main ( c21323...650a85 )
by Stefan
02:47
created

FormCur::onParentSet()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 16
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 0
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
    
29
    /**
30
     * Creates input field for currency 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 $wFlags = 0) 
36
    {
37
        parent::__construct($strName, $iSize, $wFlags);
38
    }
39
    
40
    /**
41
     * get format from configuration: <ul>
42
     * <li> right alignment (default: true) </li>
43
     * <li> currency symbol (default: from locale settings) </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('Currency.RightAlign', true)) {
49
            $this->addFlags(FormFlags::ALIGN_RIGHT);
50
        }
51
        
52
        $li = localeconv();
53
        
54
        if ($this->oFlags->isSet(FormFlags::ADD_CUR)) {
55
            $this->strSuffix = $this->oFG->getConfig()->getString('Currency.Symbol', ($li['currency_symbol'] ?: 'USD'));
56
        }
57
        
58
        $this->strDP = $this->oFG->getConfig()->getString('Currency.DecimalPoint', ($li['mon_decimal_point'] ?: '.'));
59
        $this->strTS = $this->oFG->getConfig()->getString('Currency.ThousandsSep', ($li['mon_thousands_sep'] ?: ','));
60
        
61
        $this->addAttribute('data-validation', 'cur:' . $this->strTS . $this->strDP . $this->iDec);
62
    }
63
    
64
    /**
65
     * {@inheritDoc}
66
     * @see \SKien\Formgenerator\FormElement::buildValue()
67
     */
68
    protected function buildValue() : string
69
    {
70
        $fltValue = floatval($this->oFG->getData()->getValue($this->strName));
71
72
        if ($this->oFlags->isSet(FormFlags::NO_ZERO) && $fltValue == 0.0) {
73
            return '';
74
        }
75
        
76
        $strValue = number_format($fltValue, 2, $this->strDP, $this->strTS);
77
        $strHTML = ' value="' . $strValue . '"';
78
        
79
        return $strHTML;
80
    }
81
}
82