Panel::__construct()   A
last analyzed

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\BulletListBuilder;
8
use DH\Adf\Builder\HeadingBuilder;
9
use DH\Adf\Builder\OrderedListBuilder;
10
use DH\Adf\Builder\ParagraphBuilder;
11
use DH\Adf\Node\BlockNode;
12
use DH\Adf\Node\Node;
13
use InvalidArgumentException;
14
15
/**
16
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/panel
17
 */
18
class Panel extends BlockNode
19
{
20
    use BulletListBuilder;
21
    use HeadingBuilder;
22
    use OrderedListBuilder;
23
    use ParagraphBuilder;
24
25
    public const INFO = 'info';
26
    public const NOTE = 'note';
27
    public const WARNING = 'warning';
28
    public const SUCCESS = 'success';
29
    public const ERROR = 'error';
30
31
    protected string $type = 'panel';
32
    protected array $allowedContentTypes = [
33
        BulletList::class,
34
        Heading::class,
35
        OrderedList::class,
36
        Paragraph::class,
37
    ];
38
    private string $panelType;
39
40
    public function __construct(string $type = self::INFO, ?BlockNode $parent = null)
41
    {
42
        if (!\in_array($type, [
43
            self::INFO,
44
            self::NOTE,
45
            self::WARNING,
46
            self::SUCCESS,
47
            self::ERROR,
48
        ], true)) {
49
            throw new InvalidArgumentException('Invalid panel type');
50
        }
51
52
        parent::__construct($parent);
53
        $this->panelType = $type;
54
    }
55
56
    public static function load(array $data, ?BlockNode $parent = null): self
57
    {
58
        self::checkNodeData(static::class, $data, ['attrs']);
59
        self::checkRequiredKeys(['panelType'], $data['attrs']);
60
61
        $node = new self($data['attrs']['panelType'], $parent);
62
63
        // set content if defined
64
        if (\array_key_exists('content', $data)) {
65
            foreach ($data['content'] as $nodeData) {
66
                $class = Node::NODE_MAPPING[$nodeData['type']];
67
                $child = $class::load($nodeData, $node);
68
69
                $node->append($child);
70
            }
71
        }
72
73
        return $node;
74
    }
75
76
    public function getPanelType(): string
77
    {
78
        return $this->panelType;
79
    }
80
81
    protected function attrs(): array
82
    {
83
        $attrs = parent::attrs();
84
        $attrs['panelType'] = $this->panelType;
85
86
        return $attrs;
87
    }
88
}
89