Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow header instances in message delivery methods #281

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/Postmark/Models/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@

use JsonSerializable;

use function assert;

final class Header implements JsonSerializable
{
/**
* @param non-empty-string $name
* @param scalar|null $value
*/
public function __construct(private string $name, private $value) // phpcs:ignore
public function __construct(private readonly string $name, private $value) // phpcs:ignore
{
}

Expand All @@ -28,7 +26,7 @@ public function jsonSerialize(): array
}

/**
* @param array<string, scalar|null> $values
* @param array<string, scalar|null>|array<array-key, Header> $values
*
* @return list<self>|null
*/
Expand All @@ -40,7 +38,17 @@ public static function listFromArray(array|null $values): array|null

$list = [];
foreach ($values as $name => $value) {
assert(! empty($name));
if ($value instanceof self) {
$list[] = $value;
continue;
}

$name = (string) $name;

if ($name === '') {
continue;
}

$list[] = new self($name, $value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Postmark/PostmarkClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @link JsonSerializable (Preserve Import)
*
* @psalm-type Attachments = list<PostmarkAttachment>|null
* @psalm-type HeaderList = array<string, scalar|null>
* @psalm-type HeaderList = array<string, scalar|null>|array<array-key, Header>
* @psalm-type MetaData = array<string, scalar>
* @psalm-type TemplateId = non-empty-string|positive-int
* @psalm-type EmailMessage = array{
Expand Down
37 changes: 37 additions & 0 deletions tests/Unit/Models/HeaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,41 @@ public function testThatListReturnIsNullForAnEmptyInputArray(): void
{
self::assertNull(Header::listFromArray([]));
}

public function testThatHeaderInstancesCanBeUsed(): void
{
$input = [
'whatever' => new Header('X-Some-Header', 'Foo'),
'X-Other-Header' => 'Bar',
];

$expect = json_encode([
['Name' => 'X-Some-Header', 'Value' => 'Foo'],
['Name' => 'X-Other-Header', 'Value' => 'Bar'],
], JSON_THROW_ON_ERROR);

$headers = Header::listFromArray($input);

self::assertJsonStringEqualsJsonString($expect, json_encode($headers, JSON_THROW_ON_ERROR));
}

public function testThatEmptyKeysAreIgnored(): void
{
$input = [
0 => 'Some Value',
1 => 'Another Value',
'' => 'Not There',
'foo' => 'bar',
];

$expect = json_encode([
['Name' => '0', 'Value' => 'Some Value'],
['Name' => '1', 'Value' => 'Another Value'],
['Name' => 'foo', 'Value' => 'bar'],
], JSON_THROW_ON_ERROR);

$headers = Header::listFromArray($input);

self::assertJsonStringEqualsJsonString($expect, json_encode($headers, JSON_THROW_ON_ERROR));
}
}