mirror of
https://github.com/PHPOffice/PhpSpreadsheet.git
synced 2026-10-14 07:47:14 +00:00
ec4098c8fd
While we might never be able to have 100% of our code strict, we can at the very least do it for all of our tests. This ensures that our tests are using our API with the types as intended by the test author, and not silently be cast to what our API requires.
96 lines
2.6 KiB
PHP
96 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Calculation\Functions;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\CellAddress;
|
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
|
|
|
class FormulaArguments
|
|
{
|
|
/**
|
|
* @var mixed[]
|
|
*/
|
|
protected array $args;
|
|
|
|
public function __construct(mixed ...$args)
|
|
{
|
|
$this->args = $args;
|
|
}
|
|
|
|
public function populateWorksheet(Worksheet $worksheet): string
|
|
{
|
|
$cells = [];
|
|
$cellAddress = new CellAddress('A2');
|
|
foreach ($this->args as $value) {
|
|
if (is_array($value)) {
|
|
// We need to set a matrix in the worksheet
|
|
$worksheet->fromArray($value, null, $cellAddress, true);
|
|
$from = (string) $cellAddress;
|
|
$columns = is_array($value[0]) ? count($value[0]) : count($value);
|
|
$rows = is_array($value[0]) ? count($value) : 1;
|
|
$to = $cellAddress->nextColumn($columns)->nextRow($rows);
|
|
$cells[] = "{$from}:{$to}";
|
|
$columnIncrement = $columns;
|
|
} else {
|
|
$worksheet->setCellValue($cellAddress, $value);
|
|
$cells[] = (string) $cellAddress;
|
|
$columnIncrement = 1;
|
|
}
|
|
$cellAddress = $cellAddress->nextColumn($columnIncrement);
|
|
}
|
|
|
|
return implode(',', $cells);
|
|
}
|
|
|
|
private function matrixRows(array $value): string
|
|
{
|
|
$columns = [];
|
|
foreach ($value as $column) {
|
|
$columns[] = $this->stringify($column);
|
|
}
|
|
|
|
return implode(',', $columns);
|
|
}
|
|
|
|
private function makeMatrix(array $value): string
|
|
{
|
|
$matrix = [];
|
|
foreach ($value as $row) {
|
|
if (is_array($row)) {
|
|
$matrix[] = $this->matrixRows($row);
|
|
} else {
|
|
$matrix[] = $this->stringify($row);
|
|
}
|
|
}
|
|
|
|
return implode(';', $matrix);
|
|
}
|
|
|
|
private function stringify(mixed $value): string
|
|
{
|
|
if (is_array($value)) {
|
|
return '{' . $this->makeMatrix($value) . '}';
|
|
} elseif (null === $value) {
|
|
return '';
|
|
} elseif (is_string($value)) {
|
|
return '"' . str_replace('"', '""', $value) . '"';
|
|
} elseif (is_bool($value)) {
|
|
return $value ? 'TRUE' : 'FALSE';
|
|
}
|
|
|
|
return (string) $value;
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
$args = array_map(
|
|
[self::class, 'stringify'],
|
|
$this->args
|
|
);
|
|
|
|
return implode(',', $args);
|
|
}
|
|
}
|