Skip to content

Commit 54be43f

Browse files
authored
Merge pull request #1 from WendellAdriel/feat/toonable-interface
Add Toonable interface and ToonFormat trait
2 parents 0d4cc54 + 108d9e4 commit 54be43f

File tree

7 files changed

+155
-3
lines changed

7 files changed

+155
-3
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
/vendor/
22
composer.lock
33
.phpunit.result.cache
4-
.pest
4+
.phpunit.cache/
5+
.pest
6+
.idea/

README.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,77 @@ $decoded = Toon::decode($encoded);
132132

133133
For detailed API documentation, see the [helgesverre/toon](https://github.com/helgesverre/toon) package.
134134

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+
135206
## Artisan Command
136207

137208
The package includes an Artisan command for converting between JSON and TOON formats:
@@ -183,6 +254,7 @@ php artisan toon:convert input.toon --no-strict
183254
## Features
184255

185256
- **Laravel Facade**: Use `Toon::encode()` and `Toon::decode()` anywhere in your Laravel app
257+
- **Toonable Interface & Trait**: Add `toToon()` method to any class (DTOs, models, etc.) with a simple trait
186258
- **Artisan Command**: Convert files between JSON and TOON formats via CLI
187259
- **Auto-Registration**: Service provider and facade automatically registered via package discovery
188260
- **Service Container**: Toon class registered as a singleton in Laravel's container
@@ -197,8 +269,10 @@ This package is a thin Laravel integration layer. The core TOON functionality is
197269
- [`ToonServiceProvider`](src/ToonServiceProvider.php) - Registers the service and command
198270
- [`Toon` Facade](src/Facades/Toon.php) - Laravel facade for easy access
199271
- [`ToonCommand`](src/Console/ToonCommand.php) - Artisan command for file conversion
272+
- [`Toonable` Interface](src/Contracts/Toonable.php) - Contract for objects convertible to TOON
273+
- [`ToonFormat` Trait](src/Concerns/ToonFormat.php) - Provides `toToon()` method implementation
200274
- Configuration file with Laravel integration
201-
- Comprehensive integration test suite (38 tests covering all features)
275+
- Comprehensive integration test suite covering all features
202276

203277
### What's in the core package:
204278
- All encoding/decoding logic

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
}
3232
},
3333
"scripts": {
34-
"test": "pest"
34+
"test": "pest --parallel"
3535
},
3636
"config": {
3737
"sort-packages": true,

src/Concerns/ToonFormat.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace RobertGDev\LaravelToon\Concerns;
4+
5+
use HelgeSverre\Toon\EncodeOptions;
6+
7+
8+
trait ToonFormat
9+
{
10+
/**
11+
* Convert the object to its TOON representation.
12+
*/
13+
public function toToon(?EncodeOptions $options = null): string
14+
{
15+
return app('toon')->encode($this, $options);
16+
}
17+
}

src/Contracts/Toonable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace RobertGDev\LaravelToon\Contracts;
4+
5+
interface Toonable
6+
{
7+
/**
8+
* Convert the object to its TOON representation.
9+
*/
10+
public function toToon(): string;
11+
}

tests/Datasets/UserDTO.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Datasets;
4+
5+
use RobertGDev\LaravelToon\Concerns\ToonFormat;
6+
use RobertGDev\LaravelToon\Contracts\Toonable;
7+
8+
class UserDTO implements Toonable
9+
{
10+
use ToonFormat;
11+
12+
public function __construct(
13+
public string $name,
14+
public int $age,
15+
public array $roles,
16+
) {}
17+
}

tests/ToonFormatTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Datasets\UserDTO;
4+
use HelgeSverre\Toon\EncodeOptions;
5+
6+
describe('converts to TOON format using Trait', function () {
7+
it('using default encoding options', function () {
8+
$userDTO = new UserDTO(
9+
name: 'John Doe',
10+
age: 30,
11+
roles: ['admin', 'owner'],
12+
);
13+
14+
$result = $userDTO->toToon();
15+
16+
expect($result)->toBe("name: John Doe\nage: 30\nroles[2]: admin,owner");
17+
});
18+
19+
it('using custom encoding options', function () {
20+
$userDTO = new UserDTO(
21+
name: 'John Doe',
22+
age: 30,
23+
roles: ['admin', 'owner'],
24+
);
25+
26+
$options = new EncodeOptions(delimiter: '|');
27+
$result = $userDTO->toToon($options);
28+
29+
expect($result)->toBe("name: John Doe\nage: 30\nroles[2|]: admin|owner");
30+
});
31+
});

0 commit comments

Comments
 (0)