Skip to content

Commit f1de35e

Browse files
committed
Added native enum field
1 parent 12f74ec commit f1de35e

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php declare(strict_types = 1);
2+
3+
/**
4+
* ConsistenceEnumField.php
5+
*
6+
* @license More in LICENSE.md
7+
* @copyright https://www.fastybird.com
8+
* @author Adam Kadlec <[email protected]>
9+
* @package FastyBird:JsonApi!
10+
* @subpackage Hydrators
11+
* @since 0.1.0
12+
*
13+
* @date 26.05.20
14+
*/
15+
16+
namespace FastyBird\JsonApi\Hydrators\Fields;
17+
18+
use Consistence;
19+
use IPub\JsonAPIDocument;
20+
use function call_user_func;
21+
use function is_callable;
22+
23+
/**
24+
* Entity consistence enum field
25+
*
26+
* @package FastyBird:JsonApi!
27+
* @subpackage Hydrators
28+
*
29+
* @author Adam Kadlec <[email protected]>
30+
*/
31+
final class ConsistenceEnumField extends Field
32+
{
33+
34+
public function __construct(
35+
private readonly string $typeClass,
36+
private readonly bool $isNullable,
37+
string $mappedName,
38+
string $fieldName,
39+
bool $isRequired,
40+
bool $isWritable,
41+
)
42+
{
43+
parent::__construct($mappedName, $fieldName, $isRequired, $isWritable);
44+
}
45+
46+
/**
47+
* @param JsonAPIDocument\Objects\IStandardObject<string, mixed> $attributes
48+
*/
49+
public function getValue(JsonAPIDocument\Objects\IStandardObject $attributes): Consistence\Enum\Enum|null
50+
{
51+
$value = $attributes->get($this->getMappedName());
52+
53+
$callable = [$this->typeClass, 'get'];
54+
55+
if (is_callable($callable)) {
56+
$result = $value !== null ? call_user_func($callable, $value) : null;
57+
58+
return $result instanceof Consistence\Enum\Enum ? $result : null;
59+
}
60+
61+
return null;
62+
}
63+
64+
public function isNullable(): bool
65+
{
66+
return $this->isNullable;
67+
}
68+
69+
}

src/Hydrators/Fields/EnumField.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@
1010
* @subpackage Hydrators
1111
* @since 0.1.0
1212
*
13-
* @date 26.05.20
13+
* @date 08.02.24
1414
*/
1515

1616
namespace FastyBird\JsonApi\Hydrators\Fields;
1717

18-
use Consistence;
1918
use IPub\JsonAPIDocument;
20-
use function call_user_func;
21-
use function is_callable;
19+
use ReflectionClass;
2220

2321
/**
2422
* Entity consistence enum field
@@ -31,6 +29,9 @@
3129
final class EnumField extends Field
3230
{
3331

32+
/**
33+
* @param class-string $typeClass
34+
*/
3435
public function __construct(
3536
private readonly string $typeClass,
3637
private readonly bool $isNullable,
@@ -46,16 +47,16 @@ public function __construct(
4647
/**
4748
* @param JsonAPIDocument\Objects\IStandardObject<string, mixed> $attributes
4849
*/
49-
public function getValue(JsonAPIDocument\Objects\IStandardObject $attributes): Consistence\Enum\Enum|null
50+
public function getValue(JsonAPIDocument\Objects\IStandardObject $attributes): object|null
5051
{
5152
$value = $attributes->get($this->getMappedName());
5253

53-
$callable = [$this->typeClass, 'get'];
54+
$rc = new ReflectionClass($this->typeClass);
5455

55-
if (is_callable($callable)) {
56-
$result = $value !== null ? call_user_func($callable, $value) : null;
56+
if ($rc->isEnum()) {
57+
$result = $value !== null ? $this->typeClass::from($value) : null;
5758

58-
return $result instanceof Consistence\Enum\Enum ? $result : null;
59+
return $result instanceof $this->typeClass ? $result : null;
5960
}
6061

6162
return null;

src/Hydrators/Hydrator.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ protected function mapEntity(string $entityClassName): array
541541
);
542542

543543
} elseif ($typeRc->isSubclassOf(Consistence\Enum\Enum::class)) {
544-
$fields[] = new Hydrators\Fields\EnumField(
544+
$fields[] = new Hydrators\Fields\ConsistenceEnumField(
545545
$className,
546546
$isNullable,
547547
$mappedKey,
@@ -559,6 +559,16 @@ protected function mapEntity(string $entityClassName): array
559559
$isWritable,
560560
);
561561

562+
} elseif ($typeRc->isEnum()) {
563+
$fields[] = new Hydrators\Fields\EnumField(
564+
$className,
565+
$isNullable,
566+
$mappedKey,
567+
$fieldName,
568+
$isRequired,
569+
$isWritable,
570+
);
571+
562572
} else {
563573
$fields[] = new Hydrators\Fields\SingleEntityField(
564574
$className,

0 commit comments

Comments
 (0)