-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added methods to extract unions (ie oneOf properties)
- Loading branch information
matt
committed
Nov 24, 2023
1 parent
df3a978
commit f61c3b4
Showing
5 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KynxTest\Mezzio\OpenApi\Hydrator\Asset; | ||
|
||
use Kynx\Mezzio\OpenApi\Hydrator\HydratorInterface; | ||
|
||
use function assert; | ||
use function is_array; | ||
|
||
final class ExtractableHydrator implements HydratorInterface | ||
{ | ||
public static function hydrate(mixed $data): object | ||
{ | ||
assert(is_array($data)); | ||
return new Extractable((string) $data['one'], (int) $data['two'], (bool) $data['three']); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function extract(mixed $object): array | ||
{ | ||
assert($object instanceof Extractable); | ||
return [ | ||
'one' => $object->getOne(), | ||
'two' => $object->getTwo(), | ||
'three' => $object->isThree(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KynxTest\Mezzio\OpenApi\Hydrator\Asset; | ||
|
||
final class SecondExtractable | ||
{ | ||
public function __construct(private readonly string $value) | ||
{ | ||
} | ||
|
||
public function getValue(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KynxTest\Mezzio\OpenApi\Hydrator\Asset; | ||
|
||
use Kynx\Mezzio\OpenApi\Hydrator\HydratorInterface; | ||
|
||
use function assert; | ||
use function is_string; | ||
|
||
final class SecondExtractableHydrator implements HydratorInterface | ||
{ | ||
public static function hydrate(mixed $data): object | ||
{ | ||
assert(is_string($data)); | ||
return new SecondExtractable($data); | ||
} | ||
|
||
public static function extract(mixed $object): bool|array|float|int|string|null | ||
{ | ||
assert($object instanceof SecondExtractable); | ||
return [ | ||
'value' => $object->getValue(), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters