1. Go to this page and download the library: Download digitalcorehub/laravel-toon library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
digitalcorehub / laravel-toon example snippets
// Encode to TOON
$toon = toon_encode(['id' => 1, 'name' => 'Test']);
// or
$toon = toon_encode('{"id": 1, "name": "Test"}');
// Decode from TOON
$array = toon_decode("id, name;\n1, Test");
use DigitalCoreHub\Toon\Facades\Toon;
$data = ['id' => 1, 'name' => 'Test', 'active' => true];
$colored = Toon::console($data, $output); // $output is optional OutputInterface
// In Artisan commands
$this->line(Toon::console($data, $this->output));
use DigitalCoreHub\Toon\Facades\Toon;
// Encode large JSON file to TOON format
Toon::encodeStream('storage/large.json', 'storage/large.toon');
// Support for Laravel Storage disks
Toon::encodeStream('local:data.json', 'local:data.toon');
use DigitalCoreHub\Toon\Facades\Toon;
$data = ['id' => 1, 'name' => 'Test', 'items' => [1, 2, 3]];
// Generate lines one by one
foreach (Toon::lazy($data) as $line) {
echo $line . "\n";
}
// Or write directly to file
Toon::lazy($data)->toFile('output.toon');
// Or get as array
$lines = Toon::lazy($data)->toArray();
use DigitalCoreHub\Toon\Exceptions\InvalidToonFormatException;
use DigitalCoreHub\Toon\Facades\Toon;
try {
$array = Toon::decode($toon);
} catch (InvalidToonFormatException $e) {
// Handle invalid TOON format
echo "Error: " . $e->getMessage();
}
// Before: "Mismatched key/value count"
// After: "Key count (4) does not match value count (3) at line 5."
// Before: "Keys line must end with semicolon"
// After: "Missing semicolon in header block at line 2. Found: id, name, price"
use DigitalCoreHub\Toon\Toon;
class ProductController extends Controller
{
public function __construct(
private Toon $toon
) {}
public function export()
{
$data = Product::all()->toArray();
return $this->toon->encode($data);
}
}
use DigitalCoreHub\Toon\Facades\Toon;
$data = ['id' => 1, 'name' => 'Test'];
// Store to default disk (from config)
$path = Toon::store('my-file', $data);
// Returns: "toon/my-file.toon"
// Store to specific disk
$path = Toon::store('my-file', $data, 'public');
// Returns: "toon/my-file.toon"
// Store with nested path (directory created automatically)
$path = Toon::store('exports/users', $data, 'local');
// Returns: "toon/exports/users.toon"
use DigitalCoreHub\Toon\Facades\Toon;
// In a controller
Route::get('/export/users', function () {
$users = User::all()->toArray();
return Toon::download('users', $users);
});
// With custom filename
return Toon::download('export-2024-01-01', $data);
use Illuminate\Support\Facades\Response;
// In a controller
public function index()
{
$data = Product::all()->toArray();
return response()->toon($data);
}
// routes/api.php
Route::get('/products', function () {
return response()->toon(Product::all()->toArray());
});
return [
/*
|--------------------------------------------------------------------------
| Indentation
|--------------------------------------------------------------------------
|
| The number of spaces used for indentation in the TOON output.
|
*/
'indentation' => 4,
/*
|--------------------------------------------------------------------------
| Key Separator
|--------------------------------------------------------------------------
|
| The separator used between keys in the TOON format.
|
*/
'key_separator' => ', ',
/*
|--------------------------------------------------------------------------
| Line Break
|--------------------------------------------------------------------------
|
| The line break character used in the TOON output.
|
*/
'line_break' => PHP_EOL,
/*
|--------------------------------------------------------------------------
| Strict Mode
|--------------------------------------------------------------------------
|
| When enabled, decoding will throw exceptions for any formatting issues.
| When disabled, it will attempt to parse more leniently.
|
*/
'strict_mode' => false,
/*
|--------------------------------------------------------------------------
| Preserve Order
|--------------------------------------------------------------------------
|
| Whether to preserve the original JSON key ordering in the output.
|
*/
'preserve_order' => true,
/*
|--------------------------------------------------------------------------
| Storage Configuration
|--------------------------------------------------------------------------
|
| Configuration for storing TOON files using Laravel Storage.
|
*/
'storage' => [
'default_disk' => 'local',
'default_directory' => 'toon',
],
];