Created
June 10, 2020 09:35
-
-
Save andybeak/918899d3f0ea45fb6ac37c16bfea4a5e to your computer and use it in GitHub Desktop.
Enum
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$example = new EntityTypeEnum(EntityTypeEnum::INVESTOR); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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