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(); + } +}