umpirsky/list-generator
Exports datasets to many formats: text, JSON, YAML, XML, HTML, CSV, SQL (MySQL/PostgreSQL/SQLite), PHP and XLIFF. Used to generate shared lists like countries, currencies, languages, locales and TLDs.
Installation:
composer require umpirsky/list-generator
Add to composer.json if not using autoloading:
"autoload": {
"psr-4": {
"App\\": "app/",
"Umpirsky\\": "vendor/umpirsky/"
}
}
Run composer dump-autoload.
First Use Case: Generate a simple CSV from an array:
use Umpirsky\ListGenerator\ListGenerator;
$data = [
['name' => 'John', 'age' => 30],
['name' => 'Jane', 'age' => 25],
];
$generator = new ListGenerator();
echo $generator->generate('csv', $data);
Key Classes:
ListGenerator: Main class for generating lists.ListGenerator\Format\*: Format-specific classes (e.g., Csv, Json, Xml).ListGenerator\List\*: Predefined lists (e.g., CountryList, CurrencyList).Use the ListGenerator to export Eloquent collections or arrays:
// From Eloquent models
$users = User::all();
$generator = new ListGenerator();
$html = $generator->generate('html', $users->toArray());
// With custom headers (CSV example)
$csv = $generator->generate('csv', $users->toArray(), ['headers' => ['ID', 'Name', 'Email']]);
Leverage built-in lists (e.g., countries, currencies) without manual data entry:
use Umpirsky\ListGenerator\List\CountryList;
$countryList = new CountryList();
$json = $generator->generate('json', $countryList->getList());
Export data as SQL for databases (e.g., MySQL, PostgreSQL):
$sql = $generator->generate('sql:mysql', $data, [
'table' => 'users',
'columns' => ['id', 'name', 'email'],
]);
AppServiceProvider:
$this->app->singleton(ListGenerator::class, function ($app) {
return new ListGenerator();
});
// app/Facades/ListGeneratorFacade.php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class ListGeneratorFacade extends Facade {
protected static function getFacadeAccessor() { return 'list-generator'; }
}
Register in AppServiceProvider:
$this->app->bind('list-generator', function () {
return new ListGenerator();
});
Use a config file or environment variable to switch formats:
$format = config('app.list_format', 'csv');
$result = $generator->generate($format, $data);
Create a Blade directive for inline list generation:
// app/Providers/BladeServiceProvider.php
use Illuminate\Support\Facades\Blade;
Blade::directive('list', function ($expression) {
$format = explode(',', $expression)[0] ?? 'html';
return "<?php echo app('list-generator')->generate('{$format}', {$expression}); ?>";
});
Usage in Blade:
@list($users)
Offload heavy exports to a queue job:
// app/Jobs/GenerateListJob.php
use Umpirsky\ListGenerator\ListGenerator;
class GenerateListJob implements ShouldQueue {
protected $data;
protected $format;
public function handle() {
$generator = new ListGenerator();
Storage::put("exports/{$this->format}.{$this->extension}", $generator->generate($this->format, $this->data));
}
}
Extend the package by creating a custom format class:
// app/Extensions/JsonApiFormat.php
namespace App\Extensions;
use Umpirsky\ListGenerator\Format\JsonFormat;
class JsonApiFormat extends JsonFormat {
protected function getOptions() {
return array_merge(parent::getOptions(), ['jsonapi' => true]);
}
public function generate($data) {
return $this->encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
}
Register the extension in ListGenerator:
$generator->addFormat('jsonapi', new JsonApiFormat());
$generator->stream('csv', $data, function ($chunk) {
echo $chunk;
});
$safeData = array_map(function ($item) {
return array_map('addslashes', $item);
}, $data);
$sql = $generator->generate('sql:mysql', $safeData);
é, ü) may corrupt output in CSV/HTML.$generator->generate('csv', $data, ['encoding' => 'UTF-8']);
XLIFF) may not be actively maintained.Umpirsky\ListGenerator, which might conflict with other umpirsky packages.use Umpirsky\ListGenerator\ListGenerator as UmpirskyListGenerator;
Ensure data is an array or iterable:
if (!is_array($data) && !$data instanceof \Iterator) {
throw new \InvalidArgumentException('Data must be an array or iterable.');
}
Verify the format exists before generating:
if (!$generator->hasFormat('jsonapi')) {
throw new \RuntimeException('Format not supported.');
}
Wrap generation in a try-catch block:
try {
$result = $generator->generate('csv', $data);
} catch (\Exception $e) {
Log::error("List generation failed: " . $e->getMessage());
throw $e;
}
Use var_dump() or dd() to debug:
$options = $generator->getOptions('csv');
dd($options); // Check available options
Some formats (e.g., CSV) have default options like headers or delimiters:
$csv = $generator->generate('csv', $data, [
'headers' => ['ID', 'Name'],
'delimiter' => ';',
]);
SQL formats require additional parameters:
$sql = $generator->generate('sql:postgresql', $data, [
'table' => 'users',
'columns' => ['id', 'name'],
'if_not_exists' => true,
]);
Override the default HTML template:
$generator->setTemplate('html', '<table>{rows}</table>');
Control YAML indentation for readability:
$yaml = $generator->generate('yaml', $data, ['indent' => 2]);
Create reusable list classes:
// app/Lists/CustomList.php
namespace
How can I help you explore Laravel packages today?