Media::getWidth()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DH\Adf\Node\Child;
6
7
use DH\Adf\Node\BlockNode;
8
use DH\Adf\Node\Node;
9
use InvalidArgumentException;
10
11
/**
12
 * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/media
13
 */
14
class Media extends Node
15
{
16
    public const TYPE_FILE = 'file';
17
    public const TYPE_LINK = 'link';
18
19
    protected string $type = 'media';
20
    private string $id;
21
    private string $mediaType;
22
    private string $collection;
23
    private ?string $occurrenceKey;
24
    private ?string $alt;
25
    private ?float $width;
26
    private ?float $height;
27
28
    public function __construct(string $id, string $mediaType, string $collection, ?float $width = null, ?float $height = null, ?string $occurrenceKey = null, ?BlockNode $parent = null, ?string $alt = null)
29
    {
30
        if (!\in_array($mediaType, [self::TYPE_FILE, self::TYPE_LINK], true)) {
31
            throw new InvalidArgumentException('Invalid media type');
32
        }
33
34
        parent::__construct($parent);
35
        $this->id = $id;
36
        $this->mediaType = $mediaType;
37
        $this->collection = $collection;
38
        $this->occurrenceKey = $occurrenceKey;
39
40
        if (\is_float($width)) {
41
            $width = round($width, 2);
42
        }
43
        $this->width = $width;
44
45
        if (\is_float($height)) {
46
            $height = round($height, 2);
47
        }
48
        $this->height = $height;
49
        $this->alt = $alt;
50
    }
51
52
    public static function load(array $data, ?BlockNode $parent = null): self
53
    {
54
        self::checkNodeData(static::class, $data, ['attrs']);
55
        self::checkRequiredKeys(['id', 'type', 'collection'], $data['attrs']);
56
57
        return new self(
58
            $data['attrs']['id'],
59
            $data['attrs']['type'],
60
            $data['attrs']['collection'],
61
            $data['attrs']['width'] ?? null,
62
            $data['attrs']['height'] ?? null,
63
            $data['attrs']['occurrenceKey'] ?? null,
64
            $parent,
65
            $data['attrs']['alt'] ?? null
66
        );
67
    }
68
69
    public function getId(): string
70
    {
71
        return $this->id;
72
    }
73
74
    public function getMediaType(): string
75
    {
76
        return $this->mediaType;
77
    }
78
79
    public function getCollection(): string
80
    {
81
        return $this->collection;
82
    }
83
84
    public function getOccurrenceKey(): ?string
85
    {
86
        return $this->occurrenceKey;
87
    }
88
89
    public function getWidth(): ?float
90
    {
91
        return $this->width;
92
    }
93
94
    public function getHeight(): ?float
95
    {
96
        return $this->height;
97
    }
98
99
    public function getAlt(): ?string
100
    {
101
        return $this->alt;
102
    }
103
104
    protected function attrs(): array
105
    {
106
        $attrs = parent::attrs();
107
108
        $attrs['id'] = $this->id;
109
        $attrs['type'] = $this->mediaType;
110
        $attrs['collection'] = $this->collection;
111
112
        if (null !== $this->occurrenceKey) {
113
            $attrs['occurrenceKey'] = $this->occurrenceKey;
114
        }
115
116
        if (null !== $this->width) {
117
            $attrs['width'] = $this->width;
118
        }
119
120
        if (null !== $this->height) {
121
            $attrs['height'] = $this->height;
122
        }
123
124
        if (null !== $this->alt) {
125
            $attrs['alt'] = $this->alt;
126
        }
127
128
        return $attrs;
129
    }
130
}
131