Skip to content

Commit f7cc77c

Browse files
committed
minor #862 [Platform] Rename asBase64 to asDataUri on DeferredResult for clarity and add optional mimeType arg (chr-hertel)
This PR was merged into the main branch. Discussion ---------- [Platform] Rename `asBase64` to `asDataUri` on `DeferredResult` for clarity and add optional mimeType arg | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | Docs? | no | Issues | | License | MIT Just more consistent and convenient, see #861 Commits ------- df13cce Rename `asBase64` to `asDataUri` on `DeferredResult` for clarity and add optional mimeType arg
2 parents 52c0537 + df13cce commit f7cc77c

File tree

4 files changed

+15
-6
lines changed

4 files changed

+15
-6
lines changed

examples/huggingface/text-to-image.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@
2020
'task' => Task::TEXT_TO_IMAGE,
2121
]);
2222

23-
echo $result->asBase64().\PHP_EOL;
23+
echo $result->asDataUri().\PHP_EOL;

src/platform/src/Result/BinaryResult.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ public function toBase64(): string
3939
return base64_encode($this->data);
4040
}
4141

42-
public function toDataUri(): string
42+
public function toDataUri(?string $mimeType = null): string
4343
{
44-
if (null === $this->mimeType) {
44+
if (null === ($mimeType ?? $this->mimeType)) {
4545
throw new RuntimeException('Mime type is not set.');
4646
}
4747

48-
return 'data:'.$this->mimeType.';base64,'.$this->toBase64();
48+
return 'data:'.($mimeType ?? $this->mimeType).';base64,'.$this->toBase64();
4949
}
5050
}

src/platform/src/Result/DeferredResult.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,13 @@ public function asBinary(): string
9090
/**
9191
* @throws ExceptionInterface
9292
*/
93-
public function asBase64(): string
93+
public function asDataUri(?string $mimeType = null): string
9494
{
9595
$result = $this->as(BinaryResult::class);
9696

9797
\assert($result instanceof BinaryResult);
9898

99-
return $result->toDataUri();
99+
return $result->toDataUri($mimeType);
100100
}
101101

102102
/**

src/platform/tests/Result/BinaryResultTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,13 @@ public function testToDataUriThrowsExceptionWhenMimeTypeNotSet()
5858

5959
$result->toDataUri();
6060
}
61+
62+
public function testToDataUriWithMimeTypeExplicitlySet()
63+
{
64+
$result = new BinaryResult('binary data');
65+
$actual = $result->toDataUri('image/jpeg');
66+
$expected = 'data:image/jpeg;base64,'.base64_encode('binary data');
67+
68+
$this->assertSame($expected, $actual);
69+
}
6170
}

0 commit comments

Comments
 (0)