Class EnumType
Enum type converter.
Use to convert string data between PHP and the database types.
Property Summary
-
$backingType protected
stringThe type of the enum which is either string or int
Method Summary
-
__construct() public
-
from() public static
Create an
EnumTypethat is paired with the provided$enumClassName.Usage
// In a table class $this->getSchema()->setColumnType('status', EnumType::from(StatusEnum::class)); -
getBaseType() public
Returns the base type name that this class is inheriting.
This is useful when extending base type for adding extra functionality, but still want the rest of the framework to use the same assumptions it would do about the base type it inherits from.
-
getEnumClassName() public
-
getName() public
Returns type identifier name for this object.
-
marshal() public
Marshals request data
-
newId() public
Generate a new primary key value for a given type.
This method can be used by types to create new primary key values when entities are inserted.
-
toDatabase() public
Convert enum instances into the database format.
-
toPHP() public
Transform DB value to backed enum instance
-
toStatement() public
Get the binding type to use in a PDO statement.
Method Detail
__construct() ¶ public
__construct(string $name, class-string<BackedEnum> $enumClassName)
Parameters
-
string$name The name identifying this type
-
class-string<BackedEnum>$enumClassName The associated enum class name
from() ¶ public static
from(class-string<BackedEnum> $enumClassName): string
Create an EnumType that is paired with the provided $enumClassName.
Usage
// In a table class
$this->getSchema()->setColumnType('status', EnumType::from(StatusEnum::class));
Parameters
-
class-string<BackedEnum>$enumClassName The enum class name
Returns
stringgetBaseType() ¶ public
getBaseType(): string|null
Returns the base type name that this class is inheriting.
This is useful when extending base type for adding extra functionality, but still want the rest of the framework to use the same assumptions it would do about the base type it inherits from.
Returns
string|nullgetEnumClassName() ¶ public
getEnumClassName(): class-string<BackedEnum>
Returns
class-string<BackedEnum>getName() ¶ public
getName(): string|null
Returns type identifier name for this object.
Returns
string|nullmarshal() ¶ public
marshal(mixed $value): BackedEnum|null
Marshals request data
Parameters
-
mixed$value The value to convert.
Returns
BackedEnum|nullConverted value.
newId() ¶ public
newId(): mixed
Generate a new primary key value for a given type.
This method can be used by types to create new primary key values when entities are inserted.
Returns
mixedtoDatabase() ¶ public
toDatabase(mixed $value, Cake\Database\Driver $driver): string|int|null
Convert enum instances into the database format.
Parameters
-
mixed$value The value to convert.
-
Cake\Database\Driver$driver The driver instance to convert with.
Returns
string|int|nullThrows
InvalidArgumentExceptionWhen the given value is not a valid value for the associated enum
toPHP() ¶ public
toPHP(mixed $value, Cake\Database\Driver $driver): BackedEnum|null
Transform DB value to backed enum instance
Parameters
-
mixed$value The value to convert.
-
Cake\Database\Driver$driver The driver instance to convert with.
Returns
BackedEnum|nulltoStatement() ¶ public
toStatement(mixed $value, Cake\Database\Driver $driver): int
Get the binding type to use in a PDO statement.
Parameters
-
mixed$value -
Cake\Database\Driver$driver
Returns
int