$row !== '' )); } if ($columnDelimiter !== null) { $delimiter = self::buildDelimiter($columnDelimiter); array_walk( $rows, function (&$row) use ($delimiter, $flags, $ignoreEmpty): void { $row = ($delimiter === '()') ? [$row] : preg_split("/{$delimiter}/{$flags}", $row); /** @var array $row */ if ($ignoreEmpty === true) { $row = array_values(array_filter( $row, fn ($value): bool => $value !== '' )); } } ); if ($ignoreEmpty === true) { $rows = array_values(array_filter( $rows, fn ($row): bool => $row !== [] && $row !== [''] )); } } return self::applyPadding($rows, $padding); } private static function applyPadding(array $rows, mixed $padding): array { $columnCount = array_reduce( $rows, fn (int $counter, array $row): int => max($counter, count($row)), 0 ); return array_map( function (array $row) use ($columnCount, $padding): array { return (count($row) < $columnCount) ? array_merge($row, array_fill(0, $columnCount - count($row), $padding)) : $row; }, $rows ); } /** * @param null|array|string $delimiter the text that marks the point before which you want to split * Multiple delimiters can be passed as an array of string values */ private static function buildDelimiter($delimiter): string { $valueSet = Functions::flattenArray($delimiter); if (is_array($delimiter) && count($valueSet) > 1) { $quotedDelimiters = array_map( fn ($delimiter): string => preg_quote($delimiter ?? '', '/'), $valueSet ); $delimiters = implode('|', $quotedDelimiters); return '(' . $delimiters . ')'; } return '(' . preg_quote(Functions::flattenSingleValue($delimiter), '/') . ')'; } private static function matchFlags(bool $matchMode): string { return ($matchMode === true) ? 'miu' : 'mu'; } public static function fromArray(array $array, int $format = 0): string { $result = []; foreach ($array as $row) { $cells = []; foreach ($row as $cellValue) { $value = ($format === 1) ? self::formatValueMode1($cellValue) : self::formatValueMode0($cellValue); $cells[] = $value; } $result[] = implode(($format === 1) ? ',' : ', ', $cells); } $result = implode(($format === 1) ? ';' : ', ', $result); return ($format === 1) ? '{' . $result . '}' : $result; } private static function formatValueMode0(mixed $cellValue): string { if (is_bool($cellValue)) { return Calculation::getLocaleBoolean($cellValue ? 'TRUE' : 'FALSE'); } return (string) $cellValue; } private static function formatValueMode1(mixed $cellValue): string { if (is_string($cellValue) && ErrorValue::isError($cellValue) === false) { return Calculation::FORMULA_STRING_QUOTE . $cellValue . Calculation::FORMULA_STRING_QUOTE; } elseif (is_bool($cellValue)) { return Calculation::getLocaleBoolean($cellValue ? 'TRUE' : 'FALSE'); } return (string) $cellValue; } }