|
| 1 | +# Laravel TOON |
| 2 | + |
| 3 | +Laravel integration for TOON - A human-readable data serialization format. |
| 4 | + |
| 5 | +This package provides Laravel-specific features on top of the [robertgdev/php-toon](https://github.com/robertgdev/php-toon) core library. |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +composer require robertgdev/laravel-toon |
| 11 | +``` |
| 12 | + |
| 13 | +This will automatically install the `robertgdev/php-toon` core library as a dependency. |
| 14 | + |
| 15 | +## Configuration |
| 16 | + |
| 17 | +### Publishing Configuration |
| 18 | + |
| 19 | +Publish the configuration file to customize default encoding/decoding options: |
| 20 | + |
| 21 | +```bash |
| 22 | +php artisan vendor:publish --tag=toon-config |
| 23 | +``` |
| 24 | + |
| 25 | +This creates `config/toon.php` where you can set default options. |
| 26 | + |
| 27 | +### Configuration Options |
| 28 | + |
| 29 | +The config file supports both file-based configuration and environment variables: |
| 30 | + |
| 31 | +```php |
| 32 | +// config/toon.php |
| 33 | +return [ |
| 34 | + 'encode' => [ |
| 35 | + 'indent' => env('TOON_ENCODE_INDENT', 2), |
| 36 | + 'delimiter' => env('TOON_ENCODE_DELIMITER', ','), |
| 37 | + 'lengthMarker' => env('TOON_ENCODE_LENGTH_MARKER', false), |
| 38 | + ], |
| 39 | + 'decode' => [ |
| 40 | + 'indent' => env('TOON_DECODE_INDENT', 2), |
| 41 | + 'strict' => env('TOON_DECODE_STRICT', true), |
| 42 | + 'objectsAsStdClass' => env('TOON_DECODE_OBJECTS_AS_STDCLASS', false), |
| 43 | + ], |
| 44 | +]; |
| 45 | +``` |
| 46 | + |
| 47 | +### Environment Variables |
| 48 | + |
| 49 | +Add these to your `.env` file to configure TOON globally: |
| 50 | + |
| 51 | +```env |
| 52 | +# Encoding options |
| 53 | +TOON_ENCODE_INDENT=2 |
| 54 | +TOON_ENCODE_DELIMITER=, |
| 55 | +TOON_ENCODE_LENGTH_MARKER=false |
| 56 | +
|
| 57 | +# Decoding options |
| 58 | +TOON_DECODE_INDENT=2 |
| 59 | +TOON_DECODE_STRICT=true |
| 60 | +TOON_DECODE_OBJECTS_AS_STDCLASS=false |
| 61 | +``` |
| 62 | + |
| 63 | +**Available Delimiters:** |
| 64 | +- `,` (comma, default) |
| 65 | +- `\t` (tab - use `"\t"` in config or `\t` in .env) |
| 66 | +- `|` (pipe) |
| 67 | + |
| 68 | +**Length Marker:** |
| 69 | +- `false` (default) - no marker |
| 70 | +- `true` or `#` - adds `#` prefix to array lengths |
| 71 | + |
| 72 | +**Objects as StdClass:** |
| 73 | +- `false` (default) - objects decode to associative arrays |
| 74 | +- `true` - objects decode to `StdClass` instances (enables perfect round-trips) |
| 75 | + |
| 76 | +### Using Configured Defaults |
| 77 | + |
| 78 | +When you use the facade without options, it automatically uses your configured defaults: |
| 79 | + |
| 80 | +```php |
| 81 | +use RobertGDev\LaravelToon\Facades\Toon; |
| 82 | + |
| 83 | +// Uses config defaults |
| 84 | +$encoded = Toon::encode($data); |
| 85 | +$decoded = Toon::decode($encoded); |
| 86 | + |
| 87 | +// Override with custom options |
| 88 | +use RobertGDev\Toon\Types\EncodeOptions; |
| 89 | +$encoded = Toon::encode($data, new EncodeOptions(indent: 4)); |
| 90 | +``` |
| 91 | + |
| 92 | +## Programmatic Usage |
| 93 | + |
| 94 | +### Using the Facade (Laravel-style) |
| 95 | + |
| 96 | +The package provides a Laravel facade for easy access: |
| 97 | + |
| 98 | +```php |
| 99 | +use RobertGDev\LaravelToon\Facades\Toon; |
| 100 | +use RobertGDev\Toon\Types\EncodeOptions; |
| 101 | + |
| 102 | +// Simple encoding |
| 103 | +$data = ['name' => 'Ada', 'age' => 30, 'active' => true]; |
| 104 | +$encoded = Toon::encode($data); |
| 105 | + |
| 106 | +// With options |
| 107 | +$options = new EncodeOptions( |
| 108 | + indent: 4, |
| 109 | + delimiter: "\t", |
| 110 | + lengthMarker: '#' |
| 111 | +); |
| 112 | +$encoded = Toon::encode($data, $options); |
| 113 | + |
| 114 | +// Decoding |
| 115 | +$decoded = Toon::decode($encoded); |
| 116 | +``` |
| 117 | + |
| 118 | +The facade is automatically registered via package discovery as `Toon`, so you can also use it without importing: |
| 119 | + |
| 120 | +```php |
| 121 | +$encoded = \Toon::encode(['key' => 'value']); |
| 122 | +$decoded = \Toon::decode($encoded); |
| 123 | +``` |
| 124 | + |
| 125 | +### Using the Core Library Directly |
| 126 | + |
| 127 | +You can also use the core library directly: |
| 128 | + |
| 129 | +```php |
| 130 | +use RobertGDev\Toon\Toon; |
| 131 | + |
| 132 | +$encoded = Toon::encode(['name' => 'Ada']); |
| 133 | +$decoded = Toon::decode($encoded); |
| 134 | +``` |
| 135 | + |
| 136 | +For detailed API documentation, see the [robertgdev/php-toon](https://github.com/robertgdev/php-toon) package. |
| 137 | + |
| 138 | +## Artisan Command |
| 139 | + |
| 140 | +The package includes an Artisan command for converting between JSON and TOON formats: |
| 141 | + |
| 142 | +```bash |
| 143 | +# Encode JSON to TOON |
| 144 | +php artisan toon:convert input.json --output=output.toon |
| 145 | + |
| 146 | +# Decode TOON to JSON |
| 147 | +php artisan toon:convert input.toon --output=output.json |
| 148 | + |
| 149 | +# Auto-detect mode based on file extension |
| 150 | +php artisan toon:convert data.json # Encodes to TOON |
| 151 | +php artisan toon:convert data.toon # Decodes to JSON |
| 152 | + |
| 153 | +# Print to stdout instead of file |
| 154 | +php artisan toon:convert input.json |
| 155 | + |
| 156 | +# Use custom delimiter (tab or pipe) |
| 157 | +php artisan toon:convert input.json --delimiter="\t" |
| 158 | +php artisan toon:convert input.json --delimiter="|" |
| 159 | + |
| 160 | +# Use length marker |
| 161 | +php artisan toon:convert input.json --length-marker |
| 162 | + |
| 163 | +# Show token statistics |
| 164 | +php artisan toon:convert input.json --stats |
| 165 | + |
| 166 | +# Custom indentation |
| 167 | +php artisan toon:convert input.json --indent=4 |
| 168 | + |
| 169 | +# Disable strict mode for decoding |
| 170 | +php artisan toon:convert input.toon --no-strict |
| 171 | +``` |
| 172 | + |
| 173 | +### Command Options |
| 174 | + |
| 175 | +- `input` - Input file path (required) |
| 176 | +- `--o|output` - Output file path (prints to stdout if not specified) |
| 177 | +- `--e|encode` - Force encode mode (auto-detected by default) |
| 178 | +- `--d|decode` - Force decode mode (auto-detected by default) |
| 179 | +- `--delimiter` - Delimiter for arrays: comma (,), tab (\t), or pipe (|) |
| 180 | +- `--indent` - Indentation size (default: 2) |
| 181 | +- `--length-marker` - Use length marker (#) for arrays |
| 182 | +- `--strict` - Enable strict mode for decoding (default: true) |
| 183 | +- `--no-strict` - Disable strict mode for decoding |
| 184 | +- `--stats` - Show token statistics |
| 185 | + |
| 186 | +## Features |
| 187 | + |
| 188 | +- **Laravel Facade**: Use `Toon::encode()` and `Toon::decode()` anywhere in your Laravel app |
| 189 | +- **Artisan Command**: Convert files between JSON and TOON formats via CLI |
| 190 | +- **Auto-Registration**: Service provider and facade automatically registered via package discovery |
| 191 | +- **Service Container**: Toon class registered as a singleton in Laravel's container |
| 192 | +- **File Operations**: Read and write TOON files with ease |
| 193 | +- **Token Statistics**: Estimate token savings when converting to TOON |
| 194 | + |
| 195 | +## Package Structure |
| 196 | + |
| 197 | +This package is a thin Laravel integration layer. The core TOON functionality is provided by the `robertgdev/php-toon` package, which is a standalone PHP library. |
| 198 | + |
| 199 | +### What's in this package: |
| 200 | +- [`ToonServiceProvider`](src/ToonServiceProvider.php) - Registers the service and command |
| 201 | +- [`Toon` Facade](src/Facades/Toon.php) - Laravel facade for easy access |
| 202 | +- [`ToonCommand`](src/Console/ToonCommand.php) - Artisan command for file conversion |
| 203 | +- Configuration file with Laravel integration |
| 204 | +- Comprehensive integration test suite (38 tests covering all features) |
| 205 | + |
| 206 | +### What's in the core package: |
| 207 | +- All encoding/decoding logic |
| 208 | +- Type definitions and options |
| 209 | +- Core TOON parser and serializer |
| 210 | + |
| 211 | +See [robertgdev/php-toon](https://github.com/robertgdev/php-toon) for the core library documentation. |
| 212 | + |
| 213 | +## Requirements |
| 214 | + |
| 215 | +- PHP 8.2+ |
| 216 | +- Laravel 10.x or 11.x or 12.x |
| 217 | +- robertgdev/php-toon (automatically installed) |
| 218 | + |
| 219 | +## Testing |
| 220 | + |
| 221 | +Run the test suite with: |
| 222 | + |
| 223 | +```bash |
| 224 | +vendor/bin/pest |
| 225 | +``` |
| 226 | + |
| 227 | +The package includes 38 comprehensive tests covering: |
| 228 | +- Artisan command functionality (15 tests) |
| 229 | +- Configuration and service provider features (10 tests) |
| 230 | +- Laravel integration and facade functionality (13 tests) |
| 231 | + |
| 232 | +## License |
| 233 | + |
| 234 | +MIT License |
0 commit comments