11<?php
22
3+ /**
4+ * Implements conversion logic for PHP 8.1+ enumeration types to and from database representations.
5+ * This abstract class defines the functionality to serialize enum instances for storage in a database
6+ * and deserialize stored values back into enum instances. It ensures that only instances of the specified
7+ * enum class are handled, throwing exceptions if conversion fails due to type mismatches or unrecognized values.
8+ *
9+ * @package CommonPHP
10+ * @subpackage DatabaseEngine
11+ * @author Timothy McClatchey <timothy@commonphp.org>
12+ * @copyright 2024 CommonPHP.org
13+ * @license http://opensource.org/licenses/MIT MIT License
14+ * @noinspection PhpUnused
15+ * @noinspection PhpMixedReturnTypeCanBeReducedInspection
16+ */
17+
318namespace CommonPHP \DatabaseEngine \Contracts ;
419
520use BackedEnum ;
621use CommonPHP \DatabaseEngine \Exceptions \EnumDoesNotExistException ;
722use CommonPHP \DatabaseEngine \Exceptions \TypeDecodingFailedException ;
823use CommonPHP \DatabaseEngine \Exceptions \TypeEncodingFailedException ;
924use CommonPHP \DatabaseEngine \Support \TypeConversionProvider ;
25+ use Override ;
1026use UnitEnum ;
1127
1228class AbstractEnumTypeConverter implements TypeConverterContract
1329{
14- /** @var string|UnitEnum|BackedEnum */
30+ /**
31+ * The fully qualified class name of the enum this converter handles.
32+ * It must be a valid enumeration class, either a UnitEnum or BackedEnum.
33+ *
34+ * @var string|UnitEnum|BackedEnum
35+ */
1536 private string |UnitEnum |BackedEnum $ enumClass ;
1637
38+ /**
39+ * Initializes a new instance of the AbstractEnumTypeConverter for a specific enum class.
40+ * Verifies the existence of the enum class, throwing an exception if the class does not exist.
41+ *
42+ * @param string $enumClass The fully qualified class name of the enum to be handled.
43+ * @throws EnumDoesNotExistException If the specified enum class does not exist.
44+ */
1745 public function __construct (string $ enumClass )
1846 {
1947 if (!enum_exists ($ enumClass ))
@@ -23,7 +51,16 @@ public function __construct(string $enumClass)
2351 $ this ->enumClass = $ enumClass ;
2452 }
2553
26- #[\Override] final function encode (mixed $ data ): mixed
54+ /**
55+ * Encodes an enum instance into a database-storable representation.
56+ * Currently, this method converts backed enums to their scalar values, ensuring the data is
57+ * compatible for database storage. It validates that the provided data matches the expected enum type.
58+ *
59+ * @param mixed $data The enum instance to encode.
60+ * @return mixed The encoded value suitable for database storage.
61+ * @throws TypeEncodingFailedException If encoding fails due to a type mismatch.
62+ */
63+ #[Override] final function encode (mixed $ data ): mixed
2764 {
2865 $ typeName = TypeConversionProvider::getTypeOf ($ data );
2966 if ($ typeName !== $ this ->enumClass )
@@ -33,7 +70,15 @@ public function __construct(string $enumClass)
3370 return $ data ->name ;
3471 }
3572
36- #[\Override] final function decode (mixed $ data ): mixed
73+ /**
74+ * Decodes a database-stored value back into an enum instance.
75+ * It iterates through the enum cases to find a match for the stored value, returning the corresponding enum case.
76+ *
77+ * @param mixed $data The database-stored value to decode.
78+ * @return mixed The corresponding enum instance, if a match is found.
79+ * @throws TypeDecodingFailedException If decoding fails because the stored value does not match any enum case.
80+ */
81+ #[Override] final function decode (mixed $ data ): mixed
3782 {
3883 foreach (($ this ->enumClass )::cases () as $ case )
3984 {
0 commit comments