Skip to content

Commit

Permalink
Moved rendering to strategy pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesdordoy committed Oct 22, 2024
1 parent cfea55f commit 06d6b56
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 23 deletions.
13 changes: 13 additions & 0 deletions src/Contracts/RenderStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace JamesDordoy\HTMLable\Contracts;

use Illuminate\Support\HtmlString;
use JamesDordoy\HTMLable\Enums\Tables\Element;
use JamesDordoy\HTMLable\Models\Value;

interface RenderStrategy
{
public function renderValue(Value $value): string;
public function renderChild(Element $child): HtmlString;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class RenderDocumentPDFController
{
public function __invoke(Document $document): HtmlString
public function __invoke(Document $document)
{
return $document->render();
}
Expand Down
36 changes: 14 additions & 22 deletions src/Models/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -33,45 +36,34 @@ 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}</{$tag}>");
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} />");
}

return new HtmlString("<{$tag} data-id='{$this->uuid}' {$attributes}>{$html}</{$tag}>");
}
}
}
19 changes: 19 additions & 0 deletions src/Renderer/DownloadRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace JamesDordoy\HTMLable\Renderer;

use Illuminate\Support\HtmlString;
use JamesDordoy\HTMLable\Contracts\RenderStrategy;

class DownloadRenderer implements RenderStrategy
{
public function renderValue($value): string
{
return $value->renderAttributesLocally();
}

public function renderChild($child): HtmlString
{
return $child->renderForDownload();
}
}
19 changes: 19 additions & 0 deletions src/Renderer/HTMLRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace JamesDordoy\HTMLable\Renderer;

use Illuminate\Support\HtmlString;
use JamesDordoy\HTMLable\Contracts\RenderStrategy;

class HTMLRenderer implements RenderStrategy
{
public function renderValue($value): string
{
return $value->render();
}

public function renderChild($child): HtmlString
{
return $child->render();
}
}

0 comments on commit 06d6b56

Please sign in to comment.