Countable   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
dl 0
loc 82
rs 10
c 1
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getExtendedNodes() 0 3 1
A __construct() 0 18 1
A setTemplate() 0 5 1
A __toString() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Framework\Form\Label;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Html\Attribute;
9
use AbterPhp\Framework\Html\Helper\Attributes;
10
use AbterPhp\Framework\Html\Helper\Tag as TagHelper;
11
use AbterPhp\Framework\Html\INode;
12
use AbterPhp\Framework\Html\ITemplater;
13
use AbterPhp\Framework\Html\Node;
14
use AbterPhp\Framework\Html\Tag;
15
16
class Countable extends Label implements ITemplater
17
{
18
    public const DEFAULT_SIZE = 160;
19
20
    /**
21
     * %1$s - nodes
22
     * %2$s - counter
23
     */
24
    protected const DEFAULT_TEMPLATE = '%1$s %2$s';
25
26
    protected const ATTR_DATA_COUNT = Html5::ATTR_DATA_DASH . 'count';
27
28
    protected const CLASS_COUNT = 'count';
29
30
    protected string $template = self::DEFAULT_TEMPLATE;
31
32
    protected INode $counter;
33
34
    /**
35
     * Countable constructor.
36
     *
37
     * @param string                       $inputId
38
     * @param INode[]|INode|string|null    $content
39
     * @param int                          $size
40
     * @param string[]                     $intents
41
     * @param array<string,Attribute>|null $attributes
42
     * @param string|null                  $tag
43
     */
44
    public function __construct(
45
        string $inputId,
46
        $content = null,
47
        int $size = 160,
48
        array $intents = [],
49
        ?array $attributes = null,
50
        ?string $tag = null
51
    ) {
52
        parent::__construct($inputId, $content, $intents, $attributes, $tag);
53
54
        $counterAttributes = Attributes::fromArray(
55
            [
56
                static::ATTR_DATA_COUNT => [(string)$size],
57
                Html5::ATTR_CLASS       => [static::CLASS_COUNT],
58
            ]
59
        );
60
61
        $this->counter = new Tag(null, [], $counterAttributes, Html5::TAG_SPAN);
62
    }
63
64
    /**
65
     * @param string $template
66
     *
67
     * @return $this
68
     */
69
    public function setTemplate(string $template): INode
70
    {
71
        $this->template = $template;
72
73
        return $this;
74
    }
75
76
    /**
77
     * @return INode[]
78
     */
79
    public function getExtendedNodes(): array
80
    {
81
        return array_merge([$this->counter], $this->getNodes());
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function __toString(): string
88
    {
89
        $nodes = Node::__toString();
90
91
        $content = sprintf(
92
            $this->template,
93
            $nodes,
94
            (string)$this->counter
95
        );
96
97
        return TagHelper::toString($this->tag, $content, $this->attributes);
98
    }
99
}
100