Skip to content

Commit 6b05299

Browse files
committed
Add abbility to getRequiredObject
1 parent 3ea2a5c commit 6b05299

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

docs/content/en/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,8 @@ $attributes->getString('attributeName');
368368
369369
- Allows getting object of specified type.
370370
- You can transform raw value to expected object by using transformers (with `GetterTransformer`)
371+
- Use `getObject` if value is nullable
372+
- Use `getRequiredObject` to throw missing value exception if value is null (since v0.6.2)
371373

372374
```php
373375
use Wrkflow\GetValue\Contracts\GetValueTransformerContract;

src/GetValue.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,26 @@ public function getObject(
526526
return null;
527527
}
528528

529+
/**
530+
* @template T of object
531+
* @param class-string<T> $expectedClass
532+
*
533+
* @return T
534+
*/
535+
public function getRequiredObject(
536+
string $expectedClass,
537+
string|array $key,
538+
GetValueTransformerContract $getValueTransformer
539+
): object {
540+
$result = $this->getObject($expectedClass, $key, $getValueTransformer);
541+
542+
if ($result === null) {
543+
throw $this->exceptionBuilder->missingValue($this->data->getKey($key));
544+
}
545+
546+
return $result;
547+
}
548+
529549
public function makeInstance(AbstractData $data): self
530550
{
531551
return new self(

tests/GetValueXMLDataTest.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,11 @@ public function testGetXMLGetter(): void
9393
public function testGetObject(): void
9494
{
9595
$result = $this->data->getObject(TestEntity::class, 'object', new TestEntityTransformer());
96-
$this->assertNotNull($result);
97-
$this->assertEquals('x', $result->type);
96+
$this->assertObjectResult($result);
97+
98+
$result = $this->data->getRequiredObject(TestEntity::class, 'object', new TestEntityTransformer());
99+
100+
$this->assertObjectResult($result);
98101
}
99102

100103
public function testGetObjectIncorrectResult(): void
@@ -103,6 +106,12 @@ public function testGetObjectIncorrectResult(): void
103106
$this->assertNull($result);
104107
}
105108

109+
public function testGetRequiredObjectIncorrectResult(): void
110+
{
111+
$this->expectException(MissingValueForKeyException::class);
112+
$this->data->getRequiredObject(TestEntity::class, 'object', new NullTransformer());
113+
}
114+
106115
protected function assertObject(GetValue $object): void
107116
{
108117
$this->assertEquals('x', $object->getRequiredString('type'));
@@ -113,4 +122,10 @@ protected function assertObject(GetValue $object): void
113122
$this->assertEquals('test', $child->getRequiredString(self::KeyTitle));
114123
$this->assertEquals(null, $child->getString('title2'));
115124
}
125+
126+
protected function assertObjectResult(mixed $result): void
127+
{
128+
$this->assertNotNull($result);
129+
$this->assertEquals('x', $result->type);
130+
}
116131
}

0 commit comments

Comments
 (0)