1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\CellAddress; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; |
7
|
|
|
|
8
|
|
|
class FormulaArguments |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var mixed[] |
12
|
|
|
*/ |
13
|
|
|
protected array $args; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param mixed ...$args |
17
|
|
|
*/ |
18
|
|
|
public function __construct(...$args) |
19
|
|
|
{ |
20
|
|
|
$this->args = $args; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function populateWorksheet(Worksheet $worksheet): string |
24
|
|
|
{ |
25
|
|
|
$cells = []; |
26
|
|
|
$cellAddress = new CellAddress('A2'); |
27
|
|
|
foreach ($this->args as $value) { |
28
|
|
|
if (is_array($value)) { |
29
|
|
|
// We need to set a matrix in the worksheet |
30
|
|
|
$worksheet->fromArray($value, null, $cellAddress, true); |
31
|
|
|
$from = (string) $cellAddress; |
32
|
|
|
$columns = is_array($value[0]) ? count($value[0]) : count($value); |
33
|
|
|
$rows = is_array($value[0]) ? count($value) : 1; |
34
|
|
|
$to = $cellAddress->nextColumn($columns)->nextRow($rows); |
35
|
|
|
$cells[] = "{$from}:{$to}"; |
36
|
|
|
$columnIncrement = $columns; |
37
|
|
|
} else { |
38
|
|
|
$worksheet->setCellValue($cellAddress, $value); |
39
|
|
|
$cells[] = (string) $cellAddress; |
40
|
|
|
$columnIncrement = 1; |
41
|
|
|
} |
42
|
|
|
$cellAddress = $cellAddress->nextColumn($columnIncrement); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return implode(',', $cells); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param mixed $value |
50
|
|
|
*/ |
51
|
|
|
private function matrixRows($value): string |
52
|
|
|
{ |
53
|
|
|
$columns = []; |
54
|
|
|
foreach ($value as $column) { |
55
|
|
|
$columns[] = $this->stringify($column); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return implode(',', $columns); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param mixed $value |
63
|
|
|
*/ |
64
|
|
|
private function makeMatrix($value): string |
65
|
|
|
{ |
66
|
|
|
$matrix = []; |
67
|
|
|
foreach ($value as $row) { |
68
|
|
|
if (is_array($row)) { |
69
|
|
|
$matrix[] = $this->matrixRows($row); |
70
|
|
|
} else { |
71
|
|
|
$matrix[] = $this->stringify($row); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return implode(';', $matrix); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param mixed $value |
80
|
|
|
*/ |
81
|
|
|
private function stringify($value): string |
82
|
|
|
{ |
83
|
|
|
if (is_array($value)) { |
84
|
|
|
return '{' . $this->makeMatrix($value) . '}'; |
85
|
|
|
} elseif (null === $value) { |
86
|
|
|
return ''; |
87
|
|
|
} elseif (is_string($value)) { |
88
|
|
|
return '"' . str_replace('"', '""', $value) . '"'; |
89
|
|
|
} elseif (is_bool($value)) { |
90
|
|
|
return $value ? 'TRUE' : 'FALSE'; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return (string) $value; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function __toString(): string |
97
|
|
|
{ |
98
|
|
|
$args = array_map( |
99
|
|
|
[self::class, 'stringify'], |
100
|
|
|
$this->args |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
return implode(',', $args); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|