Skip to content

Commit

Permalink
Renamed renderer to formatter.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Nov 16, 2024
1 parent 4bf5514 commit 7d08ee4
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 76 deletions.
51 changes: 26 additions & 25 deletions CsvTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
/**
* Class CsvTable.
*
* Manipulates CSV data and renders it in various formats.
* Manipulates CSV data and outputs it in various formats.
* Implemented as a single class for portability.
*
* By default, the CSV data is parsed with a header row and rendered as a table.
* By default, the CSV data is parsed with a header row and formated as a table.
*
* Custom renderers can be used to render the CSV data in different formats.
* A custom formatter allows the parsed CSV data to be output in an alternative
* format.
*/
class CsvTable {

Expand Down Expand Up @@ -159,48 +160,48 @@ public static function fromFile($filepath, $separator = ',', $enclosure = '"', $
}

/**
* Render the CSV data.
* Format the CSV data.
*
* @param callable|string|null $renderer
* A callable to renderer the output. Can be a function name, a class name,
* @param callable|string|null $formatter
* A callable to formatter the output. Can be a function name, a class name,
* a closure, or an array containing a class name and a method name. If NULL
* is provided, the default renderer will be used.
* is provided, the default formatter will be used.
* @param array<mixed> $options
* An array of options to pass to the renderer. Defaults to an empty array.
* An array of options to pass to the formatter. Defaults to an empty array.
*
* @return string
* The rendered output.
* The formated output.
*
* @throws \Exception
* When the renderer is not callable.
* When the formatter is not callable.
*/
public function render(callable|string|null $renderer = NULL, array $options = []): string {
$renderer = $renderer ?? [static::class, 'renderCsv'];
$renderer = is_string($renderer) && class_exists($renderer) ? [$renderer, 'render'] : $renderer;
public function format(callable|string|null $formatter = NULL, array $options = []): string {
$formatter = $formatter ?? [static::class, 'formatCsv'];
$formatter = is_string($formatter) && class_exists($formatter) ? [$formatter, 'format'] : $formatter;

if (!is_callable($renderer)) {
throw new \Exception('Renderer must be callable.');
if (!is_callable($formatter)) {
throw new \Exception('Formatter must be callable.');
}

$this->parse();

return call_user_func($renderer, $this->header, $this->rows, $options);
return call_user_func($formatter, $this->header, $this->rows, $options);
}

/**
* Render as CSV.
* Format as CSV.
*
* @param array<string> $header
* An array containing the header row.
* @param array<array<string>> $rows
* An array containing all non-header rows.
* @param array<string,string> $options
* An array of options for the renderer.
* An array of options for the formatter.
*
* @return string
* The formatted output.
*/
public static function renderCsv(array $header, array $rows, array $options): string {
public static function formatCsv(array $header, array $rows, array $options): string {
$options += [
'separator' => ',',
'enclosure' => '"',
Expand Down Expand Up @@ -230,19 +231,19 @@ public static function renderCsv(array $header, array $rows, array $options): st
}

/**
* Render as a table.
* Format as a table.
*
* @param array<string> $header
* An array containing the header row.
* @param array<array<string>> $rows
* An array containing all non-header rows.
* @param array<string,string> $options
* An array of options for the renderer.
* An array of options for the formatter.
*
* @return string
* The formatted output.
*/
public static function renderTable(array $header, array $rows, array $options): string {
public static function formatTable(array $header, array $rows, array $options): string {
$output = '';

$options += [
Expand All @@ -261,19 +262,19 @@ public static function renderTable(array $header, array $rows, array $options):
}

/**
* Render as a Markdown table.
* Format as a Markdown table.
*
* @param array<string> $header
* An array containing the header row.
* @param array<array<string>> $rows
* An array containing all non-header rows.
* @param array<string,string> $options
* An array of options for the renderer.
* An array of options for the formatter.
*
* @return string
* The formatted output.
*/
public static function renderMarkdownTable(array $header, array $rows, array $options): string {
public static function formatMarkdownTable(array $header, array $rows, array $options): string {
$output = '';

$options += [
Expand Down
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<img width=200px height=200px src="https://placehold.jp/000000/ffffff/200x200.png?text=CsvTable&css=%7B%22border-radius%22%3A%22%20100px%22%7D" alt="Yourproject logo"></a>
</p>

<h1 align="center">CsvTable</h1>
<h1 align="center">PHP class to parse and format CSV content</h1>

<div align="center">

Expand All @@ -19,15 +19,11 @@

---

<p align="center"> PHP class to work with CSV as a table and export it as Markdown.
<br>
</p>

## Features

- Single-file class to manipulate CSV table.
- Renderers for CSV, text table and Markdown table.
- Support for a custom renderer.
- Formatters for CSV, text table and Markdown table.
- Support for a custom formatter.

## Installation

Expand All @@ -48,8 +44,8 @@ col31,col32,col33

```php
$csv = file_get_contents($csv_file);
// Render using the default renderer.
print (new CsvTable($csv))->render();
// Format using the default formatter.
print (new CsvTable($csv))->format();
```
will produce identical CSV content by default:
```csv
Expand All @@ -61,7 +57,7 @@ col31,col32,col33
### From file

```php
print (CsvTable::fromFile($file))->render();
print (CsvTable::fromFile($file))->format();
```
will produce identical CSV content by default:
```csv
Expand All @@ -70,10 +66,10 @@ col21,col22,col23
col31,col32,col33
```

### Using `CsvTable::renderTextTable()` renderer
### Using `CsvTable::formatTextTable()` formatter

```php
print (CsvTable::fromFile($file))->render([CsvTable::class, 'renderTextTable']);
print (CsvTable::fromFile($file))->format([CsvTable::class, 'formatTextTable']);
```
will produce table content:
```csv
Expand All @@ -83,10 +79,10 @@ col21|col22|col23
col31|col32|col33
```

### Using `CsvTable::renderTextTable()` renderer without a header
### Using `CsvTable::formatTextTable()` formatter without a header

```php
print (CsvTable::fromFile($file))->withoutHeader()->render([CsvTable::class, 'renderTextTable']);
print (CsvTable::fromFile($file))->withoutHeader()->format([CsvTable::class, 'formatTextTable']);
```
will produce table content:
```csv
Expand All @@ -95,10 +91,10 @@ col21|col22|col23
col31|col32|col33
```

### Using `CsvTable::renderMarkdownTable()` renderer
### Using `CsvTable::formatMarkdownTable()` formatter

```php
print (CsvTable::fromFile($file))->withoutHeader()->render([CsvTable::class, 'renderMarkdownTable']);
print (CsvTable::fromFile($file))->withoutHeader()->format([CsvTable::class, 'formatMarkdownTable']);
```
will produce Markdown table:
```markdown
Expand All @@ -108,10 +104,10 @@ will produce Markdown table:
| col31 | col32 | col33 |
```

### Custom renderer as a callback
### Custom formatter as a callback

```php
print (CsvTable::fromFile($file))->render(function ($header, $rows, $options) {
print (CsvTable::fromFile($file))->format(function ($header, $rows, $options) {
$output = '';

if (count($header) > 0) {
Expand Down
Loading

0 comments on commit 7d08ee4

Please sign in to comment.