TableFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 7
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Grid\Factory;
6
7
use AbterPhp\Admin\Grid\Factory\Table\BodyFactory;
8
use AbterPhp\Admin\Grid\Factory\Table\HeaderFactory;
9
use AbterPhp\Framework\Grid\Component\Actions;
10
use AbterPhp\Framework\Grid\Table\Table;
11
use AbterPhp\Framework\Html\Attribute;
12
use AbterPhp\Framework\Html\Helper\Attributes;
13
14
class TableFactory
15
{
16
    protected const ATTRIBUTE_CLASS = 'class';
17
18
    protected const ERROR_MSG_BODY_CREATED      = 'Grid table body is already created.';
19
    protected const ERROR_MSG_HEADER_CREATED    = 'Grid table header is already created.';
20
    protected const ERROR_MSG_TABLE_CREATED     = 'Grid table is already created.';
21
    protected const ERROR_MSG_NO_BODY_CREATED   = 'Grid table body is not yet created';
22
    protected const ERROR_MSG_NO_HEADER_CREATED = 'Grig table header is not yet created';
23
24
    protected HeaderFactory $headerFactory;
25
26
    protected BodyFactory $bodyFactory;
27
28
    /** @var array<string,Attribute> */
29
    protected array $tableAttributes = [];
30
31
    /** @var array<string,Attribute> */
32
    protected array $headerAttributes = [];
33
34
    /**
35
     * TableFactory constructor.
36
     *
37
     * @param HeaderFactory $headerFactory
38
     * @param BodyFactory   $bodyFactory
39
     */
40
    public function __construct(HeaderFactory $headerFactory, BodyFactory $bodyFactory)
41
    {
42
        $this->headerFactory = $headerFactory;
43
        $this->bodyFactory   = $bodyFactory;
44
45
        $tableAttributes       = [self::ATTRIBUTE_CLASS => 'table table-striped table-hover table-bordered'];
46
        $this->tableAttributes = Attributes::fromArray($tableAttributes);
47
    }
48
49
    /**
50
     * @param callable[]   $getters
51
     * @param Actions|null $rowActions
52
     * @param array        $params
53
     * @param string       $baseUrl
54
     *
55
     * @return Table
56
     */
57
    public function create(
58
        array $getters,
59
        ?Actions $rowActions,
60
        array $params,
61
        string $baseUrl
62
    ): Table {
63
        $hasActions = $rowActions && count($rowActions) > 0;
64
65
        $header = $this->headerFactory->create($hasActions, $params, $baseUrl);
66
        $body   = $this->bodyFactory->create($getters, $rowActions);
67
68
        return new Table($body, $header, [], $this->tableAttributes);
69
    }
70
}
71