Passed
Push — main ( 16e3d2...2668c7 )
by Damien
07:42
created

CodeBlock::getLanguage()   A

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\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
    public function getLanguage(): ?string
53
    {
54
        return $this->language;
55
    }
56
57
    protected function attrs(): array
58
    {
59
        $attrs = parent::attrs();
60
61
        if (null !== $this->language) {
62
            $attrs['language'] = $this->language;
63
        }
64
65
        return $attrs;
66
    }
67
}
68