Options   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 1
b 0
f 0
dl 0
loc 47
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setEmptyItem() 0 4 1
A getFilterAction() 0 3 1
A setOptions() 0 4 1
A add() 0 4 1
A __construct() 0 9 2
1
<?php
2
3
namespace kalanis\kw_table\form_nette\Fields;
4
5
6
use kalanis\kw_connect\core\Interfaces\IFilterFactory;
7
use kalanis\kw_connect\core\Interfaces\IFilterType;
8
9
10
/**
11
 * Class Options
12
 * @package kalanis\kw_table\form_nette\Fields
13
 */
14
class Options extends AField
15
{
16
    protected string $emptyItem = '';
17
    /** @var string[] */
18
    protected array $options = [];
19
    protected ?int $size = null;
20
21
    public function __construct(array $options = [], array $attributes = [], string $emptyItem = '- all -')
22
    {
23
        $this->setEmptyItem($emptyItem);
24
        $this->setOptions($options);
25
        if (isset($attributes[static::ATTR_SIZE])) {
26
            $this->size = $attributes[static::ATTR_SIZE];
27
            unset($attributes[static::ATTR_SIZE]);
28
        }
29
        parent::__construct($attributes);
30
    }
31
32
    public function getFilterAction(): string
33
    {
34
        return IFilterFactory::ACTION_EXACT;
35
    }
36
37
    /**
38
     * @param string[] $options
39
     * @return $this
40
     */
41
    public function setOptions(array $options)
42
    {
43
        $this->options = [IFilterType::EMPTY_FILTER => $this->emptyItem] + $options;
44
        return $this;
45
    }
46
47
    /**
48
     * @param string $text
49
     * @return $this
50
     */
51
    public function setEmptyItem($text)
52
    {
53
        $this->emptyItem = $text;
54
        return $this;
55
    }
56
57
    public function add(): void
58
    {
59
        $this->form->addSelect($this->alias, null, $this->options, $this->size);
60
        $this->processAttributes();
61
    }
62
}
63