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

OrderedList   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 20
dl 0
loc 44
c 0
b 0
f 0
rs 10

3 Methods

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