Passed
Push — master ( f85738...3d85c1 )
by Peter
04:52
created

HeaderFactory::createCells()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.6111
c 0
b 0
f 0
cc 5
nc 6
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Grid\Factory\Table;
6
7
use AbterPhp\Framework\Constant\Html5;
8
use AbterPhp\Framework\Grid\Cell\Cell;
9
use AbterPhp\Framework\Grid\Cell\Sortable;
10
use AbterPhp\Framework\Grid\Collection\Cells;
11
use AbterPhp\Framework\Grid\Component\Header;
12
use AbterPhp\Framework\Grid\Row\Row;
13
14
class HeaderFactory
15
{
16
    const ACTIONS_CONTENT = 'framework:actions';
17
    const ACTIONS_GROUP   = 'actions';
18
19
    /** @var array */
20
    protected $headers = [];
21
22
    /** @var array */
23
    protected $inputNames = [];
24
25
    /** @var array */
26
    protected $fieldNames = [];
27
28
    /**
29
     * @param bool   $hasActions
30
     * @param array  $params
31
     * @param string $baseUrl
32
     *
33
     * @return Header
34
     */
35
    public function create(bool $hasActions, array $params, string $baseUrl): Header
36
    {
37
        $cells = $this->createCells($hasActions);
38
39
        $header   = new Header();
40
        $header[] = new Row($cells);
41
42
        $header->setParams($params)->setBaseUrl($baseUrl);
43
44
        return $header;
45
    }
46
47
    /**
48
     * @param bool $hasActions
49
     *
50
     * @return Cells
51
     */
52
    public function createCells(bool $hasActions): Cells
53
    {
54
        $cells = new Cells();
55
        foreach ($this->headers as $group => $content) {
56
            if (!array_key_exists($group, $this->inputNames) || !array_key_exists($group, $this->fieldNames)) {
57
                $cells[] = new Cell($content, $group, [], [], Html5::TAG_TH);
58
            } else {
59
                $cells[] = new Sortable($content, $group, $this->inputNames[$group], $this->fieldNames[$group], [], []);
60
            }
61
        }
62
63
        if ($hasActions) {
64
            $cells[] = new Cell(static::ACTIONS_CONTENT, static::ACTIONS_GROUP, [], [], Html5::TAG_TH);
65
        }
66
67
        return $cells;
68
    }
69
}
70