From 0e4326f1b86cce799179aadc6cdb2534b7c710d3 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Sat, 31 Aug 2024 19:23:10 +0800 Subject: [PATCH 1/3] feat: add macro in editor builder --- src/Html/Editor/Editor.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Html/Editor/Editor.php b/src/Html/Editor/Editor.php index e782a30..e5d23c0 100644 --- a/src/Html/Editor/Editor.php +++ b/src/Html/Editor/Editor.php @@ -4,6 +4,8 @@ use Illuminate\Support\Arr; use Illuminate\Support\Fluent; +use Illuminate\Support\Str; +use Illuminate\Support\Traits\Macroable; use Yajra\DataTables\Html\Editor\Fields\Field; use Yajra\DataTables\Html\HasAuthorizations; use Yajra\DataTables\Utilities\Helper; @@ -22,7 +24,9 @@ class Editor extends Fluent { use HasAuthorizations; - use HasEvents; + use HasEvents, Macroable { + Macroable::__call as macroCall; + } final public const DISPLAY_LIGHTBOX = 'lightbox'; @@ -314,4 +318,15 @@ public function hiddenOnEdit(array $fields): static { return $this->hiddenOn('edit', $fields); } + + public function __call($method, $parameters): mixed + { + if (Str::startsWith($method, 'on')) { + $event = Str::camel(substr($method, 2, strlen($method) - 2)); + + return $this->on($event, $parameters[0]); + } + + return $this->macroCall($method, $parameters); + } } From 2b3598bda4b8ef5e528d9251a37548bbeb8c4c0d Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Sat, 31 Aug 2024 19:25:57 +0800 Subject: [PATCH 2/3] fix: return type --- src/Html/Editor/Editor.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Html/Editor/Editor.php b/src/Html/Editor/Editor.php index e5d23c0..7fad16c 100644 --- a/src/Html/Editor/Editor.php +++ b/src/Html/Editor/Editor.php @@ -319,7 +319,7 @@ public function hiddenOnEdit(array $fields): static return $this->hiddenOn('edit', $fields); } - public function __call($method, $parameters): mixed + public function __call($method, $parameters): static { if (Str::startsWith($method, 'on')) { $event = Str::camel(substr($method, 2, strlen($method) - 2)); From e6a35dedfb7c585661a939e1e67ec9fff9053c35 Mon Sep 17 00:00:00 2001 From: Arjay Angeles Date: Sat, 31 Aug 2024 19:39:33 +0800 Subject: [PATCH 3/3] fix: static analysis --- src/Html/Editor/Editor.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Html/Editor/Editor.php b/src/Html/Editor/Editor.php index 7fad16c..15d63fc 100644 --- a/src/Html/Editor/Editor.php +++ b/src/Html/Editor/Editor.php @@ -327,6 +327,12 @@ public function __call($method, $parameters): static return $this->on($event, $parameters[0]); } - return $this->macroCall($method, $parameters); + $macroCall = $this->macroCall($method, $parameters); + + if (! $macroCall instanceof Editor) { + abort(500, sprintf('Method %s::%s must return an Editor instance.', static::class, $method)); + } + + return $this; } }