Skip to content

Commit

Permalink
Refactor template handling in several classes. (#185)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Dec 31, 2023
1 parent 0fee41b commit bcf5a84
Show file tree
Hide file tree
Showing 12 changed files with 76 additions and 11 deletions.
5 changes: 4 additions & 1 deletion src/Attribute/Custom/HasTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace PHPForge\Html\Attribute\Custom;

use function count;
use function explode;
use function strtr;

/**
* Is used by widgets that implement the template method.
*/
trait HasTemplate
{
protected string $template = '';
protected array $tokenValue = [];

/**
Expand Down Expand Up @@ -49,7 +52,7 @@ private function renderTemplate(string $template, array $tokenValues): string
}

if ($result !== '' && $key < count($tokens) - 1) {
$result = strtr($tokens[$key + 1], $tokenValues) !== '' ? $result . "\n" : $result;
$result = strtr($tokens[$key + 1], $tokenValues) !== '' ? $result . PHP_EOL : $result;
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/Base/AbstractElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,16 @@ abstract class AbstractElement extends Element

protected array $attributes = [];
protected string $tagName = '';
protected string $template = '{prefix}\n{tag}\n{suffix}';

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
'template()' => ['{prefix}\n{tag}\n{suffix}'],
];
}

/**
* Generate the HTML representation of the element.
Expand Down
12 changes: 11 additions & 1 deletion src/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ final class Button extends Element
protected bool $container = false;
protected string $containerTag = 'div';
protected string $tagName = 'button';
protected string $template = '{prefix}\n{tag}\n{suffix}';
protected string $type = 'button';

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
'template()' => ['{prefix}\n{tag}\n{suffix}'],
];
}

/**
* Set the button type to `submit`.
*
Expand Down Expand Up @@ -104,6 +113,7 @@ protected function run(): string
->suffixContainerAttributes($this->suffixContainerAttributes)
->suffixContainerTag($this->suffixContainerTag)
->tagName($this->tagName)
->template($this->template)
->type($type)
->render()
);
Expand Down
11 changes: 10 additions & 1 deletion src/ButtonToggle.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ final class ButtonToggle extends Element
protected bool $iconContainer = false;
protected string $iconContainerTag = 'div';
protected string $tagName = 'button';
protected string $template = '{toggle}\n{icon}\n{content}';
protected bool $toggle = true;
protected string $toggleTag = 'span';

Expand All @@ -51,6 +50,16 @@ public function link(): static
return $new;
}

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
'template()' => ['{toggle}\n{icon}\n{content}'],
];
}

/**
* Generate the HTML representation of the element.
*
Expand Down
5 changes: 4 additions & 1 deletion src/Input/Base/AbstractButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ abstract class AbstractButton extends Element implements LabelInterface, InputIn
use Attribute\Input\HasValue;

protected array $attributes = [];
protected string $template = '{prefix}\n{label}\n{tag}\n{suffix}';
protected string $type = 'button';

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
'container()' => [true],
'template()' => ['{prefix}\n{label}\n{tag}\n{suffix}'],
];
}

Expand Down
3 changes: 3 additions & 0 deletions src/Input/Base/AbstractButtonGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ public function individualContainer(bool $value): static
return $new;
}

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
Expand Down
3 changes: 3 additions & 0 deletions src/Input/Base/AbstractChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ abstract class AbstractChoiceList extends Element implements InputInterface, Lab
protected array $items = [];
protected string $separator = '';

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
Expand Down
11 changes: 10 additions & 1 deletion src/Input/Base/AbstractHidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,16 @@ abstract class AbstractHidden extends Element implements HiddenInterface
use Attribute\Input\HasValue;

protected array $attributes = [];
protected string $template = '{prefix}\n{tag}\n{suffix}';

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
'template()' => ['{prefix}\n{tag}\n{suffix}'],
];
}

protected function run(): string
{
Expand Down
11 changes: 10 additions & 1 deletion src/Input/Base/AbstractInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ abstract class AbstractInput extends Element implements InputInterface
use Attribute\Input\HasValue;

protected array $attributes = [];
protected string $template = '{prefix}\n{tag}\n{suffix}';

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
'template()' => ['{prefix}\n{tag}\n{suffix}'],
];
}

protected function buildInputTag(array $attributes, string $type): string
{
Expand Down
11 changes: 10 additions & 1 deletion src/Input/Base/AbstractInputChoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ abstract class AbstractInputChoice extends Element implements LabelInterface, In
protected string $containerTag = 'div';
protected string $separator = PHP_EOL;
protected string $tagName = '';
protected string $template = '{prefix}\n{unchecktag}\n{tag}\n{label}\n{suffix}';

/**
* This method is used to configure the widget with the provided default definitions.
*/
public function loadDefaultDefinitions(): array
{
return [
'template()' => ['{prefix}\n{unchecktag}\n{tag}\n{label}\n{suffix}'],
];
}

/**
* Generate the HTML representation of the element.
Expand Down
2 changes: 0 additions & 2 deletions tests/Attribute/Custom/HasTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ public function testImmutability(): void
{
$instance = new class () {
use HasTemplate;

protected string $template = '{input}';
};

$this->assertNotSame($instance, $instance->template(''));
Expand Down
2 changes: 1 addition & 1 deletion tests/Button/RenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ public function testTemplate(): void
<button id="button-658716145f1d9" type="button"></button>
suffix
HTML,
Button::widget()->id('button-658716145f1d9')->template('{tag}{suffix}')->suffix('suffix')->render()
Button::widget()->id('button-658716145f1d9')->template('{tag}\n{suffix}')->suffix('suffix')->render()
);
}

Expand Down

0 comments on commit bcf5a84

Please sign in to comment.