Completed
Push — master ( c55d6b...4b2e15 )
by Elf
05:02
created

EnumTransferKeys::getOriginalKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Traits;
4
5
/**
6
 * Transfer keys for `MyCLabs\Enum\Enum`.
7
 *
8
 * protected static $transferKeys = [
9
 *     'original_key' => 'transferred_key',
10
 * ];
11
 */
12
trait EnumTransferKeys
13
{
14
    /**
15
     * Get the transfer keys.
16
     *
17
     * @return array
18
     */
19
    protected static function transferKeys()
20
    {
21
        if (
22
            property_exists(get_called_class(), 'transferKeys') &&
23
            is_array(static::$transferKeys)
24
        ) {
25
            return static::$transferKeys;
26
        }
27
28
        return [];
29
    }
30
31
    /**
32
     * Get the transferred key for the given key.
33
     *
34
     * @param  mixed  $key
35
     * @return mixed
36
     */
37
    protected static function getTransferredKey($key)
38
    {
39
        return static::transferKeys()[$key] ?? $key;
40
    }
41
42
    /**
43
     * Get the original key.
44
     *
45
     * @param  mixed  $key
46
     * @return mixed
47
     */
48
    protected static function getOriginalKey($key)
49
    {
50
        $originalKey = array_search($key, static::transferKeys(), true);
51
52
        return $originalKey !== false ? $originalKey : $key;
53
    }
54
}
55