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

Panel::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 14
c 0
b 0
f 0
rs 9.9332
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Adf\Node\Block;
6
7
use DH\Adf\Builder\ParagraphBuilder;
8
use DH\Adf\Node\BlockNode;
9
use DH\Adf\Node\Node;
10
use InvalidArgumentException;
11
12
/**
13
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/panel
14
 */
15
class Panel extends BlockNode
16
{
17
    use ParagraphBuilder;
18
19
    public const INFO = 'info';
20
    public const NOTE = 'note';
21
    public const WARNING = 'warning';
22
    public const SUCCESS = 'success';
23
    public const ERROR = 'error';
24
25
    protected string $type = 'panel';
26
    protected array $allowedContentTypes = [
27
        BulletList::class,
28
        Heading::class,
29
        OrderedList::class,
30
        Paragraph::class,
31
    ];
32
    private string $panelType;
33
34
    public function __construct(string $type = self::INFO, ?BlockNode $parent = null)
35
    {
36
        if (!\in_array($type, [
37
            self::INFO,
38
            self::NOTE,
39
            self::WARNING,
40
            self::SUCCESS,
41
            self::ERROR,
42
        ], true)) {
43
            throw new InvalidArgumentException('Invalid panel type');
44
        }
45
46
        parent::__construct($parent);
47
        $this->panelType = $type;
48
    }
49
50
    public static function load(array $data, ?BlockNode $parent = null): self
51
    {
52
        self::checkNodeData(static::class, $data, ['attrs']);
53
        self::checkRequiredKeys(['panelType'], $data['attrs']);
54
55
        $node = new self($data['attrs']['panelType'], $parent);
56
57
        // set content if defined
58
        if (\array_key_exists('content', $data)) {
59
            foreach ($data['content'] as $nodeData) {
60
                $class = Node::NODE_MAPPING[$nodeData['type']];
61
                $child = $class::load($nodeData, $node);
62
63
                $node->append($child);
64
            }
65
        }
66
67
        return $node;
68
    }
69
70
    protected function attrs(): array
71
    {
72
        $attrs = parent::attrs();
73
        $attrs['panelType'] = $this->panelType;
74
75
        return $attrs;
76
    }
77
}
78