Class EnumType
Enum type converter.
Use to convert string data between PHP and the database types.
Property Summary
-
$_name protected
string|null
Identifier name for this type
-
$backingType protected
string
The type of the enum which is either string or int
-
$enumClassName protected
class-stringBackedEnum>
The enum classname which is associated to the type instance
Method Summary
-
__construct() public
-
from() public static
Create an
EnumType
that is paired with the provided$enumClassName
. -
getBaseType() public
Returns the base type name that this class is inheriting.
-
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.
-
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-stringBackedEnum> $enumClassName)
Parameters
-
string
$name The name identifying this type
-
class-stringBackedEnum>
$enumClassName The associated enum class name
from() ¶ public static
from(class-stringBackedEnum> $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-stringBackedEnum>
$enumClassName The enum class name
Returns
string
getBaseType() ¶ 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|null
getEnumClassName() ¶ public
getEnumClassName(): class-stringBackedEnum>
Returns
class-stringBackedEnum>
getName() ¶ public
getName(): string|null
Returns type identifier name for this object.
Returns
string|null
marshal() ¶ public
marshal(mixed $value): BackedEnum|null
Marshals request data
Most useful for converting request data into PHP objects, that make sense for the rest of the ORM/Database layers.
Parameters
-
mixed
$value The value to convert.
Returns
BackedEnum|null
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
mixed
toDatabase() ¶ 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|null
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|null
toStatement() ¶ 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