PHP code example of ekvedaras / php-enum

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

var_dump(PaymentStatus::enum());
/*
array(3) {
  'pending' =>
  class PaymentStatus#12 (3) {
    protected $id =>
    string(7) "pending"
    protected $name =>
    string(18) "Payment is pending"
    protected $meta =>
    NULL
  }
  'completed' =>
  class PaymentStatus#363 (3) {
    protected $id =>
    string(9) "completed"
    protected $name =>
    string(26) "Payment has been processed"
    protected $meta =>
    NULL
  }
  'failed' =>
  class PaymentStatus#13 (3) {
    protected $id =>
    string(6) "failed"
    protected $name =>
    string(18) "Payment has failed"
    protected $meta =>
    NULL
  }
}
 */

var_dump(PaymentStatus::options());
/*
array(3) {
  'pending' =>
  string(18) "Payment is pending"
  'completed' =>
  string(26) "Payment has been processed"
  'failed' =>
  string(18) "Payment has failed"
}
*/

var_dump(PaymentStatus::keys());
/*
array(3) {
  [0] =>
  string(7) "pending"
  [1] =>
  string(9) "completed"
  [2] =>
  string(6) "failed"
}
*/

var_dump(PaymentStatus::json()); // Will 

var_dump(PaymentStatus::jsonOptions());

var_dump(PaymentStatus::enum());
/*
class Illuminate\Support\Collection#362 (1) {
  protected $items =>
  array(3) {
    'pending' =>
    class PaymentStatus#12 (3) {
      protected $id =>
      string(7) "pending"
      protected $name =>
      string(18) "Payment is pending"
      protected $meta =>
      NULL
    }
    'completed' =>
    class PaymentStatus#363 (3) {
      protected $id =>
      string(9) "completed"
      protected $name =>
      string(26) "Payment has been processed"
      protected $meta =>
      NULL
    }
    'failed' =>
    class PaymentStatus#13 (3) {
      protected $id =>
      string(6) "failed"
      protected $name =>
      string(18) "Payment has failed"
      protected $meta =>
      NULL
    }
  }
}
 */

var_dump(PaymentStatus::options());
/*
class Illuminate\Support\Collection#368 (1) {
  protected $items =>
  array(3) {
    'pending' =>
    string(18) "Payment is pending"
    'completed' =>
    string(26) "Payment has been processed"
    'failed' =>
    string(18) "Payment has failed"
  }
}
*/

var_dump(PaymentStatus::keys());
/*
class Illuminate\Support\Collection#13 (1) {
  protected $items =>
  array(3) {
    [0] =>
    string(7) "pending"
    [1] =>
    string(9) "completed"
    [2] =>
    string(6) "failed"
  }
}
*/

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);
    }
}

$method = PaymentMethod::from($request['method_id']);

$handler = app($method->meta());

$method = PaymentMethod::fromMeta(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);
    }
}