Skip to content

Commit

Permalink
Csv class
Browse files Browse the repository at this point in the history
  • Loading branch information
distantnative committed May 18, 2024
1 parent 709c146 commit 8005285
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 22 deletions.
37 changes: 37 additions & 0 deletions Csv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace distantnative\CsvField;

use Kirby\Toolkit\Collection;

/**
* @package CSV Field
* @author Nico Hoffmann <nico@getkirby.com>
* @link https://github.com/distantnative/kirby-csv-field
* @copyright Nico Hoffmann
* @license https://opensource.org/licenses/MIT
*/
class Csv extends Collection
{
public function columns(): array
{
return array_keys($this->first());
}

public static function for(string $file, string $delimiter = ','): static
{
$lines = file($file);
$lines[0] = str_replace("\xEF\xBB\xBF", '', $lines[0]);
$csv = array_map(fn ($d) => str_getcsv($d, $delimiter), $lines);

array_walk($csv, fn (&$a) => $a = array_combine($csv[0], $a));
array_shift($csv);

return new static($csv);
}

public function rows(): array
{
return $this->toArray();
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Kirby CMS Panel field to upload and display a single CSV file",
"license": "MIT",
"type": "kirby-plugin",
"version": "1.0.0-beta.2",
"version": "1.0.0-beta.3",
"homepage": "https://distantnative.com/kirby-csv-field/",
"authors": [
{
Expand Down
56 changes: 54 additions & 2 deletions docs/templates.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
# Use in templates

To use your `csv` field in your templates, the plugin also ships with a `->toCsv()` field method.

```php
$csv = $page->myCsvField()->toCsv(';');
```

## `Csv` object

The field method returns a `distantnative\CsvField\Csv` object. The `Csv` object is basically a [Kirby collection](https://getkirby.com/docs/reference/objects/toolkit/collection):

```php
$table = $page->myCsvField()->toCsv(';');
$csv->first();
$csv->last();
$csv->paginate();
// ...
```

With a little added sugar for rows and columns:

var_dump($table);
```php
var_dump($csv->columns());
```

```
array (size=4)
0 => string 'Username' (length=8)
1 => string 'Identifier' (length=10)
2 => string 'First name' (length=10)
3 => string 'Last name' (length=9)
```

```php
var_dump($csv->rows());
```

```
Expand Down Expand Up @@ -40,3 +69,26 @@ array (size=5)
'Last name' => string 'Smith' (length=5)
```

## Example

```php
<?php if ($csv = $page->myCsvField()->toCsv(';')): ?>
<table>
<thead>
<?php foreach ($csv->columns() as $column): ?>
<th><?= $column ?></th>
<?php endforeach ?>
</thead>
<tbody>
<?php foreach ($csv->rows() as $row): ?>
<tr>
<?php foreach ($row as $cell): ?>
<td><?= $cell ?></td>
<?php endforeach ?>
</tr>
<?php endforeach ?>
</tbody>
</table>
<?php endif ?>
```
31 changes: 12 additions & 19 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,9 @@
use Kirby\Cms\App;
use Kirby\Content\Field;

function csv(string $file, string $delimiter = ','): array
{
$lines = file($file);
$lines[0] = str_replace("\xEF\xBB\xBF", '', $lines[0]);
$csv = array_map(fn ($d) => str_getcsv($d, $delimiter), $lines);

array_walk($csv, fn (&$a) => $a = array_combine($csv[0], $a));
array_shift($csv);

return $csv;
}
load([
'distantnative\CsvField\Csv' => __DIR__ . '/Csv.php'
]);

App::plugin('distantnative/kirby-csv-field', [
'fields' => [
Expand Down Expand Up @@ -74,22 +66,23 @@ function csv(string $file, string $delimiter = ','): array
'method' => 'GET',
'action' => function () {
$file = $this->requestQuery('file');
$file = $this->field()->model()->file($file);
return csv($file->root(), $this->field()->delimiter());
$csv = Csv::for(
$this->field()->model()->file($file)->root(),
$this->field()->delimiter()
);
return $csv->rows();
}
]
]
]
],
'fieldMethods' => [
'toCsv' => function (Field $field, string $delimiter = ','): array {
$files = $field->toFiles();

if ($file = $files->first()) {
return csv($file->root(), $delimiter);
'toCsv' => function (Field $field, string $delimiter = ','): Csv|null {
if ($file = $field->toFiles()->first()) {
return Csv::for($file->root(), $delimiter);
}

return [];
return null;
}
],
'translations' => require_once __DIR__ . '/i18n/index.php'
Expand Down

0 comments on commit 8005285

Please sign in to comment.