r/PHPhelp • u/GuybrushThreepywood • Jan 16 '25
How can I run an Enum declared by a variable/string?
I have created some enums which follow the names of the tables in my database.
In these enums, I have some validation logic that runs. E,g
enum table_maps: string {
use DbTraits;
case HEIGHT = 'height';
case WIDTH = 'width';
}
enum table_settings: string {
use DbTraits;
case DEFAULT_WAREHOUSE = 'default_warehouse'
case DEFAULT_REPORT_TYPE = 'default_report_type'
protected function friendlyName(): string {
return match ( $this ) {
self::HEIGHT => 'map height',
self::WIDTH => 'map width',
};
}
protected function validations(): Closure {
return match ( $this ) {
self::HEIGHT => function( string $value ) {
<some validation code>
},
self::WIDTH => function( string $value ) {
<some validation code>
}
}
}
}I have created a Value Object so I can deal with columns throughout my application:
class ColVo {
public string $friendlyName;
public function __construct(
public string $name,
public $colEnum,
) {
$this->friendlyName = $this->colEnum->getFriendlyName();
}
public function validateValue( string|int|null ) {
$validateFn = $this->validations();
$isValid = $validateFn( $value );
if( $isValid !== true ) {
throw new ClientResponse('Invalid value provided for ' . $this->friendlyName)
}
}
}I am instantiating the ColVo like:
$colHeight = new ColVo( 'height', table_maps::handledTryFrom( 'height'));My question is how can I set the type for the enum so that my ide can help?
i.e the 2nd promoted property in my ColVo:
public $colEnum
I would like it to look like:
public table_maps $colEnum
But this property, the enum, will change. This time it is a table_maps enum, but another time it might be the table_settings enum.
I feel like I might need to implement a 'parent' databaseColEnum, and use the type of that but I haven't yet learned how to do it.
4
u/martinbean Jan 16 '25
I don’t really understand what problem it is you’re trying to solve here? Enums are for defining possible values for something; not for holding logic.
2
u/colshrapnel Jan 17 '25
Ask yourself a question, why it's enums and not just regular classes.
4
u/MateusAzevedo Jan 17 '25
From one of your comments:
Then use an interface.
3
u/alesinicio Jan 16 '25
If its just those two enum, you can use something like:
But your IDE won't be able to do any miracles, since it cannot possibly know which one it will be before runtime.
3
u/erythro Jan 17 '25
use an interface I guess, enums support that
1
u/eurosat7 Jan 17 '25 edited Jan 17 '25
Your friedlyName() is something that could be solved with a translation. And a trait to get db stuff in there is code smell. If you need more use a class. Better keep enums simple.
1
u/yourteam Jan 17 '25
No wait. Enums are for lists of values. If you want logic make a class, then you can use an interface to "group" the classes of the tables.
Everything seems a bit of a mess here