Passed
Push — master ( 61d1a6...f87603 )
by Alexander
04:12 queued 02:11
created

Widget   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 96.3%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 25
c 2
b 0
f 0
dl 0
loc 92
ccs 26
cts 27
cp 0.963
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A id() 0 5 1
A autoIdPrefix() 0 5 1
A getId() 0 7 3
A counter() 0 3 1
A addOptions() 0 23 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bulma;
6
7
use Yiisoft\Html\Html;
8
use Yiisoft\Widget\Widget as BaseWidget;
9
10
use function implode;
11
use function is_array;
12
use function strpos;
13
14
abstract class Widget extends BaseWidget
15
{
16
    private ?string $id = null;
17
    private bool $autoGenerate = true;
18
    private string $autoIdPrefix = 'w';
19
    private static int $counter = 0;
20
21
    /**
22
     * Returns a new instance with the specified ID of the widget.
23
     *
24
     * @param string $value The the ID of the widget.
25
     *
26
     * @return static
27
     */
28 8
    public function id(string $value): self
29
    {
30 8
        $new = clone $this;
31 8
        $new->id = $value;
32 8
        return $new;
33
    }
34
35
    /**
36
     * Counter used to generate {@see id()} for widgets.
37
     *
38
     * @param int $value
39
     */
40 89
    public static function counter(int $value): void
41
    {
42 89
        self::$counter = $value;
43 89
    }
44
45
    /**
46
     * Returns a new instance with the specified prefix to the automatically generated widget IDs.
47
     *
48
     * @param string $value The prefix to the automatically generated widget IDs.
49
     *
50
     * @return static
51
     *
52
     * {@see getId()}
53
     */
54 8
    public function autoIdPrefix(string $value): self
55
    {
56 8
        $new = clone $this;
57 8
        $new->autoIdPrefix = $value;
58 8
        return $new;
59
    }
60
61
    /**
62
     * Returns the ID of the widget.
63
     *
64
     * @return string|null ID of the widget.
65
     */
66 73
    protected function getId(): ?string
67
    {
68 73
        if ($this->autoGenerate && $this->id === null) {
69 72
            $this->id = $this->autoIdPrefix . ++self::$counter;
70
        }
71
72 73
        return $this->id;
73
    }
74
75
    /**
76
     * Validates CSS class default options.
77
     *
78
     * @param array $options
79
     * @param string $defaultClass
80
     *
81
     * @return array
82
     */
83 64
    protected function addOptions(array $options, string $defaultClass): array
84
    {
85 64
        $class = '';
86
87 64
        if (isset($options['class'])) {
88
            /** @var string|string[] $class */
89 15
            $class = $options['class'];
90 15
            unset($options['class']);
91
92 15
            if (is_array($class)) {
93
                $class = implode(' ', $class);
94
            }
95
        }
96
97 64
        if (strpos($class, $defaultClass) === false) {
98 64
            Html::addCssClass($options, $defaultClass);
99
        }
100
101 64
        if (!empty($class)) {
102 15
            Html::addCssClass($options, $class);
103
        }
104
105 64
        return $options;
106
    }
107
}
108