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

EnumDefaultKey   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 11
cp 0
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A defaultKey() 0 8 2
A defaultValue() 0 4 1
1
<?php
2
3
namespace ElfSundae\Laravel\Support\Traits;
4
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Define the default key/value for `MyCLabs\Enum\Enum`.
9
 *
10
 * If the property `static $defaultKey` does not exist, the first
11
 * constant will be used.
12
 */
13
trait EnumDefaultKey
14
{
15
    /**
16
     * Get the default key.
17
     *
18
     * @return string
19
     */
20
    public static function defaultKey()
21
    {
22
        if (property_exists(get_called_class(), 'defaultKey')) {
23
            return static::$defaultKey;
24
        }
25
26
        return Arr::first(array_keys(static::toArray()));
27
    }
28
29
    /**
30
     * Get the default value.
31
     *
32
     * @return mixed
33
     */
34
    public static function defaultValue()
35
    {
36
        return Arr::get(static::toArray(), static::defaultKey());
37
    }
38
}
39