UnitFormatter   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 75.86%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 4
dl 0
loc 95
ccs 22
cts 29
cp 0.7586
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B format() 0 32 8
A setUnit() 0 11 4
A getUnit() 0 4 1
1
<?php
2
3
/*
4
 * soluble-flexstore library
5
 *
6
 * @author    Vanvelthem Sébastien
7
 * @link      https://github.com/belgattitude/soluble-flexstore
8
 * @copyright Copyright (c) 2016-2017 Vanvelthem Sébastien
9
 * @license   MIT License https://github.com/belgattitude/soluble-flexstore/blob/master/LICENSE.md
10
 *
11
 */
12
13
namespace Soluble\FlexStore\Formatter;
14
15
use Soluble\FlexStore\Exception;
16
use ArrayObject;
17
18
class UnitFormatter extends NumberFormatter
19
{
20
    /**
21
     * @var string|null
22
     */
23
    protected $unit_column;
24
25
    /**
26
     * @var array
27
     */
28
    protected $default_params = [
29
        'decimals' => 2,
30
        'locale' => null,
31
        'pattern' => null,
32
        'unit' => null,
33
        'disableUseOfNonBreakingSpaces' => false,
34
    ];
35
36
    /**
37
     * @throws Exception\ExtensionNotLoadedException if ext/intl is not present
38
     */
39 7
    public function __construct(array $params = [])
40
    {
41 7
        parent::__construct($params);
42 7
    }
43
44
    /**
45
     * Currency format a number.
46
     *
47
     * @throws Exception\RuntimeException
48
     *
49
     * @param float|string|int $number
50
     *
51
     * @return string
52
     */
53 3
    public function format($number, ArrayObject $row = null): string
54
    {
55 3
        $locale = $this->params['locale'];
56
57
        //$formatterId = md5($locale);
58 3
        $formatterId = $locale . (string) $this->params['pattern'];
59
60 3
        if (!array_key_exists($formatterId, $this->formatters)) {
61 3
            $this->loadFormatterId($formatterId);
62
        }
63
64 3
        if ($number !== null && !is_numeric($number)) {
65 2
            $this->throwNumberFormatterException($this->formatters[$formatterId], $number);
66
        }
67
68 1
        if ($this->unit_column !== null) {
69
            if (!isset($row[$this->unit_column])) {
70
                throw new Exception\RuntimeException(__METHOD__ . " Cannot determine unit code based on column '{$this->unit_column}'.");
71
            }
72
            $value = $this->formatters[$formatterId]->format($number) . ' ' . $row[$this->unit_column];
73 1
        } elseif ($this->params['unit'] != '') {
74 1
            $value = $this->formatters[$formatterId]->format($number) . ' ' . $this->params['unit'];
75
        } else {
76
            throw new Exception\RuntimeException(__METHOD__ . ' Unit code must be set prior to use the UnitFormatter');
77
        }
78
79 1
        if (intl_is_failure($this->formatters[$formatterId]->getErrorCode())) {
80
            $this->throwNumberFormatterException($this->formatters[$formatterId], $number);
81
        }
82
83 1
        return $value;
84
    }
85
86
    /**
87
     * @throws Exception\InvalidArgumentException
88
     *
89
     * @param string|RowColumn $unit
90
     *
91
     * @return UnitFormatter
92
     */
93 4
    public function setUnit($unit)
94
    {
95 4
        if ($unit instanceof RowColumn) {
96
            $this->unit_column = $unit->getColumnName();
97 4
        } elseif (!is_string($unit) || trim($unit) == '') {
98
            throw new Exception\InvalidArgumentException(__METHOD__ . ' Unit must be an non empty string (or a RowColumn object)');
99
        }
100 4
        $this->params['unit'] = $unit;
101
102 4
        return $this;
103
    }
104
105
    /**
106
     * @return string
107
     */
108 1
    public function getUnit()
109
    {
110 1
        return $this->params['unit'];
111
    }
112
}
113