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

Provide access to the server payload #278

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
9 changes: 1 addition & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.8.0@9cf4f60a333f779ad3bc704a555920e81d4fdcda">
<file src="src/Postmark/Models/CaseInsensitiveArray.php">
<MissingTemplateParam>
<code>ArrayAccess</code>
<code>Iterator</code>
</MissingTemplateParam>
</file>
</files>
<files psalm-version="5.20.0@3f284e96c9d9be6fe6b15c79416e1d1903dcfef4"/>
16 changes: 16 additions & 0 deletions src/Postmark/Models/CaseInsensitiveArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,17 @@
* CaseInsensitiveArray allows accessing elements with mixed-case keys.
* This allows access to the array to be very forgiving. (i.e. If you access something
* with the wrong CaSe, it'll still find the correct element)
*
* @implements ArrayAccess<array-key, mixed>
* @implements Iterator<array-key, mixed>
*/
class CaseInsensitiveArray implements ArrayAccess, Iterator
{
/** @var array<array-key, mixed> */
private array $data;
private int $pointer = 0;
/** @var array<array-key, mixed> */
private array $payload;

private function normaliseOffset(string $offset): string
{
Expand All @@ -39,9 +44,20 @@ private function normaliseOffset(string $offset): string
*/
public function __construct(array $initialArray = [])
{
$this->payload = $initialArray;
$this->data = array_change_key_case($initialArray, CASE_LOWER);
}

/**
* Provides a way of accessing the initial payload as returned by the server with key casing preserved
*
* @return array<array-key, mixed>
*/
public function getPayload(): array
{
return $this->payload;
}

/** @param array-key|null $offset */
public function offsetSet($offset, mixed $value): void
{
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/Models/CaseInsensitiveArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,30 @@ public function testKeysCanBeUnsetInCaseInsensitiveManner(): void
unset($data['fOo']);
self::assertArrayNotHasKey('foo', $data);
}

public function testThatTheInitialPayloadCanBeAccessed(): void
{
$payload = [
'Foo' => 'foo',
'bar' => 'bar',
'bAz' => 'baz',
'BING' => 'bing',
];

$data = new CaseInsensitiveArray($payload);

self::assertSame($payload, $data->getPayload());
}

public function testThatTheInitialPayloadWillIncludeNestedArrays(): void
{
$payload = [
'SomeScalar' => 'foo',
'NestedArray' => ['OtherValue' => 'Bar'],
];

$data = new CaseInsensitiveArray($payload);

self::assertSame($payload, $data->getPayload());
}
}