diff options
author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
---|---|---|
committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php | |
parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php')
-rw-r--r-- | vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php b/vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php new file mode 100644 index 0000000..0ebd978 --- /dev/null +++ b/vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php | |||
@@ -0,0 +1,87 @@ | |||
1 | <?php | ||
2 | |||
3 | declare(strict_types=1); | ||
4 | |||
5 | namespace Doctrine\ORM\Mapping; | ||
6 | |||
7 | use BackedEnum; | ||
8 | use ReflectionProperty; | ||
9 | use ValueError; | ||
10 | |||
11 | use function array_map; | ||
12 | use function is_array; | ||
13 | |||
14 | /** @deprecated use Doctrine\Persistence\Reflection\EnumReflectionProperty instead */ | ||
15 | final class ReflectionEnumProperty extends ReflectionProperty | ||
16 | { | ||
17 | /** @param class-string<BackedEnum> $enumType */ | ||
18 | public function __construct( | ||
19 | private readonly ReflectionProperty $originalReflectionProperty, | ||
20 | private readonly string $enumType, | ||
21 | ) { | ||
22 | parent::__construct( | ||
23 | $originalReflectionProperty->class, | ||
24 | $originalReflectionProperty->name, | ||
25 | ); | ||
26 | } | ||
27 | |||
28 | public function getValue(object|null $object = null): int|string|array|null | ||
29 | { | ||
30 | if ($object === null) { | ||
31 | return null; | ||
32 | } | ||
33 | |||
34 | $enum = $this->originalReflectionProperty->getValue($object); | ||
35 | |||
36 | if ($enum === null) { | ||
37 | return null; | ||
38 | } | ||
39 | |||
40 | if (is_array($enum)) { | ||
41 | return array_map( | ||
42 | static fn (BackedEnum $item): int|string => $item->value, | ||
43 | $enum, | ||
44 | ); | ||
45 | } | ||
46 | |||
47 | return $enum->value; | ||
48 | } | ||
49 | |||
50 | /** | ||
51 | * @param object $object | ||
52 | * @param int|string|int[]|string[]|BackedEnum|BackedEnum[]|null $value | ||
53 | */ | ||
54 | public function setValue(mixed $object, mixed $value = null): void | ||
55 | { | ||
56 | if ($value !== null) { | ||
57 | if (is_array($value)) { | ||
58 | $value = array_map(fn (int|string|BackedEnum $item): BackedEnum => $this->initializeEnumValue($object, $item), $value); | ||
59 | } else { | ||
60 | $value = $this->initializeEnumValue($object, $value); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | $this->originalReflectionProperty->setValue($object, $value); | ||
65 | } | ||
66 | |||
67 | private function initializeEnumValue(object $object, int|string|BackedEnum $value): BackedEnum | ||
68 | { | ||
69 | if ($value instanceof BackedEnum) { | ||
70 | return $value; | ||
71 | } | ||
72 | |||
73 | $enumType = $this->enumType; | ||
74 | |||
75 | try { | ||
76 | return $enumType::from($value); | ||
77 | } catch (ValueError $e) { | ||
78 | throw MappingException::invalidEnumValue( | ||
79 | $object::class, | ||
80 | $this->originalReflectionProperty->name, | ||
81 | (string) $value, | ||
82 | $enumType, | ||
83 | $e, | ||
84 | ); | ||
85 | } | ||
86 | } | ||
87 | } | ||