Passed
Pull Request — master (#364)
by
unknown
20:25
created

QiniuEnum::assertValidValueReturningKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Qiniu\Enum;
4
5
use MyCLabs\Enum\Enum;
6
7
abstract class QiniuEnum extends Enum
8
{
9
    /**
10
     * @param mixed $value
11
     * @return static
12
     */
13
    public static function from($value)
14
    {
15
        $key = self::assertValidValueReturningKey($value);
16
17
        return self::__callStatic($key, array());
18
    }
19
20
    /**
21
     * Asserts valid enum value
22
     *
23
     * @psalm-pure
24
     * @psalm-assert T $value
25
     * @param mixed $value
26
     * @return string
27
     */
28
    private static function assertValidValueReturningKey($value)
29
    {
30
        if (false === ($key = self::search($value))) {
31
            throw new \UnexpectedValueException("Value '$value' is not part of the enum " . __CLASS__);
32
        }
33
34
        return $key;
35
    }
36
}
37