Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
11 / 11 |
XlsxGenerator | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
7 | |
100.00% |
11 / 11 |
createFile | |
100.00% |
1 / 1 |
7 | |
100.00% |
11 / 11 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\FileGenerator\Services\FileGenerator\Generators; |
4 | |
5 | use Qmp\Laravel\FileGenerator\Services\FileGenerator\Exceptions\BadContentStructureException; |
6 | |
7 | class XlsxGenerator extends AbstractGenerator |
8 | { |
9 | /** |
10 | * |
11 | */ |
12 | const DEFAULT_SHEET_NAME = "Feuille sans titre"; |
13 | |
14 | /** |
15 | * @var array|string[] |
16 | */ |
17 | protected array $rules = [ |
18 | 'type' => 'required|in:xlsx', |
19 | 'content' => 'required|array' |
20 | ]; |
21 | |
22 | /** |
23 | * |
24 | */ |
25 | protected function createFile(): void |
26 | { |
27 | $xlsx = new \SimpleXLSXGen; |
28 | |
29 | foreach ($this->config->content as $key => $data) { |
30 | if (!is_array($data) || !is_array($data[0])) { |
31 | throw new BadContentStructureException($this->config->type); |
32 | } |
33 | |
34 | $xlsx->addSheet($data, is_numeric($key) ? self::DEFAULT_SHEET_NAME : $key); |
35 | } |
36 | |
37 | if ($font = $this->config->getOption('font')) { |
38 | $xlsx->setDefaultFont($font); |
39 | } |
40 | |
41 | if ($fontSize = $this->config->getOption('fontSize')) { |
42 | $xlsx->setDefaultFontSize($fontSize); |
43 | } |
44 | |
45 | $xlsx->saveAs($this->config->output_path); |
46 | } |
47 | } |