Failed Conditions
Pull Request — develop_3.0 (#594)
by
unknown
04:29
created

NumberFormat::setDecimalPlaces()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
namespace Box\Spout\Common\Entity\Style;
4
5
/**
6
 * Class Style
7
 * Represents a style to be applied to a cell
8
 */
9
class NumberFormat
10
{
11
    const TYPE_CURRENCY = 1;
12
    const TYPE_PERCENTAGE = 2;
13
    const TYPE_NUMERIC = 3;
14
15
    const TYPES = [
16
        self::TYPE_CURRENCY,
17
        self::TYPE_PERCENTAGE,
18
        self::TYPE_NUMERIC,
19
    ];
20
21
    private $id;
22
23
    private $type;
24
25
    private $minDecimalPlaces;
26
    private $maxDecimalPlaces;
27
28
    private $currencySymbol;
29
30
    private $commas = true;
31
32
    public function __construct(int $type = null)
33
    {
34
        if (!empty($type)) {
35
            $this->setType($type);
36
        }
37
    }
38
39
    /**
40
     * @param bool $commas
41
     * @return NumberFormat
42
     */
43
    public function setCommas($commas)
44
    {
45
        $this->commas = $commas;
46
        return $this;
47
    }
48
49
    /**
50
     * @param int $type
51
     * @return NumberFormat
52
     */
53
    public function setType($type)
54
    {
55
        if (!in_array($type,self::TYPES)) {
56
            return $this;
57
            //todo throw some exception or something?
58
        }
59
        $this->type = $type;
60
        if ($type == self::TYPE_CURRENCY) {
61
            if (($this->minDecimalPlaces === null) && ($this->maxDecimalPlaces === null)) {
62
                $this->setDecimalPlaces(2,2);
63
            }
64
            if ($this->currencySymbol === null) {
65
                $this->setCurrencySymbol('$');
66
            }
67
        }
68
        if ($type == self::TYPE_PERCENTAGE) {
69
            $this->setCommas(false);
70
        }
71
        return $this;
72
    }
73
74
    /**
75
     * @param int $minDecimalPlaces
0 ignored issues
show
Documentation introduced by
Should the type for parameter $minDecimalPlaces not be null|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
76
     * @param int $maxDecimalPlaces
0 ignored issues
show
Documentation introduced by
Should the type for parameter $maxDecimalPlaces not be null|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
77
     * @return NumberFormat
78
     */
79
    public function setDecimalPlaces(int $minDecimalPlaces = null, int $maxDecimalPlaces = null)
80
    {
81
        $this->minDecimalPlaces = $minDecimalPlaces;
82
        $this->maxDecimalPlaces = $maxDecimalPlaces;
83
        return $this;
84
    }
85
86
    /**
87
     * @param int $currencySymbol
0 ignored issues
show
Documentation introduced by
Should the type for parameter $currencySymbol not be string|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
88
     * @return NumberFormat
89
     */
90
    public function setCurrencySymbol(string $currencySymbol)
91
    {
92
        $this->currencySymbol = $currencySymbol;
93
        return $this;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getFormatCode()
100
    {
101
        //todo spit out xml tag
102
        $formatString = $this->type == self::TYPE_CURRENCY ? '$_' : '';
103
        $formatString .= ($this->commas ? '#,##0' : '#0');
104
        $formatString .= '.';
105
        for ($i = 0; $i < $this->minDecimalPlaces; $i++) {
106
            $formatString .= '0';
107
        }
108
        for ($i = $this->minDecimalPlaces; $i < $this->maxDecimalPlaces; $i++) {
109
            $formatString .= '#';
110
        }
111
        if ($this->type == self::TYPE_PERCENTAGE) {
112
            $formatString .= '%';
113
        }
114
        return $formatString;
115
    }
116
}
117