Passed
Push — master ( 73cd93...7a270d )
by Alexander
30:15 queued 27:55
created

Widget::addOptions()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
c 1
b 0
f 0
nc 8
nop 2
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 4
rs 9.9666
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
abstract class Widget extends BaseWidget
11
{
12
    private ?string $id = null;
13
    private bool $autoGenerate = true;
14
    private string $autoIdPrefix = 'w';
15
    private static int $counter = 0;
16
17
    /**
18
     * Set the Id of the widget.
19
     */
20 1
    public function id(string $value): self
21
    {
22 1
        $this->id = $value;
23 1
        return $this;
24
    }
25
26
    /**
27
     * Counter used to generate {@see id} for widgets.
28
     */
29 41
    public static function counter(int $value): void
30
    {
31 41
        self::$counter = $value;
32 41
    }
33
34
    /**
35
     * The prefix to the automatically generated widget IDs.
36
     *
37
     * {@see getId()}
38
     */
39 1
    public function autoIdPrefix(string $value): self
40
    {
41 1
        $this->autoIdPrefix = $value;
42 1
        return $this;
43
    }
44
45
    /**
46
     * Returns the Id of the widget.
47
     *
48
     * @return string|null Id of the widget.
49
     */
50 31
    protected function getId(): ?string
51
    {
52 31
        if ($this->autoGenerate && $this->id === null) {
53 30
            $this->id = $this->autoIdPrefix . ++self::$counter;
54
        }
55
56 31
        return $this->id;
57
    }
58
59
    /**
60
     * Validate CSS class default options.
61
     *
62
     * @param array $options
63
     * @param string $valueDefault
64
     *
65
     * @return array
66
     */
67 32
    protected function addOptions(array $options, string $valueDefault): array
68
    {
69 32
        $optionsTmp = '';
70
71 32
        if (isset($options['class'])) {
72 10
            $optionsTmp = $options['class'];
73
74 10
            unset($options['class']);
75
        }
76
77 32
        if (strpos($optionsTmp, $valueDefault) === false) {
78 32
            Html::addCssClass($options, $valueDefault);
79
        }
80
81 32
        if (!empty($optionsTmp)) {
82 10
            Html::addCssClass($options, $optionsTmp);
83
        }
84
85 32
        return $options;
86
    }
87
}
88