1. Go to this page and download the library: Download leocello/sweet-enum library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
leocello / sweet-enum example snippets
use Leocello\SweetEnum\SweetCase;
use Leocello\SweetEnum\SweetEnum;
use Leocello\SweetEnum\SweetEnumContract;
enum Status: string implements SweetEnumContract
{
use SweetEnum;
#[SweetCase(
title: 'Active',
color: 'green',
)
case Active = 'active';
#[SweetCase(
title: 'Inactive',
color: 'red',
)
case Inactive = 'inactive';
}
echo Status::Active->title(); // Will print: Active
echo Status::Inactive->color(); // Will print: red
Status::Active->hi(); // Will return `null`;
Status::Active->hi('Hello'); // Will return `"Hello"`;
Status::Active->hi(default: 'Hello!'); // Will return `"Hello!"`;
Status::Active->hi(strict: true); // Wil throw an exception
public const DEFAULT = self::Active;
$status = Status::Active;
...
if ($status->isA(Status::class)) {
// Do something
}
if ($status->isA(Color::class)) {
// Not gonna happen
}
$status = Status::Active;
...
if ($status->is(Status::Inactive)) {
// Not gonna happen
}
if ($status->is(Color::White)) {
// Also not gonna happen as it already threw an exception :(
}
if ($status->is(Status::Active)) {
// Do something
}
$info = Color::getCasesInfo(SweetFields::Original);
// The variable $info will be an array where each value will be
// an associative array with the keys `value` and `name`
...
#[SweetCase(
color: 'green',
)
case Active = 'active';
...
// Accessing the value of color:
echo Status::Active->color();
// As you can access your computed value as the other
// "property" methods, then it doesn't need to be public
protected function getRgb(): array
{
return sscanf($this->hex(), '#%02x%02x%02x');
}
protected function getComputedFields(): array
{
return [
'rgb' => $this->getRgb(),
];
}
// This is part of the implementation of enum Animal
...
#[SweetCase(
caseClass: AnimalCatCaseClass::class,
title: 'Cat',
)]
case Cat = 'cat';
// Implementation of class AnimalCatCaseClass
class AnimalCatCaseClass extends SweetCaseClass
{
public function meow(): string
{
return 'Meow!';
}
}
// Usage
echo Animal::Cat->meow(); // will print "Meow!"
// This is part of the implementation of enum Animal
...
public const DEFAULT_CASE_CLASS = AnimalCaseClass::class;
#[SweetCase(
caseClass: AnimalCatCaseClass::class,
title: 'Cat',
)]
case Cat = 'cat';
#[SweetCase(
title: 'Sheep',
)]
case Sheep = 'sheep';
...
// Implementation of class AnimalCaseClass
class AnimalCaseClass extends SweetCaseClass
{
public function meow(): string
{
return 'Sorry, a '.strtolower($this->case->title()).' cannot meow';
}
}
// Implementation of class AnimalCatCaseClass
class AnimalCatCaseClass extends AnimalCaseClass
{
public function meow(): string
{
return 'Meow!';
}
}
// Usage
echo Animal::Cat->meow(); // will print "Meow!"
echo Animal::Sheep->meow(); // will print "Sorry, a sheep cannot meow"
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.