Table::getLayout()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
    protected string $type = 'table';
25
    protected array $allowedContentTypes = [
26
        TableRow::class,
27
    ];
28
    private string $layout = 'default';
29
    private bool $isNumberColumnEnabled = false;
30
    private ?string $localId;
31
32
    public function __construct(string $layout = 'default', bool $isNumberColumnEnabled = false, ?string $localId = null, ?BlockNode $parent = null)
33
    {
34
        if (!\in_array($layout, [
35
            self::LAYOUT_DEFAULT,
36
            self::LAYOUT_FULL_WIDTH,
37
            self::LAYOUT_WIDE,
38
        ], true)) {
39
            throw new InvalidArgumentException(sprintf('Invalid layout "%s"', $layout));
40
        }
41
42
        parent::__construct($parent);
43
        $this->layout = $layout;
44
        $this->isNumberColumnEnabled = $isNumberColumnEnabled;
45
        $this->localId = $localId;
46
    }
47
48
    public static function load(array $data, ?BlockNode $parent = null): self
49
    {
50
        self::checkNodeData(static::class, $data, ['attrs']);
51
        self::checkRequiredKeys(['layout', 'isNumberColumnEnabled'], $data['attrs']);
52
53
        $node = new self($data['attrs']['layout'], (bool) $data['attrs']['isNumberColumnEnabled'], $data['attrs']['localId'] ?? null, $parent);
54
55
        // set content if defined
56
        if (\array_key_exists('content', $data)) {
57
            foreach ($data['content'] as $nodeData) {
58
                $class = Node::NODE_MAPPING[$nodeData['type']];
59
                $child = $class::load($nodeData, $node);
60
61
                $node->append($child);
62
            }
63
        }
64
65
        return $node;
66
    }
67
68
    public function getLayout(): string
69
    {
70
        return $this->layout;
71
    }
72
73
    public function isNumberColumnEnabled(): bool
74
    {
75
        return $this->isNumberColumnEnabled;
76
    }
77
78
    public function getLocalId(): ?string
79
    {
80
        return $this->localId;
81
    }
82
83
    protected function attrs(): array
84
    {
85
        $attrs = parent::attrs();
86
        $attrs['layout'] = $this->layout;
87
        $attrs['isNumberColumnEnabled'] = $this->isNumberColumnEnabled;
88
89
        if (null !== $this->localId) {
90
            $attrs['localId'] = $this->localId;
91
        }
92
93
        return $attrs;
94
    }
95
}
96