Completed
Push — master ( 80801b...d13b94 )
by Peter
04:24
created

ExplicitEnum::equals()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 3
nop 1
crap 3
1
<?php
2
3
/**
4
 * GpsLab component.
5
 *
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/MIT
9
 */
10
11
namespace GpsLab\Component\Enum;
12
13
use GpsLab\Component\Enum\Exception\OutOfEnumException;
14
15
abstract class ExplicitEnum implements Enum, \Serializable
16
{
17
    /**
18
     * @var mixed
19
     */
20
    private $value = '';
21
22
    /**
23
     * @var Enum[]
24
     */
25
    private static $instances = [];
26
27
    /**
28
     * @param mixed $value
29
     */
30 1
    final private function __construct($value)
31
    {
32 1
        $this->value = $value;
33 1
    }
34
35
    /**
36
     * @param mixed $value
37
     *
38
     * @return Enum
39
     */
40 15
    final public static function byValue($value)
41
    {
42 15
        $key = get_called_class().'|'.$value;
43
44
        // limitation of count object instances
45 15
        if (!isset(self::$instances[$key])) {
46 2
            if (!static::isValid($value)) {
47 1
                throw OutOfEnumException::create($value, get_called_class());
48
            }
49
50 1
            self::$instances[$key] = new static($value);
51 1
        }
52
53 14
        return self::$instances[$key];
54
    }
55
56
    /**
57
     * @return mixed
58
     */
59 7
    final public function value()
60
    {
61 7
        return $this->value;
62
    }
63
64
    /**
65
     * Available values.
66
     *
67
     * @return Enum[]
68
     */
69 1
    final public static function values()
70
    {
71 1
        $values = [];
72 1
        foreach (static::choices() as $value => $label) {
73 1
            $values[$label] = self::byValue($value);
74 1
        }
75
76 1
        return $values;
77
    }
78
79
    /**
80
     * @param Enum $enum
81
     *
82
     * @return bool
83
     */
84 6
    final public function equals(Enum $enum)
85
    {
86 6
        return $this === $enum || ($this->value() === $enum->value() && get_called_class() == get_class($enum));
87
    }
88
89
    /**
90
     * Is value supported.
91
     *
92
     * @param mixed $value
93
     *
94
     * @return bool
95
     */
96 5
    final public static function isValid($value)
97
    {
98 5
        return array_key_exists($value, static::choices());
99
    }
100
101
    /**
102
     * Return readable value.
103
     *
104
     * @return string
105
     */
106 3
    public function __toString()
107
    {
108 3
        return static::choices()[$this->value()];
109
    }
110
111 1
    final public function __clone()
112
    {
113 1
        throw new \LogicException('Enumerations are not cloneable');
114
    }
115
116
    /**
117
     * @return mixed
118
     */
119 3
    public function serialize()
120
    {
121 3
        return serialize($this->value);
122
    }
123
124
    /**
125
     * @param mixed $data
126
     */
127 3
    public function unserialize($data)
128
    {
129 3
        static::byValue($this->value = unserialize($data));
130 3
    }
131
}
132