Skip to content

Commit

Permalink
Make ContentState JsonSerializable
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Voskoboinyk committed Mar 23, 2017
1 parent 7da85ee commit 4731fdf
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ var_dump($contentState->getEntity($contentState->blocks[0]->characterList[0]->en
$contentState = \Prezly\DraftPhp\Converter::convertFromJson($json);

$serializedJson = \Prezly\DraftPhp\Serializer::serialize($contentState);
// or
$serializedJson = json_serialize($contentState);

// now $json is equivalent to $serializedJson (formatting and order may differ though)
// see SerializerTest for examples
Expand Down
16 changes: 15 additions & 1 deletion lib/Model/ContentState.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Prezly\DraftPhp\Model;

use InvalidArgumentException;
use JsonSerializable;
use Prezly\DraftPhp\Constants\BlockType;
use Prezly\DraftPhp\Serializer;

/**
* @property ContentBlock[] $blocks
* @property EntityInstance[] $entityMap
*/
class ContentState
class ContentState implements JsonSerializable
{
/** @var ContentBlock[] */
private $_blocks;
Expand Down Expand Up @@ -80,4 +82,16 @@ public function __get($name)
// public read-only access to private properties
return $this->{'_' . $name};
}

/**
* @return RawDraftContentState|\stdClass
*/
public function jsonSerialize()
{
$serializer = new Serializer();

$rawContentState = $serializer->serializeRaw($this);

return $rawContentState;
}
}
6 changes: 3 additions & 3 deletions lib/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ public static function serialize(ContentState $contentState, int $options = 0):
{
$serializer = new self();

$rawContentState = $serializer ->serializeContentState($contentState);
$rawContentState = $serializer->serializeRaw($contentState);

return json_encode($rawContentState, $options);
}

/**
* @param ContentState $contentState
* @return \Prezly\DraftPhp\Model\RawDraftContentState
* @return \stdClass|\Prezly\DraftPhp\Model\RawDraftContentState
*/
private function serializeContentState(ContentState $contentState)
public function serializeRaw(ContentState $contentState): \stdClass
{
/**
* @var $rawContentState \Prezly\DraftPhp\Model\RawDraftContentState
Expand Down
33 changes: 33 additions & 0 deletions tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@ public function it_should_serialize_content_to_equivalent_json(string $json)
$this->assertJsonStringEqualsJsonString(trim($json), $serialized);
}

/**
* @test
* @dataProvider fixtures
*
* @param string $json
*/
public function it_should_serialize_to_raw_content_state(string $json)
{
$rawContentState = $this->serializeRaw($this->convert($json));

$this->assertJsonStringEqualsJsonString($json, json_encode($rawContentState));
}

/**
* @test
* @dataProvider fixtures
*
* @param string $json
*/
public function content_state_instance_should_be_json_serializable(string $json)
{
$contentState = $this->convert($json);

$this->assertJsonStringEqualsJsonString($json, json_encode($contentState));
}

public function fixtures()
{
$handle = opendir(self::FIXTURES_DIR);
Expand Down Expand Up @@ -52,4 +78,11 @@ private function serialize(ContentState $contentState, int $options = JSON_PRETT
{
return Serializer::serialize($contentState, $options);
}

private function serializeRaw(ContentState $contentState)
{
$serializer = new Serializer();

return $serializer->serializeRaw($contentState);
}
}

0 comments on commit 4731fdf

Please sign in to comment.