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

Widget::addOptions()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 23
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 11
c 1
b 0
f 0
nc 12
nop 2
dl 0
loc 23
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 9.6111
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