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

Text::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 6
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Adf\Node\Inline;
6
7
use DH\Adf\Node\InlineNode;
8
use DH\Adf\Node\MarkNode;
9
use DH\Adf\Node\Node;
10
11
/**
12
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/text
13
 */
14
class Text extends InlineNode
15
{
16
    protected string $type = 'text';
17
    private string $text;
18
19
    public function __construct(string $text, MarkNode ...$marks)
20
    {
21
        $this->text = $text;
22
23
        foreach ($marks as $mark) {
24
            $this->addMark($mark);
25
        }
26
    }
27
28
    public function jsonSerialize(): array
29
    {
30
        $result = parent::jsonSerialize();
31
        $result['text'] = $this->text;
32
33
        return $result;
34
    }
35
36
    public static function load(array $data): self
37
    {
38
        self::checkNodeData(static::class, $data, ['text']);
39
40
        $args = [];
41
        if (\array_key_exists('marks', $data)) {
42
            foreach ($data['marks'] as $nodeData) {
43
                $class = Node::NODE_MAPPING[$nodeData['type']];
44
                $node = $class::load($nodeData);
45
                \assert($node instanceof MarkNode);
46
                $args[] = $node;
47
            }
48
        }
49
50
        return new self($data['text'], ...$args);
51
    }
52
}
53