You are viewing the documentation for an older version of this package. You can check the version you are using with the following command:
composer show spatie/enum
# # Basic Enum
The most basic setup to define an enum by using php-doc annotations.
use Spatie\Enum\Enum ;
final class WeekDayEnum extends Enum
{
}
# # Custom value/index Enum
This enum will be used in following chapters.
# # by Constants
This is the easiest way to change the value/index by it's enum name. The key of both arrays is the name of the method it's accessed by and the value MUST be a string
for the value map and an int
for the index map.
use Spatie\Enum\Enum ;
final class WeekDayEnum extends Enum
{
const MAP_INDEX = [
'monday ' => 1,
'tuesday ' => 2,
'wednesday ' => 3,
'thursday ' => 4,
'friday ' => 5,
'saturday ' => 6,
'sunday ' => 7,
];
const MAP_VALUE = [
'monday ' => 'Montag ',
'tuesday ' => 'Dienstag ',
'wednesday ' => 'Mittwoch ',
'thursday ' => 'Donnerstag ',
'friday ' => 'Freitag ',
'saturday ' => 'Samstag ',
'sunday ' => 'Sonntag ',
];
}
# # by Methods
This approach offers much more flexibility because you can use the return value of a function for the value/index resolving. Keep in mind that these methods are called once for the enum cache (used to make an enum) but called everytime you access the value/index via the getters.
So if you use, for example, time()
for the index you can only make the enum by the timestamp the enum was first cached but you will get the current timestamp if you call getIndex()
.
use Spatie\Enum\Enum ;
abstract class WeekDayEnum extends Enum
{
public static function monday (): WeekDayEnum
{
return new class () extends WeekDayEnum {
public function getIndex (): int
{
return 1;
}
public function getValue (): string
{
return 'Montag ';
}
};
}
public static function tuesday (): WeekDayEnum
{
return new class () extends WeekDayEnum {
public function getIndex (): int
{
return 2;
}
public function getValue (): string
{
return 'Dienstag ';
}
};
}
public static function wednesday (): WeekDayEnum
{
return new class () extends WeekDayEnum {
public function getIndex (): int
{
return 3;
}
public function getValue (): string
{
return 'Mittwoch ';
}
};
}
public static function thursday (): WeekDayEnum
{
return new class () extends WeekDayEnum {
public function getIndex (): int
{
return 4;
}
public function getValue (): string
{
return 'Donnerstag ';
}
};
}
public static function friday (): WeekDayEnum
{
return new class () extends WeekDayEnum {
public function getIndex (): int
{
return 5;
}
public function getValue (): string
{
return 'Freitag ';
}
};
}
public static function saturday (): WeekDayEnum
{
return new class () extends WeekDayEnum {
public function getIndex (): int
{
return 6;
}
public function getValue (): string
{
return 'Samstag ';
}
};
}
public static function sunday (): WeekDayEnum
{
return new class () extends WeekDayEnum {
public function getIndex (): int
{
return 7;
}
public function getValue (): string
{
return 'Sonntag ';
}
};
}
}