Passed
Push — main ( e16d46...16e3d2 )
by Damien
07:21
created

Table::attrs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
c 0
b 0
f 0
rs 10
cc 2
nc 2
nop 0
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
    protected function attrs(): array
69
    {
70
        $attrs = parent::attrs();
71
        $attrs['layout'] = $this->layout;
72
        $attrs['isNumberColumnEnabled'] = $this->isNumberColumnEnabled;
73
74
        if (null !== $this->localId) {
75
            $attrs['localId'] = $this->localId;
76
        }
77
78
        return $attrs;
79
    }
80
}
81