MediaSingle::__construct()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 3
dl 0
loc 21
rs 9.7666
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Adf\Node\Block;
6
7
use DH\Adf\Builder\MediaBuilder;
8
use DH\Adf\Node\BlockNode;
9
use DH\Adf\Node\Child\Media;
10
use DH\Adf\Node\Node;
11
use InvalidArgumentException;
12
use JsonSerializable;
13
14
/**
15
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/mediaSingle
16
 */
17
class MediaSingle extends BlockNode implements JsonSerializable
18
{
19
    use MediaBuilder;
20
21
    public const LAYOUT_WRAP_LEFT = 'wrap-left';
22
    public const LAYOUT_CENTER = 'center';
23
    public const LAYOUT_WRAP_RIGHT = 'wrap-right';
24
    public const LAYOUT_WIDE = 'wide';
25
    public const LAYOUT_FULL_WIDTH = 'full-width';
26
    public const LAYOUT_ALIGN_START = 'align-start';
27
    public const LAYOUT_ALIGN_END = 'align-end';
28
29
    protected string $type = 'mediaSingle';
30
    protected array $allowedContentTypes = [
31
        Media::class,
32
    ];
33
    private string $layout;
34
    private ?float $width;
35
36
    public function __construct(string $layout, ?float $width = null, ?BlockNode $parent = null)
37
    {
38
        if (!\in_array($layout, [
39
            self::LAYOUT_WRAP_LEFT,
40
            self::LAYOUT_CENTER,
41
            self::LAYOUT_WRAP_RIGHT,
42
            self::LAYOUT_WIDE,
43
            self::LAYOUT_FULL_WIDTH,
44
            self::LAYOUT_ALIGN_START,
45
            self::LAYOUT_ALIGN_END,
46
        ], true)) {
47
            throw new InvalidArgumentException(\sprintf('Invalid layout "%s"', $layout));
48
        }
49
50
        parent::__construct($parent);
51
        $this->layout = $layout;
52
53
        if (\is_float($width)) {
54
            $width = round($width, 2);
55
        }
56
        $this->width = $width;
57
    }
58
59
    public static function load(array $data, ?BlockNode $parent = null): self
60
    {
61
        self::checkNodeData(static::class, $data, ['attrs']);
62
        self::checkRequiredKeys(['layout'], $data['attrs']);
63
64
        $node = new self(
65
            $data['attrs']['layout'],
66
            isset($data['attrs']['width']) ? (float) $data['attrs']['width'] : null,
67
            $parent
68
        );
69
70
        // set content if defined
71
        if (\array_key_exists('content', $data)) {
72
            foreach ($data['content'] as $nodeData) {
73
                $class = Node::NODE_MAPPING[$nodeData['type']];
74
                $child = $class::load($nodeData, $node);
75
76
                $node->append($child);
77
            }
78
        }
79
80
        return $node;
81
    }
82
83
    public function getLayout(): string
84
    {
85
        return $this->layout;
86
    }
87
88
    public function getWidth(): ?float
89
    {
90
        return $this->width;
91
    }
92
93
    protected function attrs(): array
94
    {
95
        $attrs = parent::attrs();
96
97
        $attrs['layout'] = $this->layout;
98
99
        if (null !== $this->width) {
100
            $attrs['width'] = $this->width;
101
        }
102
103
        return $attrs;
104
    }
105
}
106