From 6b0529999cb408e0b0dcead47e7497c6a36443d3 Mon Sep 17 00:00:00 2001 From: Martin Kluska Date: Thu, 18 Aug 2022 19:42:12 +0200 Subject: [PATCH] Add abbility to getRequiredObject --- docs/content/en/index.md | 2 ++ src/GetValue.php | 20 ++++++++++++++++++++ tests/GetValueXMLDataTest.php | 19 +++++++++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/docs/content/en/index.md b/docs/content/en/index.md index 18a6e0f..9222f12 100644 --- a/docs/content/en/index.md +++ b/docs/content/en/index.md @@ -368,6 +368,8 @@ $attributes->getString('attributeName'); - Allows getting object of specified type. - You can transform raw value to expected object by using transformers (with `GetterTransformer`) +- Use `getObject` if value is nullable +- Use `getRequiredObject` to throw missing value exception if value is null (since v0.6.2) ```php use Wrkflow\GetValue\Contracts\GetValueTransformerContract; diff --git a/src/GetValue.php b/src/GetValue.php index 650e39d..7c5380e 100644 --- a/src/GetValue.php +++ b/src/GetValue.php @@ -526,6 +526,26 @@ public function getObject( return null; } + /** + * @template T of object + * @param class-string $expectedClass + * + * @return T + */ + public function getRequiredObject( + string $expectedClass, + string|array $key, + GetValueTransformerContract $getValueTransformer + ): object { + $result = $this->getObject($expectedClass, $key, $getValueTransformer); + + if ($result === null) { + throw $this->exceptionBuilder->missingValue($this->data->getKey($key)); + } + + return $result; + } + public function makeInstance(AbstractData $data): self { return new self( diff --git a/tests/GetValueXMLDataTest.php b/tests/GetValueXMLDataTest.php index e022012..a063731 100644 --- a/tests/GetValueXMLDataTest.php +++ b/tests/GetValueXMLDataTest.php @@ -93,8 +93,11 @@ public function testGetXMLGetter(): void public function testGetObject(): void { $result = $this->data->getObject(TestEntity::class, 'object', new TestEntityTransformer()); - $this->assertNotNull($result); - $this->assertEquals('x', $result->type); + $this->assertObjectResult($result); + + $result = $this->data->getRequiredObject(TestEntity::class, 'object', new TestEntityTransformer()); + + $this->assertObjectResult($result); } public function testGetObjectIncorrectResult(): void @@ -103,6 +106,12 @@ public function testGetObjectIncorrectResult(): void $this->assertNull($result); } + public function testGetRequiredObjectIncorrectResult(): void + { + $this->expectException(MissingValueForKeyException::class); + $this->data->getRequiredObject(TestEntity::class, 'object', new NullTransformer()); + } + protected function assertObject(GetValue $object): void { $this->assertEquals('x', $object->getRequiredString('type')); @@ -113,4 +122,10 @@ protected function assertObject(GetValue $object): void $this->assertEquals('test', $child->getRequiredString(self::KeyTitle)); $this->assertEquals(null, $child->getString('title2')); } + + protected function assertObjectResult(mixed $result): void + { + $this->assertNotNull($result); + $this->assertEquals('x', $result->type); + } }