Skip to content

Commit

Permalink
Add header / footer defaults + allow both Stringable and BackedEnums (#…
Browse files Browse the repository at this point in the history
…69)

* [UPDATE] Add default header / footer through configuration 

* [UPDATE] allow BackedEnums

* [UPDATE] Allow Stringable
  • Loading branch information
Neirda24 committed Jun 17, 2024
1 parent d0c7748 commit 0fa5fda
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 12 deletions.
6 changes: 4 additions & 2 deletions src/Builder/Pdf/AbstractChromiumPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public function waitForExpression(string $expression): static
*/
public function emulatedMediaType(EmulatedMediaType $mediaType): static
{
$this->formFields['emulatedMediaType'] = $mediaType->value;
$this->formFields['emulatedMediaType'] = $mediaType;

return $this;
}
Expand Down Expand Up @@ -445,7 +445,7 @@ public function pdfFormat(PdfFormat|null $format = null): static
return $this;
}

$this->formFields['pdfa'] = $format->value;
$this->formFields['pdfa'] = $format;

return $this;
}
Expand Down Expand Up @@ -526,6 +526,8 @@ protected function withRenderedPart(Part $pdfPart, string $template, array $cont
protected function addConfiguration(string $configurationName, mixed $value): void
{
match ($configurationName) {
'header' => $this->header(...$value),
'footer' => $this->footer(...$value),
'single_page' => $this->singlePage($value),
'pdf_format' => $this->pdfFormat(PdfFormat::from($value)),
'pdf_universal_access' => $this->pdfUniversalAccess($value),
Expand Down
18 changes: 15 additions & 3 deletions src/Builder/Pdf/AbstractPdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class AbstractPdfBuilder implements PdfBuilderInterface
private string $headerDisposition = HeaderUtils::DISPOSITION_INLINE;

/**
* @var array<string, (\Closure(mixed): array<string, array<string|int, mixed>|non-empty-string|int|float|bool|DataPart>)>
* @var array<string, (\Closure(mixed): array<string, array<string|int, mixed>|non-empty-string|\Stringable|int|float|bool|\BackedEnum|DataPart>)>
*/
private array $normalizers;

Expand Down Expand Up @@ -157,11 +157,11 @@ protected function addNormalizer(string $key, \Closure $normalizer): void
}

/**
* @param array<int|string, mixed>|string|int|float|bool|DataPart $value
* @param array<int|string, mixed>|string|\Stringable|int|float|bool|\BackedEnum|DataPart $value
*
* @return list<array<string, mixed>>
*/
private function addToMultipart(string $key, array|string|int|float|bool|DataPart $value, \Closure|null $preCallback = null): array
private function addToMultipart(string $key, array|string|\Stringable|int|float|bool|\BackedEnum|DataPart $value, \Closure|null $preCallback = null): array
{
if (null !== $preCallback) {
$result = [];
Expand Down Expand Up @@ -195,6 +195,18 @@ private function addToMultipart(string $key, array|string|int|float|bool|DataPar
]];
}

if ($value instanceof \BackedEnum) {
return [[
$key => (string) $value->value,
]];
}

if ($value instanceof \Stringable) {
return [[
$key => (string) $value,
]];
}

if (\is_array($value)) {
$result = [];
foreach ($value as $nestedValue) {
Expand Down
2 changes: 1 addition & 1 deletion src/Builder/Pdf/LibreOfficePdfBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function singlePageSheets(bool $bool = true): self
*/
public function pdfFormat(PdfFormat $format): self
{
$this->formFields['pdfa'] = $format->value;
$this->formFields['pdfa'] = $format;

return $this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function clip(bool $bool = true): static
*/
public function format(ScreenshotFormat $format): static
{
$this->formFields['format'] = $format->value;
$this->formFields['format'] = $format;

return $this;
}
Expand Down
18 changes: 15 additions & 3 deletions src/Builder/Screenshot/AbstractScreenshotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ abstract class AbstractScreenshotBuilder implements ScreenshotBuilderInterface
private string $headerDisposition = HeaderUtils::DISPOSITION_INLINE;

/**
* @var array<string, (\Closure(mixed): array<string, array<string|int, mixed>|non-empty-string|int|float|bool|DataPart>)>
* @var array<string, (\Closure(mixed): array<string, array<string|int, mixed>|non-empty-string|\Stringable|int|float|bool|\BackedEnum|DataPart>)>
*/
private array $normalizers;

Expand Down Expand Up @@ -145,11 +145,11 @@ protected function addNormalizer(string $key, \Closure $normalizer): void
}

/**
* @param array<int|string, mixed>|string|int|float|bool|DataPart $value
* @param array<int|string, mixed>|string|\Stringable|int|float|bool|\BackedEnum|DataPart $value
*
* @return list<array<string, mixed>>
*/
private function addToMultipart(string $key, array|string|int|float|bool|DataPart $value, \Closure|null $preCallback = null): array
private function addToMultipart(string $key, array|string|\Stringable|int|float|bool|\BackedEnum|DataPart $value, \Closure|null $preCallback = null): array
{
if (null !== $preCallback) {
$result = [];
Expand Down Expand Up @@ -183,6 +183,18 @@ private function addToMultipart(string $key, array|string|int|float|bool|DataPar
]];
}

if ($value instanceof \BackedEnum) {
return [[
$key => (string) $value->value,
]];
}

if ($value instanceof \Stringable) {
return [[
$key => (string) $value,
]];
}

if (\is_array($value)) {
$result = [];
foreach ($value as $nestedValue) {
Expand Down
26 changes: 26 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,32 @@ private function addChromiumPdfOptionsNode(ArrayNodeDefinition $parent): void
{
$parent
->children()
->arrayNode('header')
->info('Add default header to the builder.')
->children()
->scalarNode('template')
->info('Default header twig template to apply.')
->defaultNull()
->end()
->scalarNode('context')
->info('Default context for header twig template.')
->defaultValue([])
->end()
->end()
->end()
->arrayNode('footer')
->info('Add default footer to the builder.')
->children()
->scalarNode('template')
->info('Default footer twig template to apply.')
->defaultNull()
->end()
->scalarNode('context')
->info('Default context for footer twig template.')
->defaultValue([])
->end()
->end()
->end()
->booleanNode('single_page')
->info('Define whether to print the entire content in one single page. - default false. https://gotenberg.dev/docs/routes#page-properties-chromium')
->defaultNull()
Expand Down
6 changes: 6 additions & 0 deletions tests/Builder/Pdf/AbstractPdfBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sensiolabs\GotenbergBundle\Client\GotenbergClient;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Client\GotenbergResponse;
use Sensiolabs\GotenbergBundle\Enumeration\PdfFormat;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Sensiolabs\GotenbergBundle\Tests\Builder\AbstractBuilderTestCase;
use Symfony\Component\HttpClient\MockHttpClient;
Expand Down Expand Up @@ -81,6 +82,11 @@ public static function formFieldsNormalizerProvider(): \Generator
['metadata' => ['Author' => 'SensioLabs']],
'metadata', '{"Author":"SensioLabs"}',
];

yield 'using BackedEnum' => [
['backed_enum' => PdfFormat::Pdf3b],
'backed_enum', 'PDF/A-3b',
];
}

#[DataProvider('formFieldsNormalizerProvider')]
Expand Down
6 changes: 6 additions & 0 deletions tests/Builder/Screenshot/AbstractScreenshotBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Sensiolabs\GotenbergBundle\Client\GotenbergClient;
use Sensiolabs\GotenbergBundle\Client\GotenbergClientInterface;
use Sensiolabs\GotenbergBundle\Client\GotenbergResponse;
use Sensiolabs\GotenbergBundle\Enumeration\PdfFormat;
use Sensiolabs\GotenbergBundle\Formatter\AssetBaseDirFormatter;
use Sensiolabs\GotenbergBundle\Tests\Builder\AbstractBuilderTestCase;
use Symfony\Component\HttpClient\MockHttpClient;
Expand Down Expand Up @@ -66,6 +67,11 @@ public static function formFieldsNormalizerProvider(): \Generator
['cookies' => ['MyCookie' => ['name' => 'MyCookieName', 'value' => 'Chocolate', 'domain' => 'sensiolabs.com']]],
'cookies', '[{"name":"MyCookieName","value":"Chocolate","domain":"sensiolabs.com"}]',
];

yield 'using BackedEnum' => [
['backed_enum' => PdfFormat::Pdf3b],
'backed_enum', 'PDF/A-3b',
];
}

#[DataProvider('formFieldsNormalizerProvider')]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Sensiolabs\GotenbergBundle\Tests\Enum;
namespace Sensiolabs\GotenbergBundle\Tests\Enumeration;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand Down
2 changes: 1 addition & 1 deletion tests/Enum/UnitTest.php → tests/Enumeration/UnitTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Enum;
namespace Sensiolabs\GotenbergBundle\Tests\Enumeration;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down

0 comments on commit 0fa5fda

Please sign in to comment.