Skip to content

Commit

Permalink
Merge pull request #278 from gsteel/access-original-payload
Browse files Browse the repository at this point in the history
Provide access to the server payload
  • Loading branch information
gsteel committed Aug 6, 2024
2 parents 2a85672 + 0c563fa commit 6932709
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
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());
}
}

0 comments on commit 6932709

Please sign in to comment.