PHP code example of digitalcorehub / laravel-toon

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;

// Encode from array
$json = [
    'id' => 1,
    'name' => 'Test Product',
    'price' => 99.99
];

$toon = Toon::encode($json);
// Output:
// id, name, price;
// 1, Test Product, 99.99

// From JSON string
$toon = Toon::fromJson('{"id": 1, "name": "Test"}')->encode();

// From array
$toon = Toon::fromArray(['id' => 1, 'name' => 'Test'])->encode();

// From TOON string
$array = Toon::fromToon("id, name;\n1, Test")->decode();

$jsonString = '{"id": 1, "name": "Test Product", "price": 99.99}';
$toon = Toon::encode($jsonString);

$json = [
    'reviews' => [
        [
            'id' => 1,
            'customer' => 'John Doe',
            'rating' => 5
        ],
        [
            'id' => 2,
            'customer' => 'Jane Smith',
            'rating' => 4
        ]
    ]
];

$toon = Toon::encode($json);
// Output:
// reviews[2]{
//   id, customer, rating;
//   1, John Doe, 5
//   2, Jane Smith, 4
// }

$json = [
    'product' => 'Laptop',
    'specs' => [
        'cpu' => 'Intel i7',
        'ram' => '16GB'
    ],
    'reviews' => [
        ['id' => 1, 'rating' => 5],
        ['id' => 2, 'rating' => 4]
    ]
];

$toon = Toon::encode($json);

use Illuminate\Support\Facades\Log;

$data = ['id' => 1, 'name' => 'Test'];
Log::toon($data); // Logs at 'info' level

// Specify log level
Log::toon($data, 'debug');

// Specify channel
Log::toon($data, 'info', 'daily');

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();

// In config/toon.php
'compact' => true,

config(['toon.compact' => true]);

$data = ['id' => 1, 'name' => 'Test'];
$toon = Toon::encode($data);
// Output: id,name;1,Test (no spaces)

   Toon::encodeStream($input, $output); // Memory-efficient
   

   config(['toon.compact' => true]); // Smaller, faster
   

   foreach (Toon::lazy($data) as $line) {
       // Process line by line
   }
   

use DigitalCoreHub\Toon\Facades\Toon;

// Decode from TOON string
$toon = "reviews[1]{
  id, customer, rating, comment, verified;
  101, Alex Rivera, 5, Excellent!, true
}";

$array = Toon::decode($toon);
// Returns:
// [
//     [
//         'id' => 101,
//         'customer' => 'Alex Rivera',
//         'rating' => 5,
//         'comment' => 'Excellent!',
//         'verified' => true
//     ]
// ]

$toon = "reviews[2]{
  id, customer, rating;
  1, Alice, 5
  2, Bob, 4
}";

$array = Toon::decode($toon);
// Returns array with 2 review items

$toon = "product, reviews;
Laptop
reviews[2]{
  id, customer, rating;
  1, Alice, 5
  2, Bob, 4
}";

$array = Toon::decode($toon);
// Returns:
// [
//     'product' => 'Laptop',
//     'reviews' => [
//         ['id' => 1, 'customer' => 'Alice', 'rating' => 5],
//         ['id' => 2, 'customer' => 'Bob', 'rating' => 4]
//     ]
// ]

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"

// config/toon.php
'storage' => [
    'default_disk' => 'local',
    'default_directory' => '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',
    ],
];

use Illuminate\Support\Facades\Config;

$indentSize = config('toon.indentation');
$preserveOrder = config('toon.preserve_order');
$compact = config('toon.compact');
bash
php artisan toon:bench tests/bench/large.json
bash
   php artisan toon:bench your-file.json
   
bash
php artisan toon:bench [file]
bash
# Benchmark a specific file
php artisan toon:bench storage/large.json

# Use default benchmark file
php artisan toon:bench
bash
php artisan vendor:publish --tag=toon-config

config/toon.php