Skip to content

Instantly share code, notes, and snippets.

@andybeak
Created June 10, 2020 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andybeak/e92e89266f92762040f6fbd6bf11083b to your computer and use it in GitHub Desktop.
Save andybeak/e92e89266f92762040f6fbd6bf11083b to your computer and use it in GitHub Desktop.
Enum
<?php
declare(strict_types=1);
namespace App\Common\Enum;
use ReflectionException;
use ReflectionClass;
use InvalidArgumentException;
abstract class BaseEnum
{
protected $value;
/**
* BaseEnum constructor.
* @param $value
* @throws ReflectionException
*/
final public function __construct($value)
{
$c = new ReflectionClass($this);
if (!in_array($value, $c->getConstants())) {
throw new InvalidArgumentException("Invalid enum value");
}
$this->value = $value;
}
/**
* @return mixed
*/
final public function __toString()
{
return $this->value;
}
}
<?php
$example = new EntityTypeEnum(EntityTypeEnum::INVESTOR);
<?php
namespace App\Common\Enum;
class EntityTypeEnum extends BaseEnum
{
// entity type mapping is shared between projects
const INVESTOR = 0;
const FUND = 1;
const MANAGER = 2;
const CONSULTANT = 3;
const MANDATE = 4;
const INVESTMENT = 5;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment