From 06d6b56f130d7c1495b6ba7296b198647252f75f Mon Sep 17 00:00:00 2001 From: James Dordoy Date: Tue, 22 Oct 2024 10:41:22 +0100 Subject: [PATCH] Moved rendering to strategy pattern --- src/Contracts/RenderStrategy.php | 13 +++++++ .../Documents/RenderDocumentPDFController.php | 2 +- src/Models/Element.php | 36 ++++++++----------- src/Renderer/DownloadRenderer.php | 19 ++++++++++ src/Renderer/HTMLRenderer.php | 19 ++++++++++ 5 files changed, 66 insertions(+), 23 deletions(-) create mode 100644 src/Contracts/RenderStrategy.php create mode 100644 src/Renderer/DownloadRenderer.php create mode 100644 src/Renderer/HTMLRenderer.php diff --git a/src/Contracts/RenderStrategy.php b/src/Contracts/RenderStrategy.php new file mode 100644 index 0000000..5f3bc68 --- /dev/null +++ b/src/Contracts/RenderStrategy.php @@ -0,0 +1,13 @@ +render(); } diff --git a/src/Models/Element.php b/src/Models/Element.php index df3701a..fb0e69e 100644 --- a/src/Models/Element.php +++ b/src/Models/Element.php @@ -7,7 +7,10 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Support\HtmlString; +use JamesDordoy\HTMLable\Contracts\RenderStrategy; use JamesDordoy\HTMLable\Enums\HtmlElement; +use JamesDordoy\HTMLable\Renderer\HTMLRenderer; +use JamesDordoy\HTMLable\Renderer\DownloadRenderer; class Element extends Model implements Renderable { @@ -33,40 +36,29 @@ public function values(): HasMany return $this->hasMany(Value::class); } + // Strategy Pattern for rendering public function render(): HtmlString { - $tag = $this->tag; - $attributes = $this->values->map(fn ($value) => $value->render())->implode(' '); - $contentValue = $this->values->firstWhere('key', 'content')->value ?? ''; - - $childrenHtml = ''; - foreach ($this->children as $child) { - $childrenHtml .= $child->render(); - } - - // tap($childrenHtml = '', fn () => $this->children->map(fn($child) => $childrenHtml .= $child->render())); - - $html = $contentValue.$childrenHtml; - - if (in_array($tag, HtmlElement::getSelfClosingElements())) { - return new HtmlString("<{$tag} data-id='{$this->uuid}' {$attributes} />"); - } - - return new HtmlString("<{$tag} data-id='{$this->uuid}' {$attributes}>{$html}"); + return $this->baseRender(new HTMLRenderer()); } public function renderForDownload(): HtmlString + { + return $this->baseRender(new DownloadRenderer()); + } + + private function baseRender(RenderStrategy $strategy): HtmlString { $tag = $this->tag; - $attributes = $this->values->map(fn ($value) => $value->renderAttributesLocally())->implode(' '); + $attributes = $this->values->map(fn ($value) => $strategy->renderValue($value))->implode(' '); $contentValue = $this->values->firstWhere('key', 'content')->value ?? ''; $childrenHtml = ''; foreach ($this->children as $child) { - $childrenHtml .= $child->renderForDownload(); + $childrenHtml .= $strategy->renderChild($child); } - $html = $contentValue.$childrenHtml; + $html = $contentValue . $childrenHtml; if (in_array($tag, HtmlElement::getSelfClosingElements())) { return new HtmlString("<{$tag} data-id='{$this->uuid}' {$attributes} />"); @@ -74,4 +66,4 @@ public function renderForDownload(): HtmlString return new HtmlString("<{$tag} data-id='{$this->uuid}' {$attributes}>{$html}"); } -} +} \ No newline at end of file diff --git a/src/Renderer/DownloadRenderer.php b/src/Renderer/DownloadRenderer.php new file mode 100644 index 0000000..485cab7 --- /dev/null +++ b/src/Renderer/DownloadRenderer.php @@ -0,0 +1,19 @@ +renderAttributesLocally(); + } + + public function renderChild($child): HtmlString + { + return $child->renderForDownload(); + } +} diff --git a/src/Renderer/HTMLRenderer.php b/src/Renderer/HTMLRenderer.php new file mode 100644 index 0000000..3b7c0a5 --- /dev/null +++ b/src/Renderer/HTMLRenderer.php @@ -0,0 +1,19 @@ +render(); + } + + public function renderChild($child): HtmlString + { + return $child->render(); + } +}