| 1 |
<?php |
| 2 |
|
| 3 |
namespace Tableberg\Renderer\Icon; |
| 4 |
|
| 5 |
use Tableberg\Renderer\Attrs\SvgIconAttrs; |
| 6 |
|
| 7 |
class IconRenderer { |
| 8 |
/** |
| 9 |
* @param array<string, mixed>|null $attributes |
| 10 |
* @return string |
| 11 |
*/ |
| 12 |
public function render($attributes) { |
| 13 |
$attrs = IconAttrs::from_array($attributes); |
| 14 |
|
| 15 |
$className = 'tableberg-icon'; |
| 16 |
if ($attrs->behavior->equals('char')) { |
| 17 |
$className .= ' tableberg-icon-as-char'; |
| 18 |
} |
| 19 |
|
| 20 |
$wrapperStyles = [ |
| 21 |
'padding-top:' . $attrs->styles->padding->top->asAttr(), |
| 22 |
'padding-right:' . $attrs->styles->padding->right->asAttr(), |
| 23 |
'padding-bottom:' . $attrs->styles->padding->bottom->asAttr(), |
| 24 |
'padding-left:' . $attrs->styles->padding->left->asAttr(), |
| 25 |
]; |
| 26 |
|
| 27 |
if ($attrs->styles->background->isNotEmpty()) { |
| 28 |
$wrapperStyles[] = 'background:' . $attrs->styles->background->asAttr(); |
| 29 |
} |
| 30 |
if ($attrs->styles->rotation->value() != 0) { |
| 31 |
$wrapperStyles[] = 'transform:rotate(' . $attrs->styles->rotation->asAttr() . 'deg)'; |
| 32 |
} |
| 33 |
if ($attrs->styles->border->top->isNotEmpty()) { |
| 34 |
$wrapperStyles[] = 'border-top:' . $attrs->styles->border->top->asAttr(); |
| 35 |
} |
| 36 |
if ($attrs->styles->border->right->isNotEmpty()) { |
| 37 |
$wrapperStyles[] = 'border-right:' . $attrs->styles->border->right->asAttr(); |
| 38 |
} |
| 39 |
if ($attrs->styles->border->bottom->isNotEmpty()) { |
| 40 |
$wrapperStyles[] = 'border-bottom:' . $attrs->styles->border->bottom->asAttr(); |
| 41 |
} |
| 42 |
if ($attrs->styles->border->left->isNotEmpty()) { |
| 43 |
$wrapperStyles[] = 'border-left:' . $attrs->styles->border->left->asAttr(); |
| 44 |
} |
| 45 |
if ($attrs->styles->borderRadius->topLeft->isNotEmpty()) { |
| 46 |
$wrapperStyles[] = 'border-top-left-radius:' . $attrs->styles->borderRadius->topLeft->asAttr(); |
| 47 |
} |
| 48 |
if ($attrs->styles->borderRadius->topRight->isNotEmpty()) { |
| 49 |
$wrapperStyles[] = 'border-top-right-radius:' . $attrs->styles->borderRadius->topRight->asAttr(); |
| 50 |
} |
| 51 |
if ($attrs->styles->borderRadius->bottomRight->isNotEmpty()) { |
| 52 |
$wrapperStyles[] = 'border-bottom-right-radius:' . $attrs->styles->borderRadius->bottomRight->asAttr(); |
| 53 |
} |
| 54 |
if ($attrs->styles->borderRadius->bottomLeft->isNotEmpty()) { |
| 55 |
$wrapperStyles[] = 'border-bottom-left-radius:' . $attrs->styles->borderRadius->bottomLeft->asAttr(); |
| 56 |
} |
| 57 |
|
| 58 |
if ($attrs->styles->colorHover->isNotEmpty()) { |
| 59 |
$wrapperStyles[] = '--tableberg-icon-color-hover:' . $attrs->styles->colorHover->asAttr(); |
| 60 |
} elseif ($attrs->styles->color->isNotEmpty()) { |
| 61 |
$wrapperStyles[] = '--tableberg-icon-color-hover:' . $attrs->styles->color->asAttr(); |
| 62 |
} |
| 63 |
|
| 64 |
if ($attrs->styles->backgroundHover->isNotEmpty()) { |
| 65 |
$wrapperStyles[] = '--tableberg-icon-bg-hover:' . $attrs->styles->backgroundHover->asAttr(); |
| 66 |
} elseif ($attrs->styles->background->isNotEmpty()) { |
| 67 |
$wrapperStyles[] = '--tableberg-icon-bg-hover:' . $attrs->styles->background->asAttr(); |
| 68 |
} |
| 69 |
|
| 70 |
$size = $attrs->size->asAttr(); |
| 71 |
|
| 72 |
$iconHtml = ''; |
| 73 |
if ($attrs->icon->svg instanceof SvgIconAttrs) { |
| 74 |
$svgStyles = []; |
| 75 |
if ($attrs->styles->color->isNotEmpty()) { |
| 76 |
$svgStyles[] = 'fill:' . $attrs->styles->color->asAttr(); |
| 77 |
} |
| 78 |
|
| 79 |
$svgStyleAttr = ''; |
| 80 |
if (!empty($svgStyles)) { |
| 81 |
$svgStyleAttr = " style='" . implode(';', $svgStyles) . "'"; |
| 82 |
} |
| 83 |
|
| 84 |
$iconHtml = |
| 85 |
"<svg |
| 86 |
viewBox='{$attrs->icon->svg->viewBox->asAttr()}' |
| 87 |
xmlns='http://www.w3.org/2000/svg' |
| 88 |
height='{$size}' |
| 89 |
width='{$size}' |
| 90 |
{$svgStyleAttr} |
| 91 |
> |
| 92 |
<path d='{$attrs->icon->svg->path->asAttr()}' /> |
| 93 |
</svg>"; |
| 94 |
} |
| 95 |
|
| 96 |
if ($attrs->icon->url->isNotEmpty()) { |
| 97 |
$iconHtml = |
| 98 |
"<img |
| 99 |
src='{$attrs->icon->url->asUrl()}' |
| 100 |
alt='' |
| 101 |
style='height:{$size};width:{$size}' |
| 102 |
/>"; |
| 103 |
} |
| 104 |
|
| 105 |
if ($attrs->link->url->isNotEmpty()) { |
| 106 |
$target = $attrs->link->target->asAttr(); |
| 107 |
$relAttr = $attrs->link->target->equals('_blank') |
| 108 |
? " rel='noopener noreferrer'" |
| 109 |
: ''; |
| 110 |
|
| 111 |
$iconHtml = |
| 112 |
"<a |
| 113 |
href='{$attrs->link->url->asUrl()}' |
| 114 |
target='{$target}' |
| 115 |
{$relAttr} |
| 116 |
> |
| 117 |
{$iconHtml} |
| 118 |
</a>"; |
| 119 |
} |
| 120 |
|
| 121 |
$stylesString = implode(';', $wrapperStyles); |
| 122 |
|
| 123 |
return |
| 124 |
"<div |
| 125 |
class='{$className}' |
| 126 |
style='{$stylesString}' |
| 127 |
> |
| 128 |
{$iconHtml} |
| 129 |
</div>"; |
| 130 |
} |
| 131 |
} |
| 132 |
|