Heading   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A load() 0 18 3
A getLevel() 0 3 1
A attrs() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Adf\Node\Block;
6
7
use DH\Adf\Builder\HeadingBuilder;
8
use DH\Adf\Builder\TextBuilder;
9
use DH\Adf\Node\BlockNode;
10
use DH\Adf\Node\InlineNode;
11
use DH\Adf\Node\Node;
12
use JsonSerializable;
13
14
/**
15
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/heading
16
 */
17
class Heading extends BlockNode implements JsonSerializable
18
{
19
    use TextBuilder;
20
    use HeadingBuilder;
21
22
    protected string $type = 'heading';
23
    protected array $allowedContentTypes = [
24
        InlineNode::class,
25
    ];
26
    private int $level;
27
28
    public function __construct(int $level, ?BlockNode $parent = null)
29
    {
30
        parent::__construct($parent);
31
        $this->level = $level;
32
    }
33
34
    public static function load(array $data, ?BlockNode $parent = null): self
35
    {
36
        self::checkNodeData(static::class, $data, ['attrs']);
37
        self::checkRequiredKeys(['level'], $data['attrs']);
38
39
        $node = new self($data['attrs']['level'], $parent);
40
41
        // set content if defined
42
        if (\array_key_exists('content', $data)) {
43
            foreach ($data['content'] as $nodeData) {
44
                $class = Node::NODE_MAPPING[$nodeData['type']];
45
                $child = $class::load($nodeData, $node);
46
47
                $node->append($child);
48
            }
49
        }
50
51
        return $node;
52
    }
53
54
    public function getLevel(): int
55
    {
56
        return $this->level;
57
    }
58
59
    protected function attrs(): array
60
    {
61
        $attrs = parent::attrs();
62
        $attrs['level'] = $this->level;
63
64
        return $attrs;
65
    }
66
}
67