QiniuEnum.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. // @codingStandardsIgnoreStart
  3. // phpcs:disable PSR1.Classes.ClassDeclaration.MultipleClasses
  4. namespace Qiniu\Enum;
  5. use MyCLabs\Enum\Enum;
  6. if (method_exists("MyCLabs\\Enum\\Enum", "from")) {
  7. abstract class QiniuEnum extends Enum
  8. {
  9. // @codingStandardsIgnoreEnd
  10. // @codingStandardsIgnoreStart
  11. }
  12. } else {
  13. /**
  14. * poly fill MyCLabs\Enum\Enum::from in low version
  15. *
  16. * @link https://github.com/myclabs/php-enum
  17. */
  18. abstract class QiniuEnum extends Enum
  19. {
  20. // @codingStandardsIgnoreEnd
  21. /**
  22. * @param mixed $value
  23. * @return static
  24. */
  25. public static function from($value)
  26. {
  27. $key = self::assertValidValueReturningKey($value);
  28. return self::__callStatic($key, array());
  29. }
  30. /**
  31. * Asserts valid enum value
  32. *
  33. * @psalm-pure
  34. * @psalm-assert T $value
  35. * @param mixed $value
  36. * @return string
  37. */
  38. private static function assertValidValueReturningKey($value)
  39. {
  40. if (false === ($key = self::search($value))) {
  41. throw new \UnexpectedValueException("Value '$value' is not part of the enum " . __CLASS__);
  42. }
  43. return $key;
  44. }
  45. // @codingStandardsIgnoreStart
  46. }
  47. }