summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php')
-rw-r--r--vendor/doctrine/orm/src/Mapping/ReflectionEnumProperty.php87
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
3declare(strict_types=1);
4
5namespace Doctrine\ORM\Mapping;
6
7use BackedEnum;
8use ReflectionProperty;
9use ValueError;
10
11use function array_map;
12use function is_array;
13
14/** @deprecated use Doctrine\Persistence\Reflection\EnumReflectionProperty instead */
15final 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}