Passed
Push — main ( f05cfd...29a9c1 )
by Stefan
02:38
created

FormCur::buildValue()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 21
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 21
rs 8.8333
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
    /**
23
     * Creates input field for currency values.
24
     * @param string $strName
25
     * @param int $iSize
26
     * @param int $wFlags    default value = 0
27
     */
28
    public function __construct(string $strName, int $iSize, int $wFlags = 0) 
29
    {
30
        parent::__construct($strName, $iSize, $wFlags);
31
        $this->strValidate = 'aCur';
32
    }
33
    
34
    /**
35
     * We 'ask' the configuration for alignment.
36
     * $this->oFG is not available until the parent is set!
37
     */
38
    protected function onParentSet() : void
39
    {
40
        if ($this->oFG->getConfig()->getBool('Currency.RightAlign', true)) {
41
            $this->addFlags(FormFlags::ALIGN_RIGHT);
42
        }
43
    }
44
    
45
    /**
46
     * {@inheritDoc}
47
     * @see \SKien\Formgenerator\FormElement::buildValue()
48
     */
49
    protected function buildValue() : string
50
    {
51
        $fltValue = floatval($this->oFG->getData()->getValue($this->strName));
52
53
        $li = localeconv();
54
        
55
        if ($this->oFlags->isSet(FormFlags::ADD_CUR)) {
56
            $this->strSuffix = $this->oFG->getConfig()->getString('Currency.Symbol', ($li['currency_symbol'] ?: 'USD'));
57
        }
58
        
59
        if ($this->oFlags->isSet(FormFlags::NO_ZERO) && $fltValue == 0.0) {
60
            return '';
61
        }
62
        
63
        $strDP = $this->oFG->getConfig()->getString('Currency.DecimalPoint', ($li['mon_decimal_point'] ?: '.'));
64
        $strTS = $this->oFG->getConfig()->getString('Currency.ThousandsSep', ($li['mon_thousands_sep'] ?: ','));
65
        
66
        $strValue = number_format($fltValue, 2, $strDP, $strTS);
67
        $strHTML = ' value="' . $strValue . '"';
68
        
69
        return $strHTML;
70
    }
71
}
72