diff --git a/CHANGELOG.md b/CHANGELOG.md index 5036049..36d077a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/README.md b/README.md index 1b6edba..59a4071 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/Tests/Api/StockTest.php b/Tests/Api/StockTest.php index fc66b7d..f624ba3 100644 --- a/Tests/Api/StockTest.php +++ b/Tests/Api/StockTest.php @@ -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 = "

Heading 1

"; @@ -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 * diff --git a/src/Interfaces/ParserInterface.php b/src/Interfaces/ParserInterface.php index b7efa2d..d2ec706 100644 --- a/src/Interfaces/ParserInterface.php +++ b/src/Interfaces/ParserInterface.php @@ -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 diff --git a/src/Parser/Parse.php b/src/Parser/Parse.php index e5543a7..5540442 100644 --- a/src/Parser/Parse.php +++ b/src/Parser/Parse.php @@ -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; @@ -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 @@ -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; } } @@ -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 @@ -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; + } }