Enum.php
46 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\Support\ValueObjects; |
| 4 | |
| 5 | use BadMethodCallException; |
| 6 | use Give\Framework\Support\Facades\Str; |
| 7 | |
| 8 | /** |
| 9 | * @method public getKeyAsCamelCase() |
| 10 | */ |
| 11 | class Enum extends \MyCLabs\Enum\Enum |
| 12 | { |
| 13 | public function __call($name, $arguments) |
| 14 | { |
| 15 | if (strpos($name, 'is') === 0) { |
| 16 | $constant = Str::upper(Str::snake(Str::after($name, 'is'))); |
| 17 | |
| 18 | if ( ! self::hasConstant($constant)) { |
| 19 | throw new BadMethodCallException("$name does not match a corresponding enum constant."); |
| 20 | } |
| 21 | |
| 22 | return $this->equals(parent::$constant()); |
| 23 | } |
| 24 | |
| 25 | throw new BadMethodCallException("Method $name does not exist on enum"); |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @return string |
| 30 | */ |
| 31 | public function getKeyAsCamelCase() |
| 32 | { |
| 33 | return Str::camel($this->getKey()); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param string $name |
| 38 | * |
| 39 | * @return bool |
| 40 | */ |
| 41 | protected static function hasConstant($name) |
| 42 | { |
| 43 | return array_key_exists($name, static::toArray()); |
| 44 | } |
| 45 | } |
| 46 |