Skip to content

Commit 15181ec

Browse files
committed
make myclabs/php-enum compatible
1 parent c88c4e7 commit 15181ec

File tree

2 files changed

+34
-26
lines changed

2 files changed

+34
-26
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"require": {
2121
"php": ">=5.3.3",
22-
"myclabs/php-enum": "1.6.6"
22+
"myclabs/php-enum": "~1.6.6 || ~1.7.7 || ~1.8.4"
2323
},
2424
"require-dev": {
2525
"paragonie/random_compat": ">=2",

src/Qiniu/Enum/QiniuEnum.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,49 @@
11
<?php
2+
// phpcs:disable
3+
// PSR1.Classes.ClassDeclaration.MultipleClasses
24

35
namespace Qiniu\Enum;
46

57
use MyCLabs\Enum\Enum;
68

7-
/**
8-
* 扩展 MyCLabs\Enum\Enum 以使用其新版本的 from 方法
9-
*
10-
* @link https://github.com/myclabs/php-enum
11-
*/
12-
abstract class QiniuEnum extends Enum
13-
{
14-
/**
15-
* @param mixed $value
16-
* @return static
17-
*/
18-
public static function from($value)
9+
if (method_exists("MyCLabs\\Enum\\Enum", "from")) {
10+
abstract class QiniuEnum extends Enum
1911
{
20-
$key = self::assertValidValueReturningKey($value);
21-
22-
return self::__callStatic($key, array());
2312
}
24-
13+
} else {
2514
/**
26-
* Asserts valid enum value
15+
* poly fill MyCLabs\Enum\Enum::from in low version
2716
*
28-
* @psalm-pure
29-
* @psalm-assert T $value
30-
* @param mixed $value
31-
* @return string
17+
* @link https://github.com/myclabs/php-enum
3218
*/
33-
private static function assertValidValueReturningKey($value)
19+
abstract class QiniuEnum extends Enum
3420
{
35-
if (false === ($key = self::search($value))) {
36-
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . __CLASS__);
21+
/**
22+
* @param mixed $value
23+
* @return static
24+
*/
25+
public static function from($value)
26+
{
27+
$key = self::assertValidValueReturningKey($value);
28+
29+
return self::__callStatic($key, array());
3730
}
3831

39-
return $key;
32+
/**
33+
* Asserts valid enum value
34+
*
35+
* @psalm-pure
36+
* @psalm-assert T $value
37+
* @param mixed $value
38+
* @return string
39+
*/
40+
private static function assertValidValueReturningKey($value)
41+
{
42+
if (false === ($key = self::search($value))) {
43+
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . __CLASS__);
44+
}
45+
46+
return $key;
47+
}
4048
}
4149
}

0 commit comments

Comments
 (0)