Skip to content

Commit 38a62b9

Browse files
author
Kirill Nesmeyanov
committed
Add casting of int backed enum to int
1 parent 9104eea commit 38a62b9

File tree

11 files changed

+38
-55
lines changed

11 files changed

+38
-55
lines changed

src/Type/ArrayType/ArrayTypeDenormalizer.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace TypeLang\Mapper\Type\ArrayType;
66

7-
use TypeLang\Mapper\Exception\Definition\TypeNotFoundException;
87
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
9-
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
108
use TypeLang\Mapper\Runtime\Context;
119
use TypeLang\Mapper\Runtime\Path\Entry\ArrayIndexEntry;
1210
use TypeLang\Mapper\Type\ArrayKeyType;
@@ -26,8 +24,6 @@ public function match(mixed $value, Context $context): bool
2624
}
2725

2826
/**
29-
* {@inheritDoc}
30-
*
3127
* @return array<array-key, mixed>
3228
*/
3329
public function cast(mixed $value, Context $context): array

src/Type/ArrayType/ArrayTypeNormalizer.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace TypeLang\Mapper\Type\ArrayType;
66

7-
use TypeLang\Mapper\Exception\Definition\TypeNotFoundException;
87
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
9-
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
108
use TypeLang\Mapper\Runtime\Context;
119
use TypeLang\Mapper\Runtime\Path\Entry\ArrayIndexEntry;
1210
use TypeLang\Mapper\Type\ArrayKeyType;
@@ -26,8 +24,6 @@ public function match(mixed $value, Context $context): bool
2624
}
2725

2826
/**
29-
* {@inheritDoc}
30-
*
3127
* @return array<array-key, mixed>
3228
*/
3329
public function cast(mixed $value, Context $context): array

src/Type/BackedEnumType/BackedEnumTypeDenormalizer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace TypeLang\Mapper\Type\BackedEnumType;
66

77
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
8-
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
98
use TypeLang\Mapper\Runtime\Context;
109
use TypeLang\Mapper\Type\TypeInterface;
1110

src/Type/ClassType/ClassTypeDenormalizer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use TypeLang\Mapper\Exception\Mapping\FieldExceptionInterface;
88
use TypeLang\Mapper\Exception\Mapping\InvalidFieldTypeValueException;
9-
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
109
use TypeLang\Mapper\Exception\Mapping\InvalidValueMappingException;
1110
use TypeLang\Mapper\Exception\Mapping\MappingExceptionInterface;
1211
use TypeLang\Mapper\Exception\Mapping\MissingFieldTypeException;
@@ -39,8 +38,6 @@ public function match(mixed $value, Context $context): bool
3938
}
4039

4140
/**
42-
* {@inheritDoc}
43-
*
4441
* @return T
4542
*/
4643
public function cast(mixed $value, Context $context): object

src/Type/ClassType/ClassTypeNormalizer.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use TypeLang\Mapper\Exception\Mapping\FieldExceptionInterface;
88
use TypeLang\Mapper\Exception\Mapping\InvalidFieldTypeValueException;
9-
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
109
use TypeLang\Mapper\Exception\Mapping\InvalidValueMappingException;
1110
use TypeLang\Mapper\Exception\Mapping\MappingExceptionInterface;
1211
use TypeLang\Mapper\Exception\Mapping\MissingFieldTypeException;
@@ -38,8 +37,6 @@ public function match(mixed $value, Context $context): bool
3837
}
3938

4039
/**
41-
* {@inheritDoc}
42-
*
4340
* @return object|array<non-empty-string, mixed>
4441
*/
4542
public function cast(mixed $value, Context $context): object|array

src/Type/IntType.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,16 @@ public function cast(mixed $value, Context $context): int
5050
*/
5151
protected function convertToInt(mixed $value, Context $context): int
5252
{
53+
if ($value instanceof \BackedEnum && \is_int($value->value)) {
54+
$value = $value->value;
55+
}
56+
5357
return match (true) {
5458
\is_int($value) => $value,
5559
$value === false,
5660
$value === null => 0,
5761
$value === true => 1,
5862
// Check that the conversion to int does not lose precision.
59-
// 1)
6063
\is_numeric($value) && (float) (int) $value === (float) $value => (int) $value,
6164
default => throw InvalidValueException::createFromContext(
6265
value: $value,

src/Type/NullableType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace TypeLang\Mapper\Type;
66

7-
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
87
use TypeLang\Mapper\Runtime\Context;
98

109
class NullableType implements TypeInterface

src/Type/ObjectType/ObjectTypeNormalizer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ public function match(mixed $value, Context $context): bool
1616
}
1717

1818
/**
19-
* {@inheritDoc}
20-
*
2119
* @return array<array-key, mixed>|object
2220
*/
2321
public function cast(mixed $value, Context $context): array|object

src/Type/UnionType.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace TypeLang\Mapper\Type;
66

77
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
8-
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
98
use TypeLang\Mapper\Runtime\Context;
109
use TypeLang\Mapper\Runtime\Path\Entry\UnionLeafEntry;
1110

src/Type/UnitEnumType/UnitEnumTypeDenormalizer.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace TypeLang\Mapper\Type\UnitEnumType;
66

77
use TypeLang\Mapper\Exception\Mapping\InvalidValueException;
8-
use TypeLang\Mapper\Exception\Mapping\RuntimeExceptionInterface;
98
use TypeLang\Mapper\Runtime\Context;
109
use TypeLang\Mapper\Type\TypeInterface;
1110

0 commit comments

Comments
 (0)