InputMaskCurrencyField   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 28
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setValue() 0 7 4
A performReadonlyTransformation() 0 5 1
A __construct() 0 5 1
1
<?php
2
3
namespace LeKoala\FormElements;
4
5
/**
6
 * Format currency
7
 * "currency": {
8
 * prefix: "", //"$ ",
9
 * groupSeparator: ",",
10
 * alias: "numeric",
11
 * digits: 2,
12
 * digitsOptional: false
13
 * },
14
 */
15
class InputMaskCurrencyField extends InputMaskNumericField
16
{
17
    use CurrencyFormatter;
18
19
    public function __construct($name, $title = null, $value = null)
20
    {
21
        parent::__construct($name, $title, $value);
22
        $this->setAlias(self::ALIAS_CURRENCY);
23
        $this->setPrefix($this->getCurrencySymbol() . ' ');
24
    }
25
26
    public function setValue($value, $data = null)
27
    {
28
        // otherwise values like 84.4 will be interpreted as 844.00
29
        if ($value !== null && is_float($value) && strlen($value)) {
30
            $value = number_format($value, 2, $this->getCurrencyDecimalSeparator(), "");
31
        }
32
        return parent::setValue($value, $data);
33
    }
34
35
    /**
36
     * Create a new class for this field
37
     */
38
    public function performReadonlyTransformation()
39
    {
40
        $field = $this->castedCopy(InputMaskCurrencyField::class);
41
        $field->setReadonly(true);
42
        return $field;
43
    }
44
}
45