Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Elements/BodyComponents/MjNavbar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents;

use MadeByDenis\PhpMjmlRenderer\Elements\AbstractElement;

class MjNavbar extends AbstractElement
{
public const string TAG_NAME = 'mj-navbar';
public const bool ENDING_TAG = false;

protected array $allowedAttributes = [
'align' => ['unit' => 'string', 'type' => 'string', 'default_value' => 'center'],
'base-url' => ['unit' => 'string', 'type' => 'string', 'default_value' => ''],
];

protected array $defaultAttributes = ['align' => 'center'];

public function render(): string
{
$children = $this->getChildren() ?? [];
return $this->renderChildren($children, []);
}

public function getStyles(): array
{
return [];
}
}
53 changes: 53 additions & 0 deletions src/Elements/BodyComponents/MjNavbarLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents;

use MadeByDenis\PhpMjmlRenderer\Elements\AbstractElement;

class MjNavbarLink extends AbstractElement
{
public const string TAG_NAME = 'mj-navbar-link';
public const bool ENDING_TAG = true;

protected array $allowedAttributes = [
'color' => ['unit' => 'color', 'type' => 'color', 'default_value' => '#000000'],
'font-family' => [
'unit' => 'string',
'type' => 'string',
'default_value' => 'Ubuntu, Helvetica, Arial, sans-serif',
],
'font-size' => ['unit' => 'px', 'type' => 'string', 'default_value' => '13px'],
'href' => ['unit' => 'string', 'type' => 'string', 'default_value' => ''],
'padding' => ['unit' => 'px', 'type' => 'string', 'default_value' => '15px 10px'],
'target' => ['unit' => 'string', 'type' => 'string', 'default_value' => '_blank'],
];

protected array $defaultAttributes = [
'color' => '#000000',
'font-size' => '13px',
'padding' => '15px 10px',
'target' => '_blank',
];

public function render(): string
{
$href = $this->getAttribute('href');
$target = $this->getAttribute('target');
$aAttributes = $this->getHtmlAttributes(['style' => 'a']);
$content = $this->getContent();
return "<a href='$href' target='$target' $aAttributes>$content</a>";
}

public function getStyles(): array
{
return ['a' => [
'color' => $this->getAttribute('color'),
'font-family' => $this->getAttribute('font-family'),
'font-size' => $this->getAttribute('font-size'),
'padding' => $this->getAttribute('padding'),
'text-decoration' => 'none',
]];
}
}
13 changes: 13 additions & 0 deletions tests/Unit/Elements/BodyComponents/MjNavbarTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace MadeByDenis\PhpMjmlRenderer\Tests\Unit\Elements\BodyComponents;

use MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents\MjNavbar;
use MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents\MjNavbarLink;

it('MjNavbar has correct tag name', fn() => expect((new MjNavbar())->getTagName())->toBe('mj-navbar'));
it('MjNavbarLink has correct tag name', fn() => expect((new MjNavbarLink())->getTagName())->toBe('mj-navbar-link'));
it('MjNavbarLink renders link', function () {
$element = new MjNavbarLink(['href' => 'test.html'], 'Link Text');
expect($element->render())->toContain('<a')->toContain('test.html')->toContain('Link Text');
});