You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For detailed API documentation, see the [helgesverre/toon](https://github.com/helgesverre/toon) package.
134
134
135
+
### Using the Toonable Interface and Trait
136
+
137
+
The package provides a convenient way to make your DTOs, models, or any PHP objects directly convertible to TOON format using the `Toonable` interface and `ToonFormat` trait.
138
+
139
+
#### Basic Usage
140
+
141
+
```php
142
+
use RobertGDev\LaravelToon\Contracts\Toonable;
143
+
use RobertGDev\LaravelToon\Concerns\ToonFormat;
144
+
145
+
class UserDTO implements Toonable
146
+
{
147
+
use ToonFormat;
148
+
149
+
public function __construct(
150
+
public string $name,
151
+
public int $age,
152
+
public array $roles,
153
+
) {}
154
+
}
155
+
156
+
// Now you can call toToon() directly on your object
157
+
$user = new UserDTO('John Doe', 30, ['admin', 'owner']);
158
+
$toon = $user->toToon();
159
+
160
+
// Output:
161
+
// name: John Doe
162
+
// age: 30
163
+
// roles[2]: admin,owner
164
+
```
165
+
166
+
#### With Custom Encoding Options
167
+
168
+
You can pass custom encoding options to the `toToon()` method:
169
+
170
+
```php
171
+
use HelgeSverre\Toon\EncodeOptions;
172
+
173
+
$user = new UserDTO('John Doe', 30, ['admin', 'owner']);
174
+
175
+
// Use a custom delimiter
176
+
$options = new EncodeOptions(delimiter: '|');
177
+
$toon = $user->toToon($options);
178
+
179
+
// Output:
180
+
// name: John Doe
181
+
// age: 30
182
+
// roles[2|]: admin|owner
183
+
```
184
+
185
+
#### Using with Eloquent Models
186
+
187
+
You can also use this with Laravel Eloquent models:
188
+
189
+
```php
190
+
use Illuminate\Database\Eloquent\Model;
191
+
use RobertGDev\LaravelToon\Contracts\Toonable;
192
+
use RobertGDev\LaravelToon\Concerns\ToonFormat;
193
+
194
+
class User extends Model implements Toonable
195
+
{
196
+
use ToonFormat;
197
+
198
+
// Your model definition...
199
+
}
200
+
201
+
// Convert a model instance to TOON
202
+
$user = User::find(1);
203
+
$toon = $user->toToon();
204
+
```
205
+
135
206
## Artisan Command
136
207
137
208
The package includes an Artisan command for converting between JSON and TOON formats:
0 commit comments