Skip to content

Commit

Permalink
Allow \SplFileInfo or \Stringable (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
mleczakm authored Mar 5, 2025
1 parent ec39850 commit c10df88
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/pdf/builders_api/ConvertPdfBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Convert the resulting PDF into the given PDF/A format.
* `pdfUniversalAccess(bool $bool)`:
Enable PDF for Universal Access for optimal accessibility.

* `files(string $paths)`:
* `files(Stringable|string $paths)`:

* `downloadFrom(array $downloadFrom)`:

Expand Down
2 changes: 1 addition & 1 deletion docs/pdf/builders_api/LibreOfficePdfBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Enable PDF for Universal Access for optimal accessibility.
* `merge(bool $bool)`:
Merge alphanumerically the resulting PDFs.

* `files(string $paths)`:
* `files(Stringable|string $paths)`:
Adds office files to convert (overrides any previous files).

* `metadata(array $metadata)`:
Expand Down
2 changes: 1 addition & 1 deletion docs/pdf/builders_api/MarkdownPdfBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The HTML file that wraps the markdown content, rendered from a Twig template.
* `wrapperFile(string $path)`:
The HTML file that wraps the markdown content.

* `files(string $paths)`:
* `files(Stringable|string $paths)`:

* `cookies(array $cookies)`:

Expand Down
2 changes: 1 addition & 1 deletion docs/pdf/builders_api/MergePdfBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Convert the resulting PDF into the given PDF/A format.
* `pdfUniversalAccess(bool $bool)`:
Enable PDF for Universal Access for optimal accessibility.

* `files(string $paths)`:
* `files(Stringable|string $paths)`:

* `metadata(array $metadata)`:
Resets the metadata.
Expand Down
2 changes: 1 addition & 1 deletion docs/pdf/builders_api/SplitPdfBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Either the intervals or the page ranges to extract, depending on the selected mo
* `splitUnify(bool $bool)`:
Specify whether to put extracted pages into a single file or as many files as there are page ranges. Only works with pages mode. (default false).

* `files(string $paths)`:
* `files(Stringable|string $paths)`:

* `downloadFrom(array $downloadFrom)`:

Expand Down
2 changes: 1 addition & 1 deletion docs/screenshot/builders_api/MarkdownScreenshotBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The HTML file that wraps the markdown content, rendered from a Twig template.
* `wrapperFile(string $path)`:
The HTML file that wraps the markdown content.

* `files(string $paths)`:
* `files(Stringable|string $paths)`:

* `cookies(array $cookies)`:

Expand Down
3 changes: 2 additions & 1 deletion src/Builder/Pdf/ConvertPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ public function pdfUniversalAccess(bool $bool = true): self
return $this;
}

public function files(string ...$paths): self
public function files(string|\Stringable ...$paths): self
{
$this->formFields['files'] = [];

foreach ($paths as $path) {
$path = (string) $path;
$this->assertFileExtension($path, ['pdf']);

$dataPart = new DataPart(new DataPartFile($this->asset->resolve($path)));
Expand Down
3 changes: 2 additions & 1 deletion src/Builder/Pdf/LibreOfficePdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,12 @@ public function merge(bool $bool = true): self
/**
* Adds office files to convert (overrides any previous files).
*/
public function files(string ...$paths): self
public function files(string|\Stringable ...$paths): self
{
$this->formFields['files'] = [];

foreach ($paths as $path) {
$path = (string) $path;
$this->assertFileExtension($path, self::AVAILABLE_EXTENSIONS);

$dataPart = new DataPart(new DataPartFile($this->asset->resolve($path)));
Expand Down
3 changes: 2 additions & 1 deletion src/Builder/Pdf/MarkdownPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public function wrapperFile(string $path): self
return $this->withPdfPartFile(Part::Body, $path);
}

public function files(string ...$paths): self
public function files(string|\Stringable ...$paths): self
{
$this->formFields['files'] = [];

foreach ($paths as $path) {
$path = (string) $path;
$this->assertFileExtension($path, ['md']);

$dataPart = new DataPart(new DataPartFile($this->asset->resolve($path)));
Expand Down
3 changes: 2 additions & 1 deletion src/Builder/Pdf/MergePdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ public function pdfUniversalAccess(bool $bool = true): self
return $this;
}

public function files(string ...$paths): self
public function files(string|\Stringable ...$paths): self
{
$this->formFields['files'] = [];

foreach ($paths as $path) {
$path = (string) $path;
$this->assertFileExtension($path, ['pdf']);

$dataPart = new DataPart(new DataPartFile($this->asset->resolve($path)));
Expand Down
3 changes: 2 additions & 1 deletion src/Builder/Pdf/SplitPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ public function splitUnify(bool $bool = true): self
return $this;
}

public function files(string ...$paths): self
public function files(string|\Stringable ...$paths): self
{
$this->formFields['files'] = [];

foreach ($paths as $path) {
$path = (string) $path;
$this->assertFileExtension($path, ['pdf']);

$dataPart = new DataPart(new DataPartFile($this->asset->resolve($path)));
Expand Down
3 changes: 2 additions & 1 deletion src/Builder/Screenshot/MarkdownScreenshotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ public function wrapperFile(string $path): self
return $this->withScreenshotPartFile(Part::Body, $path);
}

public function files(string ...$paths): self
public function files(string|\Stringable ...$paths): self
{
$this->formFields['files'] = [];

foreach ($paths as $path) {
$path = (string) $path;
$this->assertFileExtension($path, ['md']);

$dataPart = new DataPart(new DataPartFile($this->asset->resolve($path)));
Expand Down
44 changes: 44 additions & 0 deletions tests/Builder/Pdf/ConvertPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Sensiolabs\GotenbergBundle\Processor\NullProcessor;
use Sensiolabs\GotenbergBundle\Tests\Builder\AbstractBuilderTestCase;
use Symfony\Component\Mime\Part\DataPart;

#[CoversClass(ConvertPdfBuilder::class)]
#[UsesClass(AbstractPdfBuilder::class)]
Expand Down Expand Up @@ -78,6 +79,49 @@ public function testRequiredFormat(): void
$builder->getMultipartFormData();
}

public function testWithStringableObject(): void
{
$stringable = new class(self::PDF_DOCUMENTS_DIR) implements \Stringable {
public function __construct(private string $directory)
{
}

public function __toString(): string
{
return $this->directory.'/document.pdf';
}
};

$builder = $this->getConvertPdfBuilder();
$builder
->files($stringable)
->pdfUniversalAccess()
;

$data = $builder->getMultipartFormData();

/* @var DataPart $dataPart */
self::assertInstanceOf(DataPart::class, $dataPart = $data[0]['files']);
self::assertSame(basename((string) $stringable), $dataPart->getFilename());
}

public function testSplFileInfoObject(): void
{
$splFileInfo = new \SplFileInfo(self::PDF_DOCUMENTS_DIR.'/document.pdf');

$builder = $this->getConvertPdfBuilder();
$builder
->files($splFileInfo)
->pdfUniversalAccess()
;

$data = $builder->getMultipartFormData();

/* @var DataPart $dataPart */
self::assertInstanceOf(DataPart::class, $dataPart = $data[0]['files']);
self::assertSame(basename((string) $splFileInfo), $dataPart->getFilename());
}

public function testRequiredPdfFile(): void
{
$builder = $this->getConvertPdfBuilder();
Expand Down
46 changes: 45 additions & 1 deletion tests/Builder/Pdf/LibreOfficePdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Sensiolabs\GotenbergBundle\Processor\NullProcessor;
use Sensiolabs\GotenbergBundle\Tests\Builder\AbstractBuilderTestCase;
use Symfony\Component\Mime\Part\DataPart;

#[CoversClass(LibreOfficePdfBuilder::class)]
#[UsesClass(AbstractPdfBuilder::class)]
Expand All @@ -34,11 +35,54 @@ public function testEndpointIsCorrect(): void
;

$this->getLibreOfficePdfBuilder()
->files(self::OFFICE_DOCUMENTS_DIR.'/document_1.docx')
->files(new \SplFileInfo(self::OFFICE_DOCUMENTS_DIR.'/document_1.docx'))
->generate()
;
}

public function testStringableObject(): void
{
$supportedFilePath = new class(self::OFFICE_DOCUMENTS_DIR) implements \Stringable {
public function __construct(private readonly string $directory)
{
}

public function __toString(): string
{
return $this->directory.'/document_1.docx';
}
};

$builder = $this->getLibreOfficePdfBuilder();
$builder
->files($supportedFilePath)
->pdfUniversalAccess()
;

$data = $builder->getMultipartFormData();

/* @var DataPart $dataPart */
self::assertInstanceOf(DataPart::class, $dataPart = $data[0]['files']);
self::assertSame(basename((string) $supportedFilePath), $dataPart->getFilename());
}

public function testSplFileInfoObject(): void
{
$supportedFilePath = new \SplFileInfo(self::OFFICE_DOCUMENTS_DIR.'/document_1.docx');

$builder = $this->getLibreOfficePdfBuilder();
$builder
->files($supportedFilePath)
->pdfUniversalAccess()
;

$data = $builder->getMultipartFormData();

/* @var DataPart $dataPart */
self::assertInstanceOf(DataPart::class, $dataPart = $data[0]['files']);
self::assertSame(basename((string) $supportedFilePath), $dataPart->getFilename());
}

public static function configurationIsCorrectlySetProvider(): \Generator
{
yield 'pdf_format' => ['pdf_format', 'PDF/A-1b', [
Expand Down
40 changes: 40 additions & 0 deletions tests/Builder/Pdf/MergePdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Sensiolabs\GotenbergBundle\Processor\NullProcessor;
use Sensiolabs\GotenbergBundle\Tests\Builder\AbstractBuilderTestCase;
use Symfony\Component\Mime\Part\DataPart;

#[CoversClass(MergePdfBuilder::class)]
#[UsesClass(AbstractPdfBuilder::class)]
Expand Down Expand Up @@ -83,6 +84,45 @@ public function testRequiredFormData(): void
$builder->getMultipartFormData();
}

public function testStringableObject(): void
{
$stringable = new class(self::PDF_DOCUMENTS_DIR) implements \Stringable {
public function __construct(private string $directory)
{
}

public function __toString(): string
{
return $this->directory.'/simple_pdf.pdf';
}
};
$builder = $this->getMergePdfBuilder();
$builder
->files($stringable)
;

$data = $builder->getMultipartFormData();

/* @var DataPart $dataPart */
self::assertInstanceOf(DataPart::class, $dataPart = $data[0]['files']);
self::assertSame(basename((string) $stringable), $dataPart->getFilename());
}

public function testSplFileInfoObject(): void
{
$splFileInfo = new \SplFileInfo(self::PDF_DOCUMENTS_DIR.'/simple_pdf.pdf');
$builder = $this->getMergePdfBuilder();
$builder
->files($splFileInfo)
;

$data = $builder->getMultipartFormData();

/* @var DataPart $dataPart */
self::assertInstanceOf(DataPart::class, $dataPart = $data[0]['files']);
self::assertSame(basename((string) $splFileInfo), $dataPart->getFilename());
}

private function getMergePdfBuilder(): MergePdfBuilder
{
return (new MergePdfBuilder($this->gotenbergClient, self::$assetBaseDirFormatter, $this->webhookConfigurationRegistry))
Expand Down
46 changes: 46 additions & 0 deletions tests/Builder/Pdf/SplitPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Sensiolabs\GotenbergBundle\Processor\NullProcessor;
use Sensiolabs\GotenbergBundle\Tests\Builder\AbstractBuilderTestCase;
use Symfony\Component\Mime\Part\DataPart;

#[CoversClass(SplitPdfBuilder::class)]
#[UsesClass(AbstractPdfBuilder::class)]
Expand Down Expand Up @@ -111,6 +112,51 @@ public function testRequiredFile(): void
$builder->getMultipartFormData();
}

public function testStringableObject(): void
{
$supportedFilePath = new class(self::PDF_DOCUMENTS_DIR) implements \Stringable {
public function __construct(private string $directory)
{
}

public function __toString(): string
{
return $this->directory.'/simple_pdf.pdf';
}
};

$builder = $this->getSplitPdfBuilder();
$builder
->files($supportedFilePath)
->splitMode(SplitMode::Pages)
->splitSpan('1')
;

$data = $builder->getMultipartFormData();

/* @var DataPart $dataPart */
self::assertInstanceOf(DataPart::class, $dataPart = $data[0]['files']);
self::assertSame(basename((string) $supportedFilePath), $dataPart->getFilename());
}

public function testSplFileInfoObject(): void
{
$supportedFilePath = new \SplFileInfo(self::PDF_DOCUMENTS_DIR.'/simple_pdf.pdf');

$builder = $this->getSplitPdfBuilder();
$builder
->files($supportedFilePath)
->splitMode(SplitMode::Pages)
->splitSpan('1')
;

$data = $builder->getMultipartFormData();

/* @var DataPart $dataPart */
self::assertInstanceOf(DataPart::class, $dataPart = $data[0]['files']);
self::assertSame(basename((string) $supportedFilePath), $dataPart->getFilename());
}

private function getSplitPdfBuilder(): SplitPdfBuilder
{
return (new SplitPdfBuilder($this->gotenbergClient, self::$assetBaseDirFormatter, $this->webhookConfigurationRegistry))
Expand Down
Loading

0 comments on commit c10df88

Please sign in to comment.