-
-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(handler): Create JsonSerializable handler
- Loading branch information
Marcin Czarnecki
committed
Jun 24, 2023
1 parent
b4e6128
commit 5a93aff
Showing
10 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Handler; | ||
|
||
use JMS\Serializer\Visitor\SerializationVisitorInterface; | ||
|
||
final class JsonSerializableHandler | ||
{ | ||
public function __invoke(SerializationVisitorInterface $visitor, \JsonSerializable $object) | ||
{ | ||
return $object->jsonSerialize(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Benchmark\Performance; | ||
|
||
use JMS\Serializer\GraphNavigatorInterface; | ||
use JMS\Serializer\Handler\HandlerRegistryInterface; | ||
use JMS\Serializer\Handler\JsonSerializableHandler; | ||
use JMS\Serializer\Tests\Benchmark\AbstractSerializationBench; | ||
use JMS\Serializer\Tests\Fixtures\Author; | ||
|
||
class JsonSerializableBench extends AbstractSerializationBench | ||
{ | ||
protected function getFormat(): string | ||
{ | ||
return 'json'; | ||
} | ||
|
||
protected function configureHandlers(HandlerRegistryInterface $handlerRegistry) | ||
{ | ||
$handlerRegistry->registerHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, Author::class, 'json', new JsonSerializableHandler()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Fixtures; | ||
|
||
use JMS\Serializer\Annotation as Serializer; | ||
|
||
final class ObjectWithJsonSerialisable | ||
{ | ||
/** | ||
* @Serializer\Type("JsonSerializable") | ||
*/ | ||
#[Serializer\Type(name: \JsonSerializable::class)] | ||
public $author; | ||
|
||
public function __construct(Author $author) | ||
{ | ||
$this->author = $author; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace JMS\Serializer\Tests\Handler; | ||
|
||
use JMS\Serializer\Handler\JsonSerializableHandler; | ||
use JMS\Serializer\Tests\Fixtures\Author; | ||
use JMS\Serializer\Visitor\SerializationVisitorInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class JsonSerializableHandlerTest extends TestCase | ||
{ | ||
/** @var JsonSerializableHandler */ | ||
private $handler; | ||
|
||
public function testSerialize(): void | ||
{ | ||
$data = new Author('scyzoryck'); | ||
|
||
$serialized = ($this->handler)($this->createMock(SerializationVisitorInterface::class), $data); | ||
|
||
$this->assertEquals(['json_full_name' => 'scyzoryck'], $serialized); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->handler = new JsonSerializableHandler(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters