-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fa5fda
commit 338204d
Showing
11 changed files
with
301 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\Builder\Pdf; | ||
|
||
use Sensiolabs\GotenbergBundle\Enumeration\PdfFormat; | ||
use Sensiolabs\GotenbergBundle\Exception\InvalidBuilderConfiguration; | ||
use Sensiolabs\GotenbergBundle\Exception\MissingRequiredFieldException; | ||
use Symfony\Component\Mime\Part\DataPart; | ||
use Symfony\Component\Mime\Part\File as DataPartFile; | ||
|
||
final class MergePdfBuilder extends AbstractPdfBuilder | ||
{ | ||
private const ENDPOINT = '/forms/pdfengines/merge'; | ||
|
||
/** | ||
* To set configurations by an array of configurations. | ||
* | ||
* @param array<string, mixed> $configurations | ||
*/ | ||
public function setConfigurations(array $configurations): self | ||
{ | ||
foreach ($configurations as $property => $value) { | ||
$this->addConfiguration($property, $value); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Convert the resulting PDF into the given PDF/A format. | ||
*/ | ||
public function pdfFormat(PdfFormat $format): self | ||
{ | ||
$this->formFields['pdfa'] = $format->value; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Enable PDF for Universal Access for optimal accessibility. | ||
*/ | ||
public function pdfUniversalAccess(bool $bool = true): self | ||
{ | ||
$this->formFields['pdfua'] = $bool; | ||
|
||
return $this; | ||
} | ||
|
||
public function files(string ...$paths): self | ||
{ | ||
$this->formFields['files'] = []; | ||
|
||
foreach ($paths as $path) { | ||
$this->assertFileExtension($path, ['pdf']); | ||
|
||
$dataPart = new DataPart(new DataPartFile($this->asset->resolve($path))); | ||
|
||
$this->formFields['files'][$path] = $dataPart; | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Resets the metadata. | ||
* | ||
* @see https://gotenberg.dev/docs/routes#metadata-chromium | ||
* @see https://exiftool.org/TagNames/XMP.html#pdf | ||
* | ||
* @param array<string, mixed> $metadata | ||
*/ | ||
public function metadata(array $metadata): static | ||
{ | ||
$this->formFields['metadata'] = $metadata; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* The metadata to write. | ||
*/ | ||
public function addMetadata(string $key, string $value): static | ||
{ | ||
$this->formFields['metadata'] ??= []; | ||
$this->formFields['metadata'][$key] = $value; | ||
|
||
return $this; | ||
} | ||
|
||
public function getMultipartFormData(): array | ||
{ | ||
if ([] === ($this->formFields['files'] ?? [])) { | ||
throw new MissingRequiredFieldException('At least one PDF file is required'); | ||
} | ||
|
||
return parent::getMultipartFormData(); | ||
} | ||
|
||
protected function getEndpoint(): string | ||
{ | ||
return self::ENDPOINT; | ||
} | ||
|
||
private function addConfiguration(string $configurationName, mixed $value): void | ||
{ | ||
match ($configurationName) { | ||
'pdf_format' => $this->pdfFormat(PdfFormat::from($value)), | ||
'pdf_universal_access' => $this->pdfUniversalAccess($value), | ||
'metadata' => $this->metadata($value), | ||
default => throw new InvalidBuilderConfiguration(sprintf('Invalid option "%s": no method does not exist in class "%s" to configured it.', $configurationName, self::class)), | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Sensiolabs\GotenbergBundle\Tests\Builder\Pdf; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use PHPUnit\Framework\Attributes\UsesClass; | ||
use Sensiolabs\GotenbergBundle\Builder\Pdf\AbstractPdfBuilder; | ||
use Sensiolabs\GotenbergBundle\Builder\Pdf\MergePdfBuilder; | ||
use Sensiolabs\GotenbergBundle\Exception\MissingRequiredFieldException; | ||
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter; | ||
use Sensiolabs\GotenbergBundle\Tests\Builder\AbstractBuilderTestCase; | ||
|
||
#[CoversClass(MergePdfBuilder::class)] | ||
#[UsesClass(AbstractPdfBuilder::class)] | ||
#[UsesClass(AssetBaseDirFormatter::class)] | ||
final class MergePdfBuilderTest extends AbstractBuilderTestCase | ||
{ | ||
private const PDF_DOCUMENTS_DIR = 'pdf'; | ||
|
||
public function testEndpointIsCorrect(): void | ||
{ | ||
$this->gotenbergClient | ||
->expects($this->once()) | ||
->method('call') | ||
->with( | ||
$this->equalTo('/forms/pdfengines/merge'), | ||
$this->anything(), | ||
$this->anything(), | ||
) | ||
; | ||
|
||
$this->getMergePdfBuilder() | ||
->files( | ||
self::PDF_DOCUMENTS_DIR.'/simple_pdf.pdf', | ||
self::PDF_DOCUMENTS_DIR.'/simple_pdf_1.pdf', | ||
) | ||
->generate() | ||
; | ||
} | ||
|
||
public static function configurationIsCorrectlySetProvider(): \Generator | ||
{ | ||
yield 'pdf_format' => ['pdf_format', 'PDF/A-1b', [ | ||
'pdfa' => 'PDF/A-1b', | ||
]]; | ||
yield 'pdf_universal_access' => ['pdf_universal_access', false, [ | ||
'pdfua' => 'false', | ||
]]; | ||
yield 'metadata' => ['metadata', ['Author' => 'SensioLabs'], [ | ||
'metadata' => '{"Author":"SensioLabs"}', | ||
]]; | ||
} | ||
|
||
/** | ||
* @param array<mixed> $expected | ||
*/ | ||
#[DataProvider('configurationIsCorrectlySetProvider')] | ||
public function testConfigurationIsCorrectlySet(string $key, mixed $value, array $expected): void | ||
{ | ||
$builder = $this->getMergePdfBuilder(); | ||
$builder->setConfigurations([ | ||
$key => $value, | ||
]); | ||
$builder->files( | ||
self::PDF_DOCUMENTS_DIR.'/simple_pdf.pdf', | ||
self::PDF_DOCUMENTS_DIR.'/simple_pdf_1.pdf', | ||
); | ||
|
||
self::assertEquals($expected, $builder->getMultipartFormData()[0]); | ||
} | ||
|
||
public function testRequiredFormData(): void | ||
{ | ||
$builder = $this->getMergePdfBuilder(); | ||
|
||
$this->expectException(MissingRequiredFieldException::class); | ||
$this->expectExceptionMessage('At least one PDF file is required'); | ||
|
||
$builder->getMultipartFormData(); | ||
} | ||
|
||
private function getMergePdfBuilder(): MergePdfBuilder | ||
{ | ||
return new MergePdfBuilder($this->gotenbergClient, self::$assetBaseDirFormatter); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.