Format   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 183
Duplicated Lines 0 %

Test Coverage

Coverage 94.92%

Importance

Changes 0
Metric Value
eloc 95
dl 0
loc 183
ccs 56
cts 59
cp 0.9492
rs 10
c 0
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getBuiltInCodes() 0 45 1
A getId() 0 3 1
A getBuiltInCodeById() 0 3 1
A getCode() 0 3 1
A properties() 0 3 1
A getBuiltInCodeIdByCode() 0 3 1
A asXML() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Eclipxe\XlsxExporter\Styles;
6
7
use Eclipxe\XlsxExporter\Utils\XmlConverter;
8
9
/**
10
 * @property int $id numeric identification for the format
11
 * @property string $code format code
12
 *
13
 * References:
14
 * http://msdn.microsoft.com/en-us/library/documentformat.openxml.spreadsheet.numberingformat%28v=office.14%29.aspx
15
 * https://github.com/PHPOffice/PHPExcel/blob/develop/Classes/PHPExcel/Style/NumberFormat.php
16
 */
17
class Format extends AbstractStyle
18
{
19
    public const FORMAT_GENERAL = 'General';
20
21
    public const FORMAT_TEXT = '@';
22
23
    public const FORMAT_NUMBER = '0';
24
25
    public const FORMAT_NUMBER_00 = '0.00';
26
27
    public const FORMAT_NUMBER_COMMA_SEPARATED1 = '#,##0.00';
28
29
    public const FORMAT_NUMBER_COMMA_SEPARATED2 = '#,##0.00_-';
30
31
    public const FORMAT_PERCENTAGE = '0%';
32
33
    public const FORMAT_PERCENTAGE_00 = '0.00%';
34
35
    public const FORMAT_DATE_YYYYMMDD2 = 'yyyy-mm-dd';
36
37
    public const FORMAT_DATE_YYYYMMDD = 'yy-mm-dd';
38
39
    public const FORMAT_DATE_DDMMYYYY = 'dd/mm/yy';
40
41
    public const FORMAT_DATE_DMYSLASH = 'd/m/yy';
42
43
    public const FORMAT_DATE_DMYMINUS = 'd-m-yy';
44
45
    public const FORMAT_DATE_DMMINUS = 'd-m';
46
47
    public const FORMAT_DATE_MYMINUS = 'm-yy';
48
49
    public const FORMAT_DATE_XLSX14 = 'mm-dd-yy';
50
51
    public const FORMAT_DATE_XLSX15 = 'd-mmm-yy';
52
53
    public const FORMAT_DATE_XLSX16 = 'd-mmm';
54
55
    public const FORMAT_DATE_XLSX17 = 'mmm-yy';
56
57
    public const FORMAT_DATE_XLSX22 = 'm/d/yy h:mm';
58
59
    public const FORMAT_DATE_DATETIME = 'd/m/yy h:mm';
60
61
    public const FORMAT_DATE_TIME1 = 'h:mm AM/PM';
62
63
    public const FORMAT_DATE_TIME2 = 'h:mm:ss AM/PM';
64
65
    public const FORMAT_DATE_TIME3 = 'h:mm';
66
67
    public const FORMAT_DATE_TIME4 = 'h:mm:ss';
68
69
    public const FORMAT_DATE_TIME5 = 'mm:ss';
70
71
    public const FORMAT_DATE_TIME6 = 'h:mm:ss';
72
73
    public const FORMAT_DATE_TIME7 = 'm:s.000000';
74
75
    public const FORMAT_DATE_TIME8 = 'h:mm:ss;@';
76
77
    public const FORMAT_DATE_YYYYMMDDSLASH = 'yy/mm/dd;@';
78
79
    public const FORMAT_CURRENCY_USD_SIMPLE = '"$"#,##0.00_-';
80
81
    public const FORMAT_CURRENCY_USD = '$#,##0_-';
82
83
    public const FORMAT_CURRENCY_EUR_SIMPLE = '[$EUR ]#,##0.00_-';
84
85
    public const FORMAT_ACCOUNTING_00 = '_-"$"* #,##0.00_-;\-"$"* #,##0.00_-;_-"$"* "-"??_-;_-@_-';
86
87
    public const FORMAT_ACCOUNTING = '_-"$"* #,##0_-;\-"$"* #,##0_-;_-"$"* "-"??_-;_-@_-';
88
89
    // custom formats
90
    public const FORMAT_COMMA_0DECS = '#,##0';
91
92
    public const FORMAT_COMMA_2DECS = '#,##0.00';
93
94
    public const FORMAT_ZERO_0DECS = '0';
95
96
    public const FORMAT_ZERO_2DECS = '0.00';
97
98
    public const FORMAT_YESNO = '"YES";"YES";"NO"';
99
100
    public const FORMAT_DATE_YMDHM = 'yyyy\-mm\-dd\ hh:mm:ss';
101
102
    public const FORMAT_DATE_YMD = 'yyyy\-mm\-dd';
103
104
    public const FORMAT_DATE_HM = 'hh:mm';
105
106 10
    protected function properties(): array
107
    {
108 10
        return ['id', 'code'];
109
    }
110
111 4
    public function asXML(): string
112
    {
113 4
        if ('' === $this->code) {
114
            return '';
115
        }
116 4
        return sprintf('<numFmt numFmtId="%d" formatCode="%s"/>', $this->id, XmlConverter::parse($this->code));
117
    }
118
119
    /**
120
     * This method forces $format->int to be always a string
121
     */
122 5
    public function getId(): int
123
    {
124 5
        return intval($this->data['id'] ?? 0);
125
    }
126
127
    /**
128
     * This method forces $format->code to be always a string
129
     */
130 5
    public function getCode(): string
131
    {
132 5
        return strval($this->data['code'] ?? '');
133
    }
134
135
    /** @return array<int, string> */
136 4
    public static function getBuiltInCodes(): array
137
    {
138 4
        return [
139 4
            0 => self::FORMAT_GENERAL,
140 4
            1 => self::FORMAT_NUMBER,
141 4
            2 => self::FORMAT_NUMBER_00,
142 4
            3 => '#,##0',
143 4
            4 => '#,##0.00',
144 4
            9 => '0%',
145 4
            10 => '0.00%',
146 4
            11 => '0.00E+00',
147 4
            12 => '# ?/?',
148 4
            13 => '# ??/??',
149 4
            14 => 'mm-dd-yy',
150 4
            15 => 'd-mmm-yy',
151 4
            16 => 'd-mmm',
152 4
            17 => 'mmm-yy',
153 4
            18 => 'h:mm AM/PM',
154 4
            19 => 'h:mm:ss AM/PM',
155 4
            20 => 'h:mm',
156 4
            21 => 'h:mm:ss',
157 4
            22 => 'm/d/yy h:mm',
158 4
            37 => '#,##0 ;(#,##0)',
159 4
            38 => '#,##0 ;[Red](#,##0)',
160 4
            39 => '#,##0.00;(#,##0.00)',
161 4
            40 => '#,##0.00;[Red](#,##0.00)',
162 4
            44 => self::FORMAT_ACCOUNTING_00,
163 4
            45 => 'mm:ss',
164 4
            46 => '[h]:mm:ss',
165 4
            47 => 'mmss.0',
166 4
            48 => '##0.0E+0',
167 4
            49 => '@',
168 4
            27 => '[$-404]e/m/d',
169 4
            30 => 'm/d/yy',
170 4
            36 => '[$-404]e/m/d',
171 4
            50 => '[$-404]e/m/d',
172 4
            57 => '[$-404]e/m/d',
173 4
            59 => 't0',
174 4
            60 => 't0.00',
175 4
            61 => 't#,##0',
176 4
            62 => 't#,##0.00',
177 4
            67 => 't0%',
178 4
            68 => 't0.00%',
179 4
            69 => 't# ?/?',
180 4
            70 => 't# ??/??',
181 4
        ];
182
    }
183
184
    /**
185
     * Get the code for a built-in id, if not found return an empty string
186
     */
187
    public static function getBuiltInCodeById(int $id): string
188
    {
189
        return static::getBuiltInCodes()[$id] ?? '';
190
    }
191
192
    /**
193
     * Get the id for a built-in code, if not found return false
194
     *
195
     * @return int|false
196
     */
197 4
    public static function getBuiltInCodeIdByCode(string $code)
198
    {
199 4
        return array_search($code, static::getBuiltInCodes(), true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return array_search($cod...etBuiltInCodes(), true) also could return the type string which is incompatible with the documented return type false|integer.
Loading history...
200
    }
201
}
202