Skip to content

Commit

Permalink
feat: allow for custom implementations of component rendering data
Browse files Browse the repository at this point in the history
  • Loading branch information
smichaelsen committed Sep 9, 2024
1 parent b56e842 commit fda25e5
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 10 deletions.
8 changes: 3 additions & 5 deletions Classes/ContentObject/WebcomponentContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Sinso\Webcomponents\DataProviding\AssertionFailedException;
use Sinso\Webcomponents\DataProviding\ComponentInterface;
use Sinso\Webcomponents\Dto\ComponentRenderingData;
use Sinso\Webcomponents\Dto\ComponentRenderingDataInterface;
use Sinso\Webcomponents\Dto\Events\ComponentWillBeRendered;
use Sinso\Webcomponents\Dto\InputData;
use Sinso\Webcomponents\Rendering\ComponentRenderer;
Expand Down Expand Up @@ -77,7 +78,7 @@ public function render($conf = []): string
/**
* @param array<string, mixed> $conf
*/
private function evaluateTypoScriptConfiguration(ComponentRenderingData $componentRenderingData, array $conf): ComponentRenderingData
private function evaluateTypoScriptConfiguration(ComponentRenderingData $componentRenderingData, array $conf): ComponentRenderingDataInterface
{
if (is_array($conf['properties.'] ?? null)) {
foreach ($conf['properties.'] as $key => $value) {
Expand All @@ -94,12 +95,9 @@ private function evaluateTypoScriptConfiguration(ComponentRenderingData $compone
return $componentRenderingData;
}

private function renderMarkup(ComponentRenderingData $componentRenderingData): string
private function renderMarkup(ComponentRenderingDataInterface $componentRenderingData): string
{
$tagName = $componentRenderingData->getTagName();
if ($tagName === null) {
throw new AssertionFailedException('No tag name provided', 1722672898);
}
$content = $componentRenderingData->getTagContent();
$properties = $componentRenderingData->getTagProperties();

Expand Down
9 changes: 7 additions & 2 deletions Classes/Dto/ComponentRenderingData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace Sinso\Webcomponents\Dto;

class ComponentRenderingData
use Sinso\Webcomponents\DataProviding\AssertionFailedException;

class ComponentRenderingData implements ComponentRenderingDataInterface, TagPropertiesSettable, TagContentSettable, TagNameSettable
{
private ?string $tagContent = null;
private ?string $tagName = null;
Expand Down Expand Up @@ -44,8 +46,11 @@ public function setTagProperties(array $tagProperties): void
$this->tagProperties = $tagProperties;
}

public function getTagName(): ?string
public function getTagName(): string
{
if ($this->tagName === null) {
throw new AssertionFailedException('No tag name provided', 1722672898);
}
return $this->tagName;
}

Expand Down
15 changes: 15 additions & 0 deletions Classes/Dto/ComponentRenderingDataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Sinso\Webcomponents\Dto;

interface ComponentRenderingDataInterface
{
public function getTagContent(): ?string;

/**
* @return array<string, mixed>|null
*/
public function getTagProperties(): ?array;

public function getTagName(): string;
}
3 changes: 2 additions & 1 deletion Classes/Dto/Events/ComponentWillBeRendered.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sinso\Webcomponents\Dto\Events;

use Sinso\Webcomponents\Dto\ComponentRenderingData;
use Sinso\Webcomponents\Dto\ComponentRenderingDataInterface;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;

/**
Expand All @@ -16,6 +17,6 @@ class ComponentWillBeRendered
{
public function __construct(
public readonly ContentObjectRenderer $contentObjectRenderer,
public readonly ComponentRenderingData $componentRenderingData,
public readonly ComponentRenderingDataInterface $componentRenderingData,
) {}
}
10 changes: 10 additions & 0 deletions Classes/Dto/TagContentSettable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sinso\Webcomponents\Dto;

interface TagContentSettable
{
public function setTagContent(?string $tagContent): void;
}
10 changes: 10 additions & 0 deletions Classes/Dto/TagNameSettable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Sinso\Webcomponents\Dto;

interface TagNameSettable
{
public function setTagName(string $tagName): void;
}
12 changes: 12 additions & 0 deletions Classes/Dto/TagPropertiesSettable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Sinso\Webcomponents\Dto;

interface TagPropertiesSettable
{
public function setTagProperty(string $key, mixed $value): void;

public function setTagProperties(array $tagProperties): void;

Check failure on line 11 in Classes/Dto/TagPropertiesSettable.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Sinso\Webcomponents\Dto\TagPropertiesSettable::setTagProperties() has parameter $tagProperties with no value type specified in iterable type array.
}
4 changes: 2 additions & 2 deletions Classes/Rendering/ComponentRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ class ComponentRenderer
/**
* @param array<string, mixed> $properties
*/
public function renderComponent(string $tagName, ?string $content, array $properties): string
public function renderComponent(string $tagName, ?string $content, ?array $properties): string
{
/** @var TagBuilder $tagBuilder */
$tagBuilder = GeneralUtility::makeInstance(TagBuilder::class);
$tagBuilder->setTagName($tagName);
if (!empty($content)) {
$tagBuilder->setContent($content);
}
foreach ($properties as $key => $value) {
foreach ($properties ?? [] as $key => $value) {
if ($value === null) {
continue;
}
Expand Down

0 comments on commit fda25e5

Please sign in to comment.