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

CodeBlock   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 21
dl 0
loc 45
c 0
b 0
f 0
rs 10

3 Methods

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