Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
68.18% covered (warning)
68.18%
15 / 22
37.50% covered (danger)
37.50%
3 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Decimal
68.18% covered (warning)
68.18%
15 / 22
37.50% covered (danger)
37.50%
3 / 8
20.31
0.00% covered (danger)
0.00%
0 / 1
 __construct
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
2.03
 __toString
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 unwrap
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 localize
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 currency
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 json
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFormatter
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Cosray\Value;
6
7use Cosray\Field\Field;
8use Cosray\Field\Owner;
9use NumberFormatter;
10
11class Decimal extends Value
12{
13    public readonly ?float $value;
14
15    public function __construct(Owner $owner, Field $field, ValueContext $context)
16    {
17        parent::__construct($owner, $field, $context);
18
19        $value = $this->value();
20
21        if (is_numeric($value)) {
22            $this->value = floatval($value);
23        } else {
24            $this->value = null;
25        }
26    }
27
28    public function __toString(): string
29    {
30        if ($this->value === null) {
31            return '';
32        }
33
34        return (string) $this->value;
35    }
36
37    public function unwrap(): ?float
38    {
39        return $this->value;
40    }
41
42    public function isset(): bool
43    {
44        return isset($this->value) ? true : false;
45    }
46
47    public function localize(?int $digits = 2, ?string $locale = null): string
48    {
49        if ($this->value) {
50            $formatter = $this->getFormatter(NumberFormatter::DECIMAL, $digits, $locale);
51
52            return $formatter->format($this->value);
53        }
54
55        return '';
56    }
57
58    public function currency(string $currency, ?int $digits = 2, ?string $locale = null): string
59    {
60        if ($this->value) {
61            $formatter = $this->getFormatter(NumberFormatter::CURRENCY, $digits, $locale);
62
63            return $formatter->formatCurrency($this->value, $currency);
64        }
65
66        return '';
67    }
68
69    public function json(): mixed
70    {
71        return $this->value;
72    }
73
74    protected function getFormatter(int $style, int $digits, ?string $locale = null): NumberFormatter
75    {
76        $formatter = new NumberFormatter($locale ?: $this->locale->id, $style);
77        $formatter->setAttribute(NumberFormatter::MIN_FRACTION_DIGITS, $digits);
78
79        return $formatter;
80    }
81}