PrettyEnum::pretty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace JefersonC\Enum;
4
5
abstract class PrettyEnum
6
{
7
    protected static $instances = [];
8
9 6
    protected static function childInstance() {
10 6
        $class = get_called_class();
11
12 6
        if(!isset(static::$instances[$class])) {
13 2
            static::$instances[$class] = new $class();
14
        }
15
16 6
        return static::$instances[$class];
17
    }
18
19 6
    protected static function constants() {
20 6
        $class = static::childInstance();
21 6
        return $class->register();
22
    }
23
24 2
    public static function get($index) {
25 2
      $item = static::getItem($index);
26
27 2
      return $item['value'];
28
    }
29
30 3
    public static function pretty($index) {
31 3
        $item = static::getItem($index);
32
33 2
        return $item['name'];
34
    }
35
36 1
    public static function all() {
37 1
        $constants = static::constants();
38
39 1
        $out = [];
40
41 1
        foreach($constants as $index => $const) {
42 1
            $out[] = static::mountItem($const);
43
        }
44
45 1
        return $out;
46
    }
47
48 5
    protected static function getItem($key) {
49 5
        $constants = static::constants();
50
51 5
        if (!isset($constants[$key])) {
52 1
            throw new InvalidEnumKeyException(sprintf('Key %s does not exists on %s', $key, get_class(static::childInstance())));
53
        }
54
55 4
        return $constants[$key];
56
    }
57
58 1
    public static function item($key) {
59 1
        return static::mountItem(static::getItem($key));
60
    }
61
62 2
    protected static function mountItem($item) {
63 2
        $obj = new \stdClass();
64
65 2
        foreach($item as $key => $value) {
66 2
            $obj->{$key} = $value;
67
        }
68
69 2
        return $obj;
70
    }
71
72 1
    public static function __callStatic($name, $arguments)
73
    {
74 1
        if(!method_exists(__CLASS__, $name)) {
75 1
            throw new InvalidEnumMethodException(sprintf('Method %s does not exists', $name));
76
        }
77
78
        return static::{$name}($arguments);
79
    }
80
}
81