Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Cli.php #4342

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions src/PhpSpreadsheet/Writer/Cli.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
<?php

Check warning on line 1 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: no_closing_tag

Check warning on line 1 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: no_trailing_whitespace

Check warning on line 1 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: phpdoc_summary

Check warning on line 1 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: blank_line_before_statement

Check warning on line 1 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: no_whitespace_in_blank_line

Check warning on line 1 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / php-cs-fixer

Found violation(s) of type: single_blank_line_at_eof

namespace PhpOffice\PhpSpreadsheet\Writer;

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;

class Cli extends BaseWriter
{
/**
* Spreadsheet object.
*
* @var Spreadsheet
*/
protected $spreadsheet;

/**
* Sheet index to write.
*
* @var null|int
*/
private $sheetIndex = 0;

/**
* Create a new CLI Writer.
*/
public function __construct(Spreadsheet $spreadsheet)
{
$this->spreadsheet = $spreadsheet;
}

/**
* Save Spreadsheet to file.
*
* @param resource|string $filename
*/
public function save($filename, int $flags = 0): void
{
$this->processFlags($flags);

// Open file
$this->openFileHandle($filename);

// Write CLI table
fwrite($this->fileHandle, $this->generateCliTable());

// Close file
$this->maybeCloseFileHandle();
}

/**
* Generate CLI table output
*/
private function generateCliTable(): string
{
// Get sheet
$sheet = ($this->sheetIndex === null)

Check failure on line 58 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / phpcs

Whitespace found at end of line
? $this->spreadsheet->getActiveSheet()
: $this->spreadsheet->getSheet($this->sheetIndex);

// Get worksheet dimension
[$min, $max] = explode(':', $sheet->calculateWorksheetDataDimension());
[$minCol, $minRow] = Coordinate::indexesFromString($min);
[$maxCol, $maxRow] = Coordinate::indexesFromString($max);

// Get column widths
$colWidths = $this->calculateColumnWidths($sheet, $minCol, $maxCol, $minRow, $maxRow);

// Generate the output
$output = $this->generateTableHeader($colWidths);
$output .= $this->generateTableRows($sheet, $minCol, $maxCol, $minRow, $maxRow, $colWidths);

Check failure on line 73 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / phpcs

Whitespace found at end of line
return $output;
}

/**
* Calculate the width of each column
*/
private function calculateColumnWidths(Worksheet $sheet, int $minCol, int $maxCol, int $minRow, int $maxRow): array
{
$colWidths = [];

// Initialize with column header widths
for ($col = $minCol; $col <= $maxCol; ++$col) {
$cell = $sheet->getCellByColumnAndRow($col, $minRow);

Check failure on line 86 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::getCellByColumnAndRow().
$colWidths[$col] = strlen($cell->getValue() ?? '');
}

// Check cell values
for ($row = $minRow; $row <= $maxRow; ++$row) {
for ($col = $minCol; $col <= $maxCol; ++$col) {
$cell = $sheet->getCellByColumnAndRow($col, $row);

Check failure on line 93 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::getCellByColumnAndRow().
$value = $cell->getValue();
if ($value !== null) {
$length = strlen($value);
if ($length > ($colWidths[$col] ?? 0)) {
$colWidths[$col] = $length;
}
}
}
}

return $colWidths;
}

/**
* Generate the table header with correct column widths
*/
private function generateTableHeader(array $colWidths): string
{
$separator = '+';
foreach ($colWidths as $width) {
$separator .= str_repeat('-', $width + 2) . '+';
}
return $separator . "\n";
}

/**
* Generate the table rows
*/
private function generateTableRows(Worksheet $sheet, int $minCol, int $maxCol, int $minRow, int $maxRow, array $colWidths): string
{
$output = '';

for ($row = $minRow; $row <= $maxRow; ++$row) {
$output .= '|';
for ($col = $minCol; $col <= $maxCol; ++$col) {
$cell = $sheet->getCellByColumnAndRow($col, $row);

Check failure on line 129 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method PhpOffice\PhpSpreadsheet\Worksheet\Worksheet::getCellByColumnAndRow().
$value = (string) $cell->getValue();
$output .= ' ' . str_pad($value, $colWidths[$col]) . ' |';
}
$output .= "\n";

// Add separator after each row
$output .= $this->generateTableHeader($colWidths);
}

return $output;
}

/**
* Get sheet index.
*/
public function getSheetIndex(): ?int
{
return $this->sheetIndex;
}

/**
* Set sheet index.
*
* @param int $sheetIndex Sheet index
*
* @return $this
*/
public function setSheetIndex($sheetIndex)
{
$this->sheetIndex = $sheetIndex;
return $this;
}

/**
* Write all sheets (resets sheetIndex to NULL).
*
* @return $this
*/
public function writeAllSheets()
{
$this->sheetIndex = null;
return $this;
}
}
?>

Check failure on line 174 in src/PhpSpreadsheet/Writer/Cli.php

View workflow job for this annotation

GitHub Actions / phpcs

A closing tag is not permitted at the end of a PHP file
Loading