Skip to content

Commit

Permalink
Merge pull request #33 from TheDragonCode/2.x
Browse files Browse the repository at this point in the history
Added `toJson` method
  • Loading branch information
Andrey Helldar authored Oct 22, 2022
2 parents ef1ad02 + 49ba86b commit 146f654
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Concerns/To.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace DragonCode\SimpleDataTransferObject\Concerns;

trait To
{
public function toJson($flags = JSON_UNESCAPED_UNICODE): string
{
return json_encode($this->toArray(), $flags);
}
}
2 changes: 2 additions & 0 deletions src/DataTransferObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DragonCode\SimpleDataTransferObject\Concerns\Castable;
use DragonCode\SimpleDataTransferObject\Concerns\From;
use DragonCode\SimpleDataTransferObject\Concerns\Reflection;
use DragonCode\SimpleDataTransferObject\Concerns\To;
use DragonCode\Support\Concerns\Makeable;
use DragonCode\Support\Facades\Helpers\Arr;
use DragonCode\Support\Facades\Helpers\Str;
Expand All @@ -23,6 +24,7 @@ abstract class DataTransferObject implements Contract
use From;
use Makeable;
use Reflection;
use To;

protected $map = [];

Expand Down
24 changes: 24 additions & 0 deletions tests/Unit/ToJsonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace Tests\Unit;

use Tests\Fixtures\Simple;
use Tests\TestCase;

class ToJsonTest extends TestCase
{
public function testToJson()
{
$object = new Simple([
'foo' => $this->foo,
'bar' => $this->bar,
'baz' => $this->baz,
]);

$this->assertJson($object->toJson());

$this->assertSame('{"foo":"Foo"}', $object->toJson());
}
}

0 comments on commit 146f654

Please sign in to comment.