-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ci: load database migrations when running `phpstan` * feat: add handler for `BackedEnum` type resolves #33 * Fix styling [skip-ci] * style: add type for `HasFactory` trait * Fix styling [skip-ci] * fix: skip if `ReflectionEnum` does not exist * test: run enum tests only for php 8.1+ * Fix styling [skip-ci] --------- Co-authored-by: marijoo <[email protected]>
- Loading branch information
Showing
17 changed files
with
315 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace Kolossal\Multiplex\DataType; | ||
|
||
use BackedEnum; | ||
use Exception; | ||
use ReflectionEnum; | ||
|
||
/** | ||
* Handle serialization of backed enums. | ||
*/ | ||
class EnumHandler implements HandlerInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDataType(): string | ||
{ | ||
return 'enum'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function canHandleValue($value): bool | ||
{ | ||
return $value instanceof BackedEnum && class_exists(ReflectionEnum::class); | ||
} | ||
|
||
/** | ||
* Convert the value to a string, so that it can be stored in the database. | ||
* | ||
* @param BackedEnum $enum | ||
*/ | ||
public function serializeValue($enum): string | ||
{ | ||
if (!$this->canHandleValue($enum)) { | ||
return ''; | ||
} | ||
|
||
return get_class($enum) . '::' . $enum->value; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function unserializeValue(?string $value): mixed | ||
{ | ||
// @codeCoverageIgnoreStart | ||
if (!class_exists(ReflectionEnum::class)) { | ||
throw new Exception('Cannot unserialize enum value since \ReflectionEnum is not available. This will only work in PHP >= 8.1.'); | ||
} | ||
// @codeCoverageIgnoreEnd | ||
|
||
if (is_null($value)) { | ||
return $value; | ||
} | ||
|
||
if (strpos($value, '::') === false) { | ||
return null; | ||
} | ||
|
||
[$class, $value] = explode('::', $value, 2); | ||
|
||
if (!enum_exists($class)) { | ||
return null; | ||
} | ||
|
||
if (!(new ReflectionEnum($class))->isBacked()) { | ||
return null; | ||
} | ||
|
||
/** | ||
* @var \BackedEnum $class | ||
*/ | ||
return $class::tryFrom($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.