Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2136 from tarlepp/feat/enums-take-two
Feat - Use `Language` enum instead of class constant
- Loading branch information
Showing
20 changed files
with
458 additions
and
59 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,102 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* /src/Doctrine/DBAL/Types/RealEnumType.php | ||
* | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
|
||
namespace App\Doctrine\DBAL\Types; | ||
|
||
use App\Enum\Interfaces\DatabaseEnumInterface; | ||
use BackedEnum; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\ConversionException; | ||
use Doctrine\DBAL\Types\Type; | ||
use InvalidArgumentException; | ||
use function array_map; | ||
use function gettype; | ||
use function implode; | ||
use function in_array; | ||
use function is_string; | ||
|
||
/** | ||
* Class RealEnumType | ||
* | ||
* @package App\Doctrine\DBAL\Types | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
abstract class RealEnumType extends Type | ||
{ | ||
protected static string $name; | ||
|
||
/** | ||
* @psalm-var class-string<DatabaseEnumInterface&BackedEnum> | ||
*/ | ||
protected static string $enum; | ||
|
||
/** | ||
* @return array<int, string> | ||
*/ | ||
public static function getValues(): array | ||
{ | ||
return static::$enum::getValues(); | ||
} | ||
|
||
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string | ||
{ | ||
$enumDefinition = implode( | ||
', ', | ||
array_map(static fn (string $value): string => "'" . $value . "'", static::getValues()), | ||
); | ||
|
||
return 'ENUM(' . $enumDefinition . ')'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function convertToDatabaseValue($value, AbstractPlatform $platform): string | ||
{ | ||
if (!in_array($value, static::$enum::cases(), true)) { | ||
$message = sprintf( | ||
"Invalid '%s' value '%s'", | ||
static::$name, | ||
is_string($value) ? $value : gettype($value), | ||
); | ||
|
||
throw new InvalidArgumentException($message); | ||
} | ||
|
||
return (string)parent::convertToDatabaseValue($value->value, $platform); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function convertToPHPValue($value, AbstractPlatform $platform): DatabaseEnumInterface | ||
{ | ||
$value = (string)parent::convertToPHPValue($value, $platform); | ||
$enum = static::$enum::tryFrom($value); | ||
|
||
if ($enum !== null) { | ||
return $enum; | ||
} | ||
|
||
throw ConversionException::conversionFailedFormat( | ||
gettype($value), | ||
static::$name, | ||
'One of: "' . implode('", "', static::getValues()) . '"', | ||
); | ||
} | ||
|
||
/** | ||
* Parent method is deprecated, so remove this after it has been removed. | ||
* | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getName(): string | ||
{ | ||
return ''; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* /src/Enum/Interfaces/StringEnumInterface.php | ||
* | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
|
||
namespace App\Enum\Interfaces; | ||
|
||
/** | ||
* Enum StringEnumInterface | ||
* | ||
* @package App\Enum\Interfaces | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
interface DatabaseEnumInterface extends StringEnumInterface | ||
{ | ||
} |
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,25 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* /src/Enum/Interfaces/StringEnumInterface.php | ||
* | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
|
||
namespace App\Enum\Interfaces; | ||
|
||
use BackedEnum; | ||
|
||
/** | ||
* Enum StringEnumInterface | ||
* | ||
* @package App\Enum\Interfaces | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
interface StringEnumInterface extends BackedEnum | ||
{ | ||
/** | ||
* @return array<int, string> | ||
*/ | ||
public static function getValues(): array; | ||
} |
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,31 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* /src/Enum/Language.php | ||
* | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
|
||
namespace App\Enum; | ||
|
||
use App\Enum\Interfaces\DatabaseEnumInterface; | ||
use App\Enum\Traits\GetValues; | ||
|
||
/** | ||
* Language Role | ||
* | ||
* @package App\Entity | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
enum Language: string implements DatabaseEnumInterface | ||
{ | ||
use GetValues; | ||
|
||
case EN = 'en'; | ||
case FI = 'fi'; | ||
|
||
public static function getDefault(): self | ||
{ | ||
return self::EN; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
/** | ||
* /src/Enum/Traits/GetValues.php | ||
* | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
|
||
namespace App\Enum\Traits; | ||
|
||
use function array_column; | ||
|
||
/** | ||
* Trait GetValues | ||
* | ||
* @package App\Enum\Traits | ||
* @author TLe, Tarmo Leppänen <tarmo.leppanen@pinja.com> | ||
*/ | ||
trait GetValues | ||
{ | ||
/** | ||
* @return array<int, string> | ||
*/ | ||
public static function getValues(): array | ||
{ | ||
return array_column(self::cases(), '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
Oops, something went wrong.