Passed
Push — master ( d5c6a7...b7d446 )
by Alexander
02:56
created

Widget   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

5 Methods

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