Table::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 15
c 1
b 1
f 0
nc 3
nop 5
dl 0
loc 21
rs 9.7666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Adf\Node\Block;
6
7
use DH\Adf\Builder\TableRowBuilder;
8
use DH\Adf\Node\BlockNode;
9
use DH\Adf\Node\Child\TableRow;
10
use DH\Adf\Node\Node;
11
use InvalidArgumentException;
12
13
/**
14
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/table
15
 */
16
class Table extends BlockNode
17
{
18
    use TableRowBuilder;
19
20
    public const LAYOUT_DEFAULT = 'default';
21
    public const LAYOUT_FULL_WIDTH = 'full-width';
22
    public const LAYOUT_WIDE = 'wide';
23
24
    public const LAYOUT_START = 'align-start';
25
    public const LAYOUT_CENTER = 'center';
26
27
    protected string $type = 'table';
28
    protected array $allowedContentTypes = [
29
        TableRow::class,
30
    ];
31
    private string $layout = 'default';
32
    private bool $isNumberColumnEnabled = false;
33
    private ?string $localId;
34
    private ?int $width = null;
35
36
    public function __construct(string $layout = self::LAYOUT_DEFAULT, bool $isNumberColumnEnabled = false, ?int $width = null, ?string $localId = null, ?BlockNode $parent = null)
37
    {
38
        if (!\in_array($layout, [
39
            self::LAYOUT_DEFAULT,
40
            self::LAYOUT_FULL_WIDTH,
41
            self::LAYOUT_WIDE,
42
            self::LAYOUT_START,
43
            self::LAYOUT_CENTER,
44
        ], true)) {
45
            throw new InvalidArgumentException(\sprintf('Invalid layout "%s"', $layout));
46
        }
47
48
        parent::__construct($parent);
49
        $this->layout = $layout;
50
        $this->isNumberColumnEnabled = $isNumberColumnEnabled;
51
        $this->localId = $localId;
52
53
        if (null !== $width) {
54
            $width = abs($width);
55
        }
56
        $this->width = $width;
57
    }
58
59
    public static function load(array $data, ?BlockNode $parent = null): self
60
    {
61
        self::checkNodeData(static::class, $data, ['attrs']);
62
        self::checkRequiredKeys(['layout', 'isNumberColumnEnabled'], $data['attrs']);
63
64
        $node = new self($data['attrs']['layout'], (bool) $data['attrs']['isNumberColumnEnabled'], $data['attrs']['width'] ?? null, $data['attrs']['localId'] ?? null, $parent);
65
66
        // set content if defined
67
        if (\array_key_exists('content', $data)) {
68
            foreach ($data['content'] as $nodeData) {
69
                $class = Node::NODE_MAPPING[$nodeData['type']];
70
                $child = $class::load($nodeData, $node);
71
72
                $node->append($child);
73
            }
74
        }
75
76
        return $node;
77
    }
78
79
    public function getLayout(): string
80
    {
81
        return $this->layout;
82
    }
83
84
    public function isNumberColumnEnabled(): bool
85
    {
86
        return $this->isNumberColumnEnabled;
87
    }
88
89
    public function getLocalId(): ?string
90
    {
91
        return $this->localId;
92
    }
93
94
    protected function attrs(): array
95
    {
96
        $attrs = parent::attrs();
97
        $attrs['layout'] = $this->layout;
98
        $attrs['isNumberColumnEnabled'] = $this->isNumberColumnEnabled;
99
100
        if (null !== $this->localId) {
101
            $attrs['localId'] = $this->localId;
102
        }
103
104
        if (null !== $this->width) {
105
            $attrs['width'] = $this->width;
106
        }
107
108
        return $attrs;
109
    }
110
}
111