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

Allow to specify formatter by name. #27

Merged
merged 1 commit into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
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
24 changes: 17 additions & 7 deletions CsvTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,11 @@ public static function fromFile($filepath, $separator = ',', $enclosure = '"', $
/**
* Format the CSV data.
*
* @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 formatter will be used.
* @param string|array<int, string>|callable|null $formatter
* Formatter to use. Can be a function name, a class name, a closure, an
* array containing a class name and a method name, or a predefined
* formatter available as format<Name> method. If NULL is provided, the
* 'CSV" formatter will be used.
* @param array<mixed> $options
* An array of options to pass to the formatter. Defaults to an empty array.
*
Expand All @@ -175,9 +176,18 @@ public static function fromFile($filepath, $separator = ',', $enclosure = '"', $
* @throws \Exception
* When the formatter is not callable.
*/
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;
public function format(string|array|callable|null $formatter = NULL, array $options = []): string {
$formatter = $formatter ?? 'csv';

if (is_string($formatter) && !function_exists($formatter)) {
if (class_exists($formatter)) {
$formatter = [$formatter, 'format'];
}
else {
$method = 'format' . str_replace(' ', '', ucwords(str_replace(['-', '_'], ' ', strtolower($formatter))));
$formatter = method_exists($this, $method) ? [$this, $method] : NULL;
}
}

if (!is_callable($formatter)) {
throw new \Exception('Formatter must be callable.');
Expand Down
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ col21,col22,col23
col31,col32,col33
```

### Using `CsvTable::formatTextTable()` formatter
### Using `text_table` formatter

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

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

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

### Using `CsvTable::formatMarkdownTable()` formatter
### Using `markdown_table` formatter

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

### Custom formatter as a callback
### Custom formatter as an anonymous callback

```php
print (CsvTable::fromFile($file))->format(function ($header, $rows, $options) {
Expand All @@ -128,6 +128,19 @@ col21|col22|col23
col31|col32|col33
```

### Custom formatter as a class with default `format` method

```php
print (CsvTable::fromFile($file))->withoutHeader()->format(CustomFormatter::class);
```

### Custom formatter as a class with a custom method and options

```php
$formatter_options = ['option1' => 'value1', 'option2' => 'value2'];
print (CsvTable::fromFile($file))->withoutHeader()->format([CustomFormatter::class, 'customFormat'], $formatter_options);
```

## Maintenance

```bash
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
"AlexSkrypnyk\\CsvTable\\": ""
}
},
"autoload-dev": {
"psr-4": {
"AlexSkrypnyk\\CsvTable\\Tests\\": "tests"
}
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true,
Expand Down
Loading