AField::processAttributes()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 0
1
<?php
2
3
namespace kalanis\kw_table\form_nette\Fields;
4
5
6
use kalanis\kw_connect\core\ConnectException;
7
use kalanis\kw_connect\core\Interfaces\IFilterType;
8
use kalanis\kw_connect\core\Interfaces\IIterableConnector;
9
use kalanis\kw_table\core\Interfaces\Form\IField;
10
use kalanis\kw_table\core\TableException;
11
use kalanis\kw_table\form_nette\NetteForm;
12
use Nette\Application\UI\Form as BaseForm;
0 ignored issues
show
Bug introduced by
The type Nette\Application\UI\Form was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Nette\Forms\Controls\BaseControl;
14
15
16
/**
17
 * Class AField
18
 * @package kalanis\kw_table\form_nette\Fields
19
 */
20
abstract class AField implements IField
21
{
22
    protected const ATTR_SIZE = 'size';
23
24
    /** @var BaseForm|NetteForm<string, BaseControl> */
25
    protected $form;
26
    protected string $alias = '';
27
    /** @var array */
28
    protected array $attributes = [];
29
    protected ?IIterableConnector $dataSource;
30
31
    public function __construct(array $attributes = [])
32
    {
33
        $this->setAttributes($attributes);
34
    }
35
36
    /**
37
     * @param BaseForm $form
38
     * @return $this
39
     */
40
    public function setForm(BaseForm $form)
41
    {
42
        $this->form = $form;
43
        return $this;
44
    }
45
46
    public function setAlias(string $alias): void
47
    {
48
        $this->alias = $alias;
49
    }
50
51
    public function setDataSourceConnector(IIterableConnector $dataSource): void
52
    {
53
        $this->dataSource = $dataSource;
54
    }
55
56
    /**
57
     * @param string $name
58
     * @param string $value
59
     * @return $this
60
     */
61
    public function addAttribute(string $name, string $value)
62
    {
63
        $this->attributes[$name] = $value;
64
        return $this;
65
    }
66
67
    public function setAttributes(array $attributes): void
68
    {
69
        $this->attributes = $attributes + $this->attributes;
70
    }
71
72
    public function prepareAlias()
73
    {
74
        $this->alias = str_replace('.', '_', $this->alias);
75
    }
76
77
    public function processAttributes()
78
    {
79
        foreach ($this->attributes as $name => $value) {
80
            $this->form[$this->alias]->setAttribute($name, $value);
81
        }
82
    }
83
84
    /**
85
     * @throws ConnectException
86
     * @throws TableException
87
     * @return IFilterType
88
     */
89
    public function getFilterType(): IFilterType
90
    {
91
        $ds = $this->dataSource;
92
        if (!$ds || (!$ds instanceof IIterableConnector)) {
93
            throw new TableException('Set the datasource first!');
94
        }
95
        return $ds->getFilterFactory()->getFilter($this->getFilterAction());
96
    }
97
98
    abstract public function getFilterAction(): string;
99
}
100