Skip to content

Commit 67734d8

Browse files
authored
Refactor media dimensions to use float type for width and height (#23)
1 parent da9f7ee commit 67734d8

5 files changed

Lines changed: 24 additions & 15 deletions

File tree

src/Builder/MediaBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait MediaBuilder
1010
{
1111
use BuilderInterface;
1212

13-
public function media(string $id, string $mediaType, string $collection, ?int $width = null, ?int $height = null, ?string $occurrenceKey = null): Media
13+
public function media(string $id, string $mediaType, string $collection, ?float $width = null, ?float $height = null, ?string $occurrenceKey = null): Media
1414
{
1515
$block = new Media($id, $mediaType, $collection, $width, $height, $occurrenceKey, $this);
1616
$this->append($block);

src/Builder/MediaSingleBuilder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ trait MediaSingleBuilder
1010
{
1111
use BuilderInterface;
1212

13-
public function mediaSingle(string $layout, ?int $width = null): MediaSingle
13+
public function mediaSingle(string $layout, ?float $width = null): MediaSingle
1414
{
1515
$block = new MediaSingle($layout, $width, $this);
1616
$this->append($block);

src/Node/Block/MediaSingle.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ class MediaSingle extends BlockNode implements JsonSerializable
3131
Media::class,
3232
];
3333
private string $layout;
34-
private ?int $width;
34+
private ?float $width;
3535

36-
public function __construct(string $layout, ?int $width = null, ?BlockNode $parent = null)
36+
public function __construct(string $layout, ?float $width = null, ?BlockNode $parent = null)
3737
{
3838
if (!\in_array($layout, [
3939
self::LAYOUT_WRAP_LEFT,
@@ -49,6 +49,9 @@ public function __construct(string $layout, ?int $width = null, ?BlockNode $pare
4949

5050
parent::__construct($parent);
5151
$this->layout = $layout;
52+
53+
if (is_float($width))
54+
$width = round($width, 2);
5255
$this->width = $width;
5356
}
5457

@@ -77,7 +80,7 @@ public function getLayout(): string
7780
return $this->layout;
7881
}
7982

80-
public function getWidth(): ?int
83+
public function getWidth(): ?float
8184
{
8285
return $this->width;
8386
}

src/Node/Child/Media.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class Media extends Node
2121
private string $mediaType;
2222
private string $collection;
2323
private ?string $occurrenceKey;
24-
private ?int $width;
25-
private ?int $height;
24+
private ?float $width;
25+
private ?float $height;
2626

27-
public function __construct(string $id, string $mediaType, string $collection, ?int $width = null, ?int $height = null, ?string $occurrenceKey = null, ?BlockNode $parent = null)
27+
public function __construct(string $id, string $mediaType, string $collection, ?float $width = null, ?float $height = null, ?string $occurrenceKey = null, ?BlockNode $parent = null)
2828
{
2929
if (!\in_array($mediaType, [self::TYPE_FILE, self::TYPE_LINK], true)) {
3030
throw new InvalidArgumentException('Invalid media type');
@@ -35,7 +35,13 @@ public function __construct(string $id, string $mediaType, string $collection, ?
3535
$this->mediaType = $mediaType;
3636
$this->collection = $collection;
3737
$this->occurrenceKey = $occurrenceKey;
38+
39+
if (is_float($width))
40+
$width = round($width, 2);
3841
$this->width = $width;
42+
43+
if (is_float($height))
44+
$height = round($height, 2);
3945
$this->height = $height;
4046
}
4147

@@ -75,12 +81,12 @@ public function getOccurrenceKey(): ?string
7581
return $this->occurrenceKey;
7682
}
7783

78-
public function getWidth(): ?int
84+
public function getWidth(): ?float
7985
{
8086
return $this->width;
8187
}
8288

83-
public function getHeight(): ?int
89+
public function getHeight(): ?float
8490
{
8591
return $this->height;
8692
}

tests/Exporter/Html/Block/DocumentExporterTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -211,28 +211,28 @@ public function testDocumentWithMediaSingle(): void
211211
{
212212
$document = (new Document())
213213
->mediaSingle(MediaSingle::LAYOUT_WIDE)
214-
->media('6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5', Media::TYPE_FILE, 'my project files', 100, 200)
214+
->media('6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5', Media::TYPE_FILE, 'my project files', 110.7, 200)
215215
->end()
216216
->end()
217217
;
218218
$exporter = new DocumentExporter($document);
219219

220-
self::assertSame('<div class="adf-container"><div class="adf-mediasingle"><div class="adf-media"><!--{"type":"media","attrs":{"id":"6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5","type":"file","collection":"my project files","width":100,"height":200}}--><p>Atlassian Media API is not publicly available at the moment.</p></div></div></div>', $exporter->export());
220+
self::assertSame('<div class="adf-container"><div class="adf-mediasingle"><div class="adf-media"><!--{"type":"media","attrs":{"id":"6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5","type":"file","collection":"my project files","width":110.7,"height":200}}--><p>Atlassian Media API is not publicly available at the moment.</p></div></div></div>', $exporter->export());
221221
}
222222

223223
public function testDocumentWithMediaGroup(): void
224224
{
225225
$document = (new Document())
226226
->mediaGroup()
227-
->media('6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5', Media::TYPE_FILE, 'my project files', 100, 200)
227+
->media('6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5', Media::TYPE_FILE, 'my project files', 100.335, 200)
228228
->end()
229-
->media('7a7c7f2c-dd7a-499c-bceb-6f32bfbf30c7', Media::TYPE_FILE, 'my project files', 100, 200)
229+
->media('7a7c7f2c-dd7a-499c-bceb-6f32bfbf30c7', Media::TYPE_FILE, 'my project files', 100, 220.5)
230230
->end()
231231
->end()
232232
;
233233
$exporter = new DocumentExporter($document);
234234

235-
self::assertSame('<div class="adf-container"><div class="adf-mediagroup"><div class="adf-media"><!--{"type":"media","attrs":{"id":"6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5","type":"file","collection":"my project files","width":100,"height":200}}--><p>Atlassian Media API is not publicly available at the moment.</p></div><div class="adf-media"><!--{"type":"media","attrs":{"id":"7a7c7f2c-dd7a-499c-bceb-6f32bfbf30c7","type":"file","collection":"my project files","width":100,"height":200}}--><p>Atlassian Media API is not publicly available at the moment.</p></div></div></div>', $exporter->export());
235+
self::assertSame('<div class="adf-container"><div class="adf-mediagroup"><div class="adf-media"><!--{"type":"media","attrs":{"id":"6e7c7f2c-dd7a-499c-bceb-6f32bfbf30b5","type":"file","collection":"my project files","width":100.34,"height":200}}--><p>Atlassian Media API is not publicly available at the moment.</p></div><div class="adf-media"><!--{"type":"media","attrs":{"id":"7a7c7f2c-dd7a-499c-bceb-6f32bfbf30c7","type":"file","collection":"my project files","width":100,"height":220.5}}--><p>Atlassian Media API is not publicly available at the moment.</p></div></div></div>', $exporter->export());
236236
}
237237

238238
public function testDocumentWithTable(): void

0 commit comments

Comments
 (0)