Completed
Push — master ( 848250...ac23ee )
by Marc
01:52
created

EnumMap::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 2
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace MabeEnum;
4
5
use SplObjectStorage;
6
use InvalidArgumentException;
7
8
/**
9
 * EnumMap implementation in base of SplObjectStorage
10
 *
11
 * @link http://github.com/marc-mabe/php-enum for the canonical source repository
12
 * @copyright Copyright (c) 2015 Marc Bennewitz
13
 * @license http://github.com/marc-mabe/php-enum/blob/master/LICENSE.txt New BSD License
14
 */
15
class EnumMap extends SplObjectStorage
16
{
17
    /**
18
     * The classname of the enumeration type
19
     * @var string
20
     */
21
    private $enumeration;
22
23
    /**
24
     * Constructor
25
     * @param string $enumeration The classname of the enumeration type
26
     * @throws InvalidArgumentException
27
     */
28 8
    public function __construct($enumeration)
29
    {
30 8
        if (!is_subclass_of($enumeration, __NAMESPACE__ . '\Enum')) {
1 ignored issue
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if __NAMESPACE__ . '\\Enum' can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
31 1
            throw new InvalidArgumentException(sprintf(
32 1
                "This EnumMap can handle subclasses of '%s' only",
33 1
                __NAMESPACE__ . '\Enum'
34
            ));
35
        }
36 7
        $this->enumeration = $enumeration;
37 7
    }
38
39
    /**
40
     * Get the classname of the enumeration
41
     * @return string
42
     */
43 1
    public function getEnumeration()
44
    {
45 1
        return $this->enumeration;
46
    }
47
48
    /**
49
     * Attach a new enumerator or overwrite an existing one
50
     * @param Enum|null|boolean|int|float|string $enumerator
51
     * @param mixed                              $data
52
     * @return void
53
     * @throws InvalidArgumentException On an invalid given enumerator
54
     */
55 3
    public function attach($enumerator, $data = null)
56
    {
57 3
        $enumeration = $this->enumeration;
58 3
        parent::attach($enumeration::get($enumerator), $data);
59 3
    }
60
61
    /**
62
     * Test if the given enumerator exists
63
     * @param Enum|null|boolean|int|float|string $enumerator
64
     * @return boolean
65
     */
66 5
    public function contains($enumerator)
67
    {
68
        try {
69 5
            $enumeration = $this->enumeration;
70 5
            return parent::contains($enumeration::get($enumerator));
71 1
        } catch (InvalidArgumentException $e) {
72
            // On an InvalidArgumentException the given argument can't be contained in this map
73 1
            return false;
74
        }
75
    }
76
77
    /**
78
     * Detach an enumerator
79
     * @param Enum|null|boolean|int|float|string $enumerator
80
     * @return void
81
     * @throws InvalidArgumentException On an invalid given enumerator
82
     */
83 2
    public function detach($enumerator)
84
    {
85 2
        $enumeration = $this->enumeration;
86 2
        parent::detach($enumeration::get($enumerator));
87 2
    }
88
89
    /**
90
     * Get a unique identifier for the given enumerator
91
     * @param Enum|null|boolean|int|float|string $enumerator
92
     * @return string
93
     * @throws InvalidArgumentException On an invalid given enumerator
94
     */
95 5
    public function getHash($enumerator)
96
    {
97 5
        $enumeration = $this->enumeration;
98 5
        return spl_object_hash($enumeration::get($enumerator));
99
    }
100
101
    /**
102
     * Test if the given enumerator exists
103
     * @param Enum|null|boolean|int|float|string $enumerator
104
     * @return boolean
105
     * @see contains()
106
     */
107 3
    public function offsetExists($enumerator)
108
    {
109 3
        return $this->contains($enumerator);
110
    }
111
112
    /**
113
     * Get mapped data for the given enumerator
114
     * @param Enum|null|boolean|int|float|string $enumerator
115
     * @return mixed
116
     * @throws InvalidArgumentException On an invalid given enumerator
117
     */
118 4
    public function offsetGet($enumerator)
119
    {
120 4
        $enumeration = $this->enumeration;
121 4
        return parent::offsetGet($enumeration::get($enumerator));
122
    }
123
124
    /**
125
     * Attach a new enumerator or overwrite an existing one
126
     * @param Enum|null|boolean|int|float|string $enumerator
127
     * @param mixed                              $data
128
     * @return void
129
     * @throws InvalidArgumentException On an invalid given enumerator
130
     * @see attach()
131
     */
132 3
    public function offsetSet($enumerator, $data = null)
133
    {
134 3
        $enumeration = $this->enumeration;
135 3
        parent::offsetSet($enumeration::get($enumerator), $data);
136 2
    }
137
138
    /**
139
     * Detach an existing enumerator
140
     * @param Enum|null|boolean|int|float|string $enumerator
141
     * @return void
142
     * @throws InvalidArgumentException On an invalid given enumerator
143
     * @see detach()
144
     */
145 2
    public function offsetUnset($enumerator)
146
    {
147 2
        $enumeration = $this->enumeration;
148 2
        parent::offsetUnset($enumeration::get($enumerator));
149 2
    }
150
151
    /**
152
     * Get the current value
153
     * @return mixed
154
     */
155 1
    public function current()
156
    {
157 1
        return parent::getInfo();
158
    }
159
160
    /**
161
     * Get the current key
162
     * @return Enum|null
163
     */
164 1
    public function key()
165
    {
166 1
        return parent::current();
167
    }
168
}
169