Skip to content

Commit 3ea2a5c

Browse files
committed
Add ability to transformed object with correct type hint
1 parent 2e0e57b commit 3ea2a5c

File tree

8 files changed

+155
-2
lines changed

8 files changed

+155
-2
lines changed

docs/content/en/index.md

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,8 @@ $value = $data->getRequiredArrayGetter('key');
311311

312312
### Get SimpleXMLElement
313313

314+
> Since v0.6.0
315+
314316
Return SimpleXMLElement for given key. Uses `xml` transformer strategy.
315317

316318
```php
@@ -319,7 +321,8 @@ $xml = $data->getNullableXML('child');
319321

320322
### GetValue with XMLData
321323

322-
> Throws always `NotXMLException` exception if value is not a SimpleXMLElement. XML transformer strategy is applied.
324+
> Since v0.6.0. Throws always `NotXMLException` exception if value is not a SimpleXMLElement. XML transformer strategy
325+
> is applied.
323326
324327
Get always `GetValue` instance even if provided data is missing or if null.
325328

@@ -342,7 +345,8 @@ $value = $data->getRequiredXMLGetter('key');
342345

343346
### GetValue XML attributes
344347

345-
> Throws always `NotXMLException` exception if value is not a SimpleXMLElement. XML transformer strategy is applied.
348+
> Since v0.6.0. Throws always `NotXMLException` exception if value is not a SimpleXMLElement. XML transformer strategy
349+
> is applied.
346350
347351
Wraps XML element attributes in GetValue instance (even if attributes are not set in element).
348352

@@ -358,6 +362,51 @@ $attributes = $data->getXMLAttributesGetter('node');
358362
$attributes->getString('attributeName');
359363
```
360364

365+
### GetObject
366+
367+
> Since v0.6.1
368+
369+
- Allows getting object of specified type.
370+
- You can transform raw value to expected object by using transformers (with `GetterTransformer`)
371+
372+
```php
373+
use Wrkflow\GetValue\Contracts\GetValueTransformerContract;
374+
use Wrkflow\GetValue\GetValue;
375+
use Wrkflow\GetValue\DataHolders\ArrayData;
376+
use Wrkflow\GetValue\Transformers\GetterTransformer;
377+
378+
$data = new GetValue(new ArrayData([
379+
'person' => ['name' => 'Marco', 'surname' => 'Polo'],
380+
]));
381+
382+
class PersonEntity
383+
{
384+
public function __construct(
385+
public readonly string $firstName,
386+
public readonly string $lastName,
387+
) {
388+
}
389+
}
390+
391+
class TestEntityTransformer implements GetValueTransformerContract
392+
{
393+
public function transform(GetValue $value, string $key): PersonEntity
394+
{
395+
return new PersonEntity(
396+
firstName: $value->getRequiredString('name'),
397+
lastName: $value->getRequiredString('surname'),
398+
);
399+
}
400+
}
401+
402+
$person = $this->data->getObject(
403+
Person::class,
404+
'person',
405+
new PersonTransformer()
406+
);
407+
// PersonEntity{firstName=Marco,lastName=Polo}
408+
```
409+
361410
## Exceptions
362411

363412
> All exceptions receive full key that was used for getting data. You can receive it by using `$exception->getKey()`

src/GetValue.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Wrkflow\GetValue\Actions\ValidateAction;
1616
use Wrkflow\GetValue\Builders\ExceptionBuilder;
1717
use Wrkflow\GetValue\Contracts\ExceptionBuilderContract;
18+
use Wrkflow\GetValue\Contracts\GetValueTransformerContract;
1819
use Wrkflow\GetValue\Contracts\RuleContract;
1920
use Wrkflow\GetValue\Contracts\TransformerContract;
2021
use Wrkflow\GetValue\Contracts\TransformerStrategy;
@@ -28,6 +29,7 @@
2829
use Wrkflow\GetValue\Rules\NumericRule;
2930
use Wrkflow\GetValue\Rules\StringRule;
3031
use Wrkflow\GetValue\Strategies\DefaultTransformerStrategy;
32+
use Wrkflow\GetValue\Transformers\GetterTransformer;
3133

3234
class GetValue
3335
{
@@ -503,6 +505,27 @@ public function getXMLAttributesGetter(string|array|null $key = null, ?array $tr
503505
return $this->makeInstance(new XMLAttributesData($attributes, $this->data->getKey($parentKey)));
504506
}
505507

508+
/**
509+
* @template T of object
510+
* @param class-string<T> $expectedClass
511+
*
512+
* @return T|null
513+
*/
514+
public function getObject(
515+
string $expectedClass,
516+
string|array $key,
517+
GetValueTransformerContract $getValueTransformer
518+
): ?object {
519+
$transformers = [new GetterTransformer($getValueTransformer)];
520+
$result = $this->getValidatedValue(ValueType::Object, $key, transformers: $transformers);
521+
522+
if ($result instanceof $expectedClass) {
523+
return $result;
524+
}
525+
526+
return null;
527+
}
528+
506529
public function makeInstance(AbstractData $data): self
507530
{
508531
return new self(

tests/AbstractArrayTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,9 @@ protected function setUp(): void
129129
self::KeyEnumInt => 's',
130130
],
131131
self::KeyMissingValue => ['force-non-empty'],
132+
'object' => [
133+
'type' => 'x',
134+
],
132135
]);
133136
$this->data = new GetValue($this->arrayData);
134137
}

tests/Entities/TestEntity.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Entities;
6+
7+
class TestEntity
8+
{
9+
public function __construct(
10+
public readonly string $type,
11+
) {
12+
}
13+
}

tests/GetValueArrayDataTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
use Wrkflow\GetValue\Exceptions\NotSupportedDataException;
1111
use Wrkflow\GetValue\GetValue;
1212
use Wrkflow\GetValue\Strategies\DefaultTransformerStrategy;
13+
use Wrkflow\GetValueTests\Entities\TestEntity;
14+
use Wrkflow\GetValueTests\Transformers\NullTransformer;
15+
use Wrkflow\GetValueTests\Transformers\TestEntityTransformer;
1316

1417
class GetValueArrayDataTest extends AbstractArrayTestCase
1518
{
@@ -131,6 +134,19 @@ public function testGetXMLAttributesGetterOnRootArrayData(): void
131134
$this->data->getXMLAttributesGetter();
132135
}
133136

137+
public function testGetObject(): void
138+
{
139+
$result = $this->data->getObject(TestEntity::class, 'object', new TestEntityTransformer());
140+
$this->assertNotNull($result);
141+
$this->assertEquals('x', $result->type);
142+
}
143+
144+
public function testGetObjectIncorrectResult(): void
145+
{
146+
$result = $this->data->getObject(TestEntity::class, 'object', new NullTransformer());
147+
$this->assertNull($result);
148+
}
149+
134150
protected function assertItem(
135151
GetValue $item,
136152
string $expectedName,

tests/GetValueXMLDataTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
use Wrkflow\GetValue\Exceptions\MissingValueForKeyException;
1212
use Wrkflow\GetValue\GetValue;
1313
use Wrkflow\GetValue\Strategies\DefaultTransformerStrategy;
14+
use Wrkflow\GetValueTests\Entities\TestEntity;
15+
use Wrkflow\GetValueTests\Transformers\NullTransformer;
16+
use Wrkflow\GetValueTests\Transformers\TestEntityTransformer;
1417

1518
class GetValueXMLDataTest extends AbstractXMLTestCase
1619
{
@@ -87,6 +90,19 @@ public function testGetXMLGetter(): void
8790
$this->assertNotNull($this->data->getXMLGetter('not_exists'));
8891
}
8992

93+
public function testGetObject(): void
94+
{
95+
$result = $this->data->getObject(TestEntity::class, 'object', new TestEntityTransformer());
96+
$this->assertNotNull($result);
97+
$this->assertEquals('x', $result->type);
98+
}
99+
100+
public function testGetObjectIncorrectResult(): void
101+
{
102+
$result = $this->data->getObject(TestEntity::class, 'object', new NullTransformer());
103+
$this->assertNull($result);
104+
}
105+
90106
protected function assertObject(GetValue $object): void
91107
{
92108
$this->assertEquals('x', $object->getRequiredString('type'));
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Transformers;
6+
7+
use Wrkflow\GetValue\Contracts\GetValueTransformerContract;
8+
use Wrkflow\GetValue\GetValue;
9+
10+
class NullTransformer implements GetValueTransformerContract
11+
{
12+
public function transform(GetValue $value, string $key): mixed
13+
{
14+
return null;
15+
}
16+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Wrkflow\GetValueTests\Transformers;
6+
7+
use Wrkflow\GetValue\Contracts\GetValueTransformerContract;
8+
use Wrkflow\GetValue\GetValue;
9+
use Wrkflow\GetValueTests\Entities\TestEntity;
10+
11+
class TestEntityTransformer implements GetValueTransformerContract
12+
{
13+
public function transform(GetValue $value, string $key): TestEntity
14+
{
15+
return new TestEntity($value->getRequiredString('type'));
16+
}
17+
}

0 commit comments

Comments
 (0)