From 250f73cdd25ca5a0e01717dff5f9ba05e0aa8892 Mon Sep 17 00:00:00 2001 From: itsrafsanjani Date: Sun, 6 Oct 2024 18:34:52 +0600 Subject: [PATCH] feat: validation rule and isValid method add --- composer.json | 5 +- src/LaravelEditorJs.php | 21 +++++++ src/Rules/EditorJsRule.php | 126 +++++++++++++++++++++++++++++++++++++ src/helpers.php | 13 ++++ 4 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/Rules/EditorJsRule.php create mode 100644 src/helpers.php diff --git a/composer.json b/composer.json index f087032..4703dd3 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,10 @@ "autoload": { "psr-4": { "AlAminFirdows\\LaravelEditorJs\\": "src/" - } + }, + "files": [ + "src/helpers.php" + ] }, "autoload-dev": { "psr-4": { diff --git a/src/LaravelEditorJs.php b/src/LaravelEditorJs.php index 955fc4c..1543fb2 100644 --- a/src/LaravelEditorJs.php +++ b/src/LaravelEditorJs.php @@ -2,9 +2,11 @@ namespace AlAminFirdows\LaravelEditorJs; +use AlAminFirdows\LaravelEditorJs\Rules\EditorJsRule; use EditorJS\EditorJS; use EditorJS\EditorJSException; use Exception; +use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\View; use Illuminate\Support\Str; @@ -45,4 +47,23 @@ public function render(string $data): string throw new Exception($e->getMessage()); } } + + /** + * Check if the data is valid + * + * @param string $data + * @return bool + */ + public function isValid(string $data): bool + { + $validator = Validator::make(['data' => $data], [ + 'data' => ['required', 'string', new EditorJsRule] + ]); + + if ($validator->passes()) { + return true; + } + + return false; + } } diff --git a/src/Rules/EditorJsRule.php b/src/Rules/EditorJsRule.php new file mode 100644 index 0000000..e46f08f --- /dev/null +++ b/src/Rules/EditorJsRule.php @@ -0,0 +1,126 @@ +handleEditorJSException($e, $decodedData['blocks'] ?? [], $fail); + } + } + + /** + * Handle EditorJS exceptions and call the fail closure with appropriate messages + * + * @param EditorJSException $e + * @param array $blocks + * @param Closure(string): void $fail + */ + private function handleEditorJSException(EditorJSException $e, array $blocks, Closure $fail): void + { + $message = $e->getMessage(); + + if (preg_match('/Tool `(.+)` not found/', $message, $matches)) { + $invalidTool = $matches[1]; + $blockIndex = $this->findBlockIndex($blocks, $invalidTool); + + $fail("Block type '{$invalidTool}' is not supported" . + ($blockIndex !== null ? " (found in block {$blockIndex})" : "")); + + } elseif (preg_match('/Not found required param `(.+)`/', $message, $matches)) { + $missingParam = $matches[1]; + $location = $this->findParameterLocation($blocks, $missingParam); + + $errorMessage = "Missing required parameter '{$missingParam}'"; + if ($location) { + $errorMessage .= " in block {$location['blockIndex']} (type: {$location['blockType']})"; + } + + $fail($errorMessage); + + } elseif (preg_match('/Found extra param `(.+)`/', $message, $matches)) { + $extraParam = $matches[1]; + $location = $this->findParameterLocation($blocks, $extraParam); + + $errorMessage = "Unexpected parameter '{$extraParam}'"; + if ($location) { + $errorMessage .= " in block {$location['blockIndex']} (type: {$location['blockType']})"; + } + + $fail($errorMessage); + + } else { + $fail($message); + } + } + + /** + * Find block index by type + * + * @param array $blocks + * @param string $type + * @return int|null + */ + private function findBlockIndex(array $blocks, string $type): ?int + { + foreach ($blocks as $index => $block) { + if (isset($block['type']) && $block['type'] === $type) { + return $index; + } + } + return null; + } + + /** + * Find parameter location in blocks + * + * @param array $blocks + * @param string $param + * @return array|null + */ + private function findParameterLocation(array $blocks, string $param): ?array + { + foreach ($blocks as $index => $block) { + if (isset($block['data']) && array_key_exists($param, $block['data'])) { + return [ + 'blockIndex' => $index, + 'blockType' => $block['type'] ?? 'unknown', + ]; + } + } + return null; + } +} diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..6e64464 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,13 @@ +