diff --git a/src/Fields.php b/src/Fields.php index 6f0f24e..f44073b 100644 --- a/src/Fields.php +++ b/src/Fields.php @@ -117,21 +117,35 @@ public function getIterator(): Traversable return $iterator; } + /** + * @param string $offset + */ public function offsetExists(mixed $offset): bool { return isset($this->fields[$offset]); } + /** + * @param string $offset + * @return true|Fields|null + */ public function offsetGet(mixed $offset): mixed { - return $this->fields[$offset]; + return $this->fields[$offset] ?? null; } + /** + * @param string $offset + * @param true|Fields $value + */ public function offsetSet(mixed $offset, mixed $value): void { $this->fields[$offset] = $value; } + /** + * @param string $offset + */ public function offsetUnset(mixed $offset): void { unset($this->fields[$offset]); diff --git a/tests/FieldsTest.php b/tests/FieldsTest.php index 656ea9c..111252c 100644 --- a/tests/FieldsTest.php +++ b/tests/FieldsTest.php @@ -8,10 +8,26 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use PHPUnit\Framework\TestStatus\Warning; #[CoversClass(Fields::class)] class FieldsTest extends TestCase { + public function testArrayAccess(): void + { + $fields = new Fields(); + + $this->assertNull($fields['@id']); + + $fields['@id'] = true; + + $this->assertTrue($fields['@id']); + + $fields['eventDate'] = new Fields(); + + $this->assertInstanceOf(Fields::class, $fields['eventDate']); + } + public function testKeys(): void { $fields = Fields::fromArray([