Skip to content

Commit 8703373

Browse files
authored
Merge pull request #31 from dingo-d/feature/mj-social
Implement mj-social components
2 parents 408fa5f + 870ee7f commit 8703373

File tree

3 files changed

+144
-0
lines changed

3 files changed

+144
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents;
6+
7+
use MadeByDenis\PhpMjmlRenderer\Elements\AbstractElement;
8+
9+
class MjSocial extends AbstractElement
10+
{
11+
public const string TAG_NAME = 'mj-social';
12+
public const bool ENDING_TAG = false;
13+
14+
protected array $allowedAttributes = [
15+
'align' => ['unit' => 'string', 'type' => 'alignment', 'default_value' => 'center'],
16+
'border-radius' => ['unit' => 'px', 'type' => 'string', 'default_value' => '3px'],
17+
'color' => ['unit' => 'color', 'type' => 'color', 'default_value' => '#333333'],
18+
'font-family' => [
19+
'unit' => 'string',
20+
'type' => 'string',
21+
'default_value' => 'Ubuntu, Helvetica, Arial, sans-serif',
22+
],
23+
'font-size' => ['unit' => 'px', 'type' => 'string', 'default_value' => '13px'],
24+
'icon-size' => ['unit' => 'px', 'type' => 'string', 'default_value' => '20px'],
25+
'mode' => ['unit' => 'string', 'type' => 'string', 'default_value' => 'horizontal'],
26+
'padding' => ['unit' => 'px', 'type' => 'string', 'default_value' => '10px 25px'],
27+
];
28+
29+
protected array $defaultAttributes = [
30+
'align' => 'center',
31+
'border-radius' => '3px',
32+
'color' => '#333333',
33+
'font-size' => '13px',
34+
'icon-size' => '20px',
35+
'mode' => 'horizontal',
36+
'padding' => '10px 25px',
37+
];
38+
39+
public function render(): string
40+
{
41+
$children = $this->getChildren() ?? [];
42+
$content = $this->renderChildren($children, []);
43+
$divAttributes = $this->getHtmlAttributes(['style' => 'div']);
44+
return "<div $divAttributes>$content</div>";
45+
}
46+
47+
public function getChildContext(): array
48+
{
49+
return [
50+
...$this->context,
51+
'border-radius' => $this->getAttribute('border-radius'),
52+
'color' => $this->getAttribute('color'),
53+
'font-family' => $this->getAttribute('font-family'),
54+
'font-size' => $this->getAttribute('font-size'),
55+
'icon-size' => $this->getAttribute('icon-size'),
56+
];
57+
}
58+
59+
public function getStyles(): array
60+
{
61+
return ['div' => ['text-align' => $this->getAttribute('align')]];
62+
}
63+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents;
6+
7+
use MadeByDenis\PhpMjmlRenderer\Elements\AbstractElement;
8+
9+
class MjSocialElement extends AbstractElement
10+
{
11+
public const string TAG_NAME = 'mj-social-element';
12+
public const bool ENDING_TAG = true;
13+
14+
protected array $allowedAttributes = [
15+
'align' => ['unit' => 'string', 'type' => 'alignment', 'default_value' => 'left'],
16+
'background-color' => ['unit' => 'color', 'type' => 'color', 'default_value' => ''],
17+
'border-radius' => ['unit' => 'px', 'type' => 'string', 'default_value' => ''],
18+
'color' => ['unit' => 'color', 'type' => 'color', 'default_value' => ''],
19+
'font-family' => ['unit' => 'string', 'type' => 'string', 'default_value' => ''],
20+
'font-size' => ['unit' => 'px', 'type' => 'string', 'default_value' => ''],
21+
'href' => ['unit' => 'string', 'type' => 'string', 'default_value' => ''],
22+
'icon-size' => ['unit' => 'px', 'type' => 'string', 'default_value' => ''],
23+
'name' => ['unit' => 'string', 'type' => 'string', 'default_value' => ''],
24+
'padding' => ['unit' => 'px', 'type' => 'string', 'default_value' => '4px'],
25+
'src' => ['unit' => 'string', 'type' => 'string', 'default_value' => ''],
26+
'target' => ['unit' => 'string', 'type' => 'string', 'default_value' => '_blank'],
27+
];
28+
29+
protected array $defaultAttributes = [
30+
'align' => 'left',
31+
'padding' => '4px',
32+
'target' => '_blank',
33+
];
34+
35+
public function render(): string
36+
{
37+
$href = $this->getAttribute('href');
38+
$target = $this->getAttribute('target');
39+
$src = $this->getAttribute('src');
40+
$iconSize = $this->getAttribute('icon-size') ?: $this->context['icon-size'] ?? '20px';
41+
$content = $this->getContent();
42+
43+
$aAttributes = $this->getHtmlAttributes(['style' => 'a']);
44+
$icon = $src ? "<img src='$src' width='$iconSize' height='$iconSize' />" : '';
45+
46+
return "<a href='$href' target='$target' $aAttributes>$icon $content</a>";
47+
}
48+
49+
public function getStyles(): array
50+
{
51+
$color = $this->getAttribute('color') ?: $this->context['color'] ?? '#333333';
52+
$fontFamily = $this->getAttribute('font-family') ?:
53+
$this->context['font-family'] ?? 'Ubuntu, Helvetica, Arial, sans-serif';
54+
55+
return ['a' => [
56+
'color' => $color,
57+
'font-family' => $fontFamily,
58+
'padding' => $this->getAttribute('padding'),
59+
'text-decoration' => 'none',
60+
]];
61+
}
62+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace MadeByDenis\PhpMjmlRenderer\Tests\Unit\Elements\BodyComponents;
4+
5+
use MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents\MjSocial;
6+
use MadeByDenis\PhpMjmlRenderer\Elements\BodyComponents\MjSocialElement;
7+
8+
it('MjSocial has correct tag name', fn() => expect((new MjSocial())->getTagName())->toBe('mj-social'));
9+
it('MjSocial has correct defaults', fn() => expect((new MjSocial())->getAttribute('mode'))->toBe('horizontal'));
10+
11+
it('MjSocialElement has correct tag name', function () {
12+
expect((new MjSocialElement())->getTagName())->toBe('mj-social-element');
13+
});
14+
15+
it('MjSocialElement renders', function () {
16+
$element = new MjSocialElement(['href' => 'https://facebook.com'], 'Facebook');
17+
$out = $element->render();
18+
expect($out)->toContain('<a')->toContain('https://facebook.com')->toContain('Facebook');
19+
});

0 commit comments

Comments
 (0)