InlineCard::load()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Adf\Node\Inline;
6
7
use DH\Adf\Node\BlockNode;
8
use DH\Adf\Node\InlineNode;
9
use InvalidArgumentException;
10
11
/**
12
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/inlineCard
13
 */
14
class InlineCard extends InlineNode
15
{
16
    protected string $type = 'inlineCard';
17
    private ?string $data;
18
    private ?string $url;
19
20
    public function __construct(?string $url = null, ?string $data = null)
21
    {
22
        if (null === $data && null === $url) {
23
            throw new InvalidArgumentException('Either data or url must be set.');
24
        }
25
26
        // TODO: ensure data, if provided, is valid jsonld
27
        $this->data = $data;
28
        $this->url = $url;
29
    }
30
31
    public static function load(array $data, ?BlockNode $parent = null): self
32
    {
33
        self::checkNodeData(static::class, $data);
34
35
        return new self($data['attrs']['url'] ?? null, $data['attrs']['data'] ?? null);
36
    }
37
38
    public function getData(): ?string
39
    {
40
        return $this->data;
41
    }
42
43
    public function getUrl(): ?string
44
    {
45
        return $this->url;
46
    }
47
48
    protected function attrs(): array
49
    {
50
        $attrs = parent::attrs();
51
52
        if (null !== $this->data) {
53
            $attrs['data'] = $this->data;
54
        }
55
        if (null !== $this->url) {
56
            $attrs['url'] = $this->url;
57
        }
58
59
        return $attrs;
60
    }
61
}
62