|
| 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 | +} |
0 commit comments