Skip to content
This repository has been archived by the owner on Sep 18, 2023. It is now read-only.

Commit

Permalink
Merge pull request #124 from deanblackborough/v3.17.4
Browse files Browse the repository at this point in the history
v3.17.4
  • Loading branch information
deanblackborough authored May 1, 2019
2 parents adfcfc9 + 860c008 commit 78bb8bb
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@

Full changelog for PHP Quill Renderer

## v3.17.4 - 2019-05-01

* Allow decoded json to be passed in rather than the library decode it, thank you [Lode Claassen](https://github.com/lode).
* Validation code DRY, thank you [Lode Claassen](https://github.com/lode).

## v3.17.3 - 2019-04-10

* Missing supported format in exception methods, thank you [Lode Claassen](https://github.com/lode).
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ for use under the MIT License (MIT).
* [davidraijmakers](https://github.com/davidraijmakers) [Issue #108] - Children not supported with headers.
* [philippkuehn](https://github.com/philippkuehn) [Issue #109] - Multiple list output incorrect and paragraphs not being closed.
* [mechanicalgux](https://github.com/mechanicalgux) [Issue #117] - Compound deltas don't know that they can be links.
* [Lode Claassen](https://github.com/lode) - Missing supported format in exception messages.
* [Lode Claassen](https://github.com/lode) [PR121] - Missing supported format in exception messages.
* [Lode Claassen](https://github.com/lode) [PR122] - Validation code DRY.
* [Lode Claassen](https://github.com/lode) [PR123] - Allow already decoded json to be passed to parser.

## Coding standard credit
## Coding standards credits

* [Lode Claassen](https://github.com/lode) [PR #113] - Incorrect case in keyword.
29 changes: 29 additions & 0 deletions Tests/Api/StockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class StockTest extends \PHPUnit\Framework\TestCase
{
private $delta_null_insert = '{"ops":[{"insert":"Heading 1"},{"insert":null},{"attributes":{"header":1},"insert":"\n"}]}';
private $delta_header = '{"ops":[{"insert":"Heading 1"},{"attributes":{"header":1},"insert":"\n"}]}';
private $delta_header_array = ["ops" => [["insert" => "Heading 1"], ["attributes" => ["header" => 1], "insert" => "\n"]]];
private $delta_header_invalid = '{"ops":[{"insert":"Heading 1"},{"attributes":{"header":1},"insert":"\n"}}';

private $expected_null_insert = "<h1>Heading 1</h1>";
Expand Down Expand Up @@ -77,6 +78,34 @@ public function testMultipleInstancesInScript()
);
}

/**
* Test passing an already decoded array of a json string. Load should not try to decode again.
*
* @return void
* @throws \Exception
*/
public function testLoadAlreadyDecoded()
{
$result = null;

$parser = new \DBlackborough\Quill\Parser\Html();

try {
$parser->load($this->delta_header_array)->parse();

$renderer = new \DBlackborough\Quill\Renderer\Html();
$result = $renderer->load($parser->deltas())->render();
} catch (\Exception $e) {
$this->fail(__METHOD__ . 'failure, ' . $e->getMessage());
}

$this->assertEquals(
$this->expected_header,
trim($result),
__METHOD__ . ' Array load call failure'
);
}

/**
* Test to see if an exception is thrown when an invalid parser is requested
*
Expand Down
6 changes: 3 additions & 3 deletions src/Interfaces/ParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ interface ParserInterface
* Load the deltas string, checks the json is valid and can be decoded
* and then saves the decoded array to the the $quill_json property
*
* @param string $quill_json Quill json string
* @param array|string $quill_json Quill json string
*
* @return Parse
* @throws \InvalidArgumentException Throws an exception if there was an error decoding the json
*/
public function load(string $quill_json): Parse;
public function load($quill_json): Parse;

/**
* Load multiple delta strings, checks the json is valid for each index,
* ensures they can be decoded and the saves each decoded array to the
* $quill_json_stack property indexed by the given key
*
* @param array An array of $quill json strings, returnable via array index
* @param array An array of $quill json array|strings, returnable via array index
*
* @return Parse
* @throws \InvalidArgumentException Throws an exception if there was an error decoding the json
Expand Down
48 changes: 36 additions & 12 deletions src/Parser/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,19 @@ public function __construct()
* Load the deltas string, checks the json is valid and can be decoded
* and then saves the decoded array to the the $quill_json property
*
* @param string $quill_json Quill json string
* @param array|string $quill_json Quill json string
*
* @return Parse
* @throws \InvalidArgumentException Throws an exception if there was an error decoding the json
*/
public function load(string $quill_json): Parse
public function load($quill_json): Parse
{
$this->quill_json = json_decode($quill_json, true);
$this->quill_json = $quill_json;
if (is_string($this->quill_json) === true) {
$this->quill_json = json_decode($quill_json, true);
}

if (is_array($this->quill_json) === true && count($this->quill_json) > 0) {
if ($this->isValidDeltaJson($this->quill_json)) {
$this->valid = true;
$this->deltas = [];
return $this;
Expand All @@ -97,7 +100,7 @@ public function load(string $quill_json): Parse
* ensures they can be decoded and the saves each decoded array to the
* $quill_json_stack property indexed by the given key
*
* @param array An array of $quill json strings, returnable via array index
* @param array An array of $quill json arrays|strings, returnable via array index
*
* @return Parse
* @throws \InvalidArgumentException Throws an exception if there was an error decoding the json
Expand All @@ -106,10 +109,12 @@ public function loadMultiple(array $quill_json): Parse
{
$this->deltas_stack = [];

foreach ($quill_json as $index => $json) {
$json_stack_value = json_decode($json, true);
foreach ($quill_json as $index => $json_stack_value) {
if (is_string($json_stack_value) === true) {
$json_stack_value = json_decode($json_stack_value, true);
}

if (is_array($json_stack_value) === true && count($json_stack_value) > 0) {
if ($this->isValidDeltaJson($json_stack_value)) {
$this->quill_json_stack[$index] = $json_stack_value;
}
}
Expand Down Expand Up @@ -232,10 +237,7 @@ public function splitOnSingleNewlineOccurrences(string $insert, bool $newlines =
*/
public function parse(): bool
{
if (
$this->valid === true &&
array_key_exists('ops', $this->quill_json) === true
) {
if ($this->valid === true) {
/**
* Before processing through the deltas, generate new deltas by splliting
* on all new lines, will make it much simpler to work out which
Expand Down Expand Up @@ -495,4 +497,26 @@ public function video(array $quill)
{
$this->deltas[] = new $this->class_delta_video($quill['insert']['video']);
}

/**
* Checks the delta json is valid and can be decoded
*
* @param $quill_json Quill json string
*
* @return boolean
*/
private function isValidDeltaJson($quill_json): bool
{
if (is_array($quill_json) === false) {
return false;
}
if (count($quill_json) === 0) {
return false;
}
if (array_key_exists('ops', $quill_json) === false) {
return false;
}

return true;
}
}

0 comments on commit 78bb8bb

Please sign in to comment.