Enum   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 100
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getValue() 0 4 1
A setValue() 0 8 2
A isValidEnumValue() 0 10 3
A __construct() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
A __toString() 0 4 1
1
<?php
2
3
namespace FinanCalc\Utils {
4
5
    use Exception;
6
    use FinanCalc\Constants\ErrorMessages;
7
8
    /**
9
     * util Enum class pulled in its entirety from here: http://www.codeproject.com/Articles/683009/Enum-support-in-PHP
10
     *
11
     * @author Johan Ohlin
12
     */
13
    abstract class Enum
14
    {
15
        protected $value;
16
17
        /**
18
         * Return string representation of this enum
19
         *
20
         * @return integer
21
         */
22
        public function getValue()
23
        {
24
            return $this->value;
25
        }
26
27
        /**
28
         * Tries to set the value  of this enum
29
         *
30
         * @param int $value
31
         * @throws Exception If value is not part of this enum
32
         */
33
        public function setValue($value)
34
        {
35
            if ($this->isValidEnumValue($value)) {
36
                $this->value = $value;
37
            } else {
38
                throw new Exception(ErrorMessages::getInvalidTypeMessage());
39
            }
40
        }
41
42
        /**
43
         * Validates if the type given is part of this enum class
44
         *
45
         * @param string $checkValue
46
         * @return bool
47
         */
48
        public function isValidEnumValue($checkValue)
49
        {
50
            $reflector = new \ReflectionClass(get_class($this));
51
            foreach ($reflector->getConstants() as $validValue) {
52
                if ($validValue == $checkValue) {
53
                    return true;
54
                }
55
            }
56
            return false;
57
        }
58
59
        /**
60
         * @param string $value Value for this display type
61
         */
62
        public function __construct($value)
63
        {
64
            $this->setValue($value);
65
        }
66
67
        /**
68
         * With a magic getter you can get the value from this enum using
69
         * any variable name as in:
70
         *
71
         * <code>
72
         *   $myEnum = new MyEnum(MyEnum::start);
73
         *   echo $myEnum->v;
74
         * </code>
75
         *
76
         * @param string $property
77
         * @return integer
78
         */
79
        public function __get($property)
80
        {
81
            return $this->value;
82
        }
83
84
        /**
85
         * With a magic setter you can set the enum value using any variable
86
         * name as in:
87
         *
88
         * <code>
89
         *   $myEnum = new MyEnum(MyEnum::Start);
90
         *   $myEnum->v = MyEnum::End;
91
         * </code>
92
         *
93
         * @param string $property
94
         * @param string $value
95
         * @throws Exception Throws exception if an invalid type is used
96
         */
97
        public function __set($property, $value)
98
        {
99
            $this->setValue($value);
100
        }
101
102
        /**
103
         * If the enum is requested as a string then this function will be automatically
104
         * called and the value of this enum will be returned as a string.
105
         *
106
         * @return string
107
         */
108
        public function __toString()
109
        {
110
            return (string)$this->value;
111
        }
112
    }
113
}
114