1. Go to this page and download the library: Download ekvedaras/php-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/ */
ekvedaras / php-enum example snippets
use EKvedaras\PHPEnum\PHPArray\Enum;
class PaymentStatus extends Enum
{
/**
* @return static
*/
final public static function pending(): self
{
return static::get('pending', 'Payment is pending');
}
/**
* @return static
*/
final public static function completed(): self
{
return static::get('completed', 'Payment has been processed');
}
/**
* @return static
*/
final public static function failed(): self
{
return static::get('failed', 'Payment has failed');
}
}
// Retrieving value statically
$status1 = PaymentStatus::completed();
// Retrieving value dynamically from ID
$status2 = PaymentStatus::from('completed');
// Strict comparison is supported
var_dump($status1 === $status2); // true
$status = PaymentStatus::copmleted();
$status->id(); // 'completed'
$status->name(); // 'Payment has been processed'
$status->meta(); // null
use EKvedaras\PHPEnum\PHPArray\Enum;
class PaymentMethod extends Enum
{
final public static function payPal(): self
{
return static::get('paypal', 'PayPal', PayPalHandler::class);
}
final public static function stripe(): self
{
return static::get('stripe', 'Stripe', StripeHandler::class);
}
}
use EKvedaras\PHPEnum\Illuminate\Collection\Enum;
use Illuminate\Support\Collection;
class PaymentMethods extends Enum
{
/**
* @return static
*/
final public static function payPal(): self
{
return static::get('paypal', 'PayPal');
}
/**
* @return static
*/
final public static function stripe(): self
{
return static::get('stripe', 'Stripe');
}
/**
* @return static
*/
final public static function mollie(): self
{
return static::get('mollie', 'Mollie');
}
/**
* Returns only enabled payment methods. Useful for validation or rendering payments UI
* @return Collection|static[]
*/
public static function enabled(): Collection
{
return static::enum()->only(config('payments.enabled'));
}
}
class Payment
{
/** @var string */
private $method;
public function setMethod(PaymentMethod $method)
{
$this->method = $method->id();
}
public function getMethod(): PaymentMethod
{
return PaymentMethod::from($this->method);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.