1. Go to this page and download the library: Download jomisacu/enumerations 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/ */
jomisacu / enumerations example snippets
final class PakageState extends Jomisacu\Enumerations\Enumeration
{
public const SENT = 'sent';
public const IN_WAY = 'in-way';
public const RECEIVED = 'received';
}
// retrieving from raw values
$state = PackageState::fromValue($valueFromRequest); // throws an exception if invalid value
// TypeHint
class Package
{
private PackageState $state;
public function changePackageState(PackageState $state)
{
$this->state = $state;
}
public function getPackageState()
{
return $this->state;
}
}
// printing the value
$package = new Package;
$state = PackageState::fromValue($valueFromRequest); // throws an exception if invalid value
$package->changePackageState($state);
echo $package->getPackageState(); // prints sent, in-way or received
class Career extends Jomisacu\Enumerations\Enumeration
{
// WTF??? What is this?
// using our own value we drop the dependency with external sources
// but below we will see how to deal with these values
// the values in the database are the values that we decided in the class constants
public const SOFTWARE_ENGINEER = "a372d961-22d9-4cc4-a9ee-4cb47a15b26d";
public const ARCHITECT = "6d8165dc-621d-4279-bc71-4e4f4782d972";
// we always can get the current code
// if external code changes we only need to update the code here
// the values in the database are the values that we decided in the class constants
public function getCode()
{
$codes = [
self::SOFTWARE_ENGINEER => '0XYZ',
self::ARCHITECT => '0YYK',
];
return $codes[(string) $this->getValue()];
}
// now, we can store a reference to the previous code, so we can interop with old formats
public function getOldCode()
{
$codes = [
self::SOFTWARE_ENGINEER => 'XYZ',
self::ARCHITECT => 'YYK',
];
return $codes[(string) $this->getValue()];
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.