Optgroup::baseHtml()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Enjoys\Forms\Elements;
6
7
use Enjoys\Forms\AttributeFactory;
8
use Enjoys\Forms\Element;
9
use Enjoys\Forms\Interfaces\Fillable;
10
use Enjoys\Forms\Traits\Fill;
11
12
/**
13
 * Class Optgroup
14
 * @package Enjoys\Forms\Elements
15
 *
16
 * variant 1
17
 * $form->select('foo', 'bar')
18
 *      ->setOptgroup(
19
 *          'group1',
20
 *          [1, 2, 3],
21
 *          ['class' => 'text-danger']
22
 *      )->setOptgroup(
23
 *          'group2',
24
 *          [4, 5, 6]
25
 *      );
26
 *
27
 * variant 2
28
 * $select = $form->select('foo', 'bar');
29
 * $dataGroup = [
30
 *      'Города' => [
31
 *          'vrn' => 'Воронеж',
32
 *          'msk' => 'Москва',
33
 *      ],
34
 *      'Страны' => [
35
 *          'ru' => 'Russia',
36
 *          'de' => [
37
 *              'Germany', [
38
 *                  'disabled'
39
 *                  ]
40
 *              ],
41
 *          'usa' => [
42
 * 'USA', [
43
 *                  'class' => 'h1 text-danger'
44
 *                  ]
45
 *          ],
46
 *      ]
47
 * ];
48
 *
49
 * foreach ($dataGroup as $optgroup => $filldata) {
50
 *      $select->setOptgroup($optgroup, $filldata);
51
 * }
52
 * //можно также продолжить заполнять select option'ами без optgroup
53
 * $select->fill([
54
 *      1, 2, 3
55
 * ]);
56
 *
57
 * @since 2.4.0
58
 */
59
class Optgroup extends Element implements Fillable
60
{
61
    use Fill;
62
63
    protected string $type = 'option';
64
65
66 8
    public function __construct(string $title, string $parentName, mixed $defaults = '')
67
    {
68 8
        parent::__construct(\uniqid('optgroup'), $title);
69 8
        $this->setAttributes(
70 8
            AttributeFactory::createFromArray([
71 8
                'label' => $title
72 8
            ])
73 8
        );
74 8
        $this->setName($parentName);
75 8
        $this->getAttributeCollection()
76 8
            ->remove('name')
77 8
            ->remove('id');
78 8
        $this->setDefault($defaults);
79
    }
80
81
82 8
    protected function setDefault(mixed $value = null): static
83
    {
84 8
        $this->setDefaultValue($value);
85 8
        return $this;
86
    }
87
88 1
    public function baseHtml(): string
89
    {
90 1
        return '';
91
    }
92
}
93