Passed
Push — master ( 4bc322...e35e92 )
by Peter
04:39
created

ButtonWithIcon::getText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Html\Component;
6
7
use AbterPhp\Framework\Html\Helper\StringHelper;
8
use AbterPhp\Framework\Html\IComponent;
9
use AbterPhp\Framework\Html\INode;
10
use AbterPhp\Framework\Html\ITemplater;
11
12
class ButtonWithIcon extends Button implements ITemplater
13
{
14
    /**
15
     *   %1$s - text
16
     *   %2$s - icon
17
     */
18
    const DEFAULT_TEMPLATE = '%2$s %1$s';
19
20
    const INTENT_BUTTON_ICON = 'button-icon';
21
    const INTENT_BUTTON_TEXT = 'button-text';
22
23
    /** @var string */
24
    protected $template = self::DEFAULT_TEMPLATE;
25
26
    /** @var IComponent */
27
    protected $text;
28
29
    /** @var IComponent */
30
    protected $icon;
31
32
    /**
33
     * ButtonWithIcon constructor.
34
     *
35
     * @param IComponent  $text
36
     * @param IComponent  $icon
37
     * @param string[]    $intents
38
     * @param string[][]  $attributes
39
     * @param string|null $tag
40
     */
41
    public function __construct(
42
        IComponent $text,
43
        IComponent $icon,
44
        array $intents = [],
45
        array $attributes = [],
46
        ?string $tag = null
47
    ) {
48
        parent::__construct(null, $intents, $attributes, $tag);
49
50
        $this->text = $text->addIntent(static::INTENT_BUTTON_TEXT);
51
        $this->icon = $icon->addIntent(static::INTENT_BUTTON_ICON);
52
    }
53
54
    /**
55
     * @param string $template
56
     *
57
     * @return $this
58
     */
59
    public function setTemplate(string $template): INode
60
    {
61
        $this->template = $template;
62
63
        return $this;
64
    }
65
66
    /**
67
     * @return INode[]
68
     */
69
    public function getExtendedNodes(): array
70
    {
71
        return array_merge([$this->text, $this->icon], $this->getNodes());
72
    }
73
74
    public function getText(): IComponent
75
    {
76
        return $this->text;
77
    }
78
79
    public function getIcon(): IComponent
80
    {
81
        return $this->icon;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function __toString(): string
88
    {
89
        $content = sprintf(
90
            $this->template,
91
            (string)$this->text,
92
            (string)$this->icon
93
        );
94
95
        $content = StringHelper::wrapInTag($content, $this->tag, $this->attributes);
96
97
        return $content;
98
    }
99
}
100