Completed
Push — master ( bcf621...6c322c )
by Ondrej
13s queued 11s
created

Enum::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
/**
3
MIT License
4
5
Copyright (c) 2017 Ondrej Hatala
6
7
Permission is hereby granted, free of charge, to any person obtaining a copy
8
of this software and associated documentation files (the "Software"), to deal
9
in the Software without restriction, including without limitation the rights
10
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
copies of the Software, and to permit persons to whom the Software is
12
furnished to do so, subject to the following conditions:
13
14
The above copyright notice and this permission notice shall be included in all
15
copies or substantial portions of the Software.
16
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
SOFTWARE.
24
 */
25
namespace SpareParts\Enum;
26
27
use SpareParts\Enum\Exception\InvalidEnumSetupException;
28
use SpareParts\Enum\Exception\InvalidEnumValueException;
29
use SpareParts\Enum\Exception\OperationNotAllowedException;
30
use SpareParts\Enum\Mapping\Annotations;
31
use SpareParts\Enum\Set\ISet;
32
33
abstract class Enum
34
{
35
36
    /** @var string[] */
37
    protected static $values = [];
38
39
    /** @var string[][] */
40
    private static $innerValues = [];
41
42
    /** @var self[][] */
43
    protected static $instances = [];
44
45
    /**
46
     * @var string
47
     */
48
    protected $value;
49
50
    /**
51
     * PROTECTED!! Not callable directly.
52
     * @see static::instance()
53
     * @see static::__callStatic()
54
     * @internal
55
     */
56 8
    protected function __construct(string $value)
57
    {
58 8
        if (!isset(self::$innerValues[get_class($this)])) {
59
            // first, try to fetch correct values
60 4
            if (empty(static::$values)) {
61
                // try to use annotations
62 2
                self::$innerValues[get_class($this)] = Annotations::loadEnumValues(get_class($this));
63
            } else {
64 2
                self::$innerValues[get_class($this)] = static::$values;
65
            }
66
67
            // then check we did it in a constructive way
68 4
            if (empty(self::$innerValues[get_class($this)])) {
69 1
                throw new InvalidEnumSetupException('Incorrect setup! Enum '.get_called_class().' doesn\'t have its static::$values set.');
70
            }
71
        }
72
73
        // does this $value exist in current Enum
74 7
        if (!in_array($value, self::$innerValues[get_class($this)])) {
75 3
            throw new InvalidEnumValueException('Enum '.get_called_class().' does not contain value '.$value.'. Possible values are: '.implode(', ', static::$values));
76
        }
77 4
        $this->value = $value;
78 4
    }
79
80
    /**
81
     * String representation
82
     */
83 50
    public function __toString(): string
84
    {
85 50
        return $this->value;
86
    }
87
88 62
    public static function __callStatic(string $method, array $args): self
89
    {
90 62
        if ($method[0] === '_') {
91 7
            $method = substr($method, 1);
92
        }
93 62
        return static::instance($method);
94
    }
95
96 68
    public static function instance(string $value): self
97
    {
98 68
        if (!isset(static::$instances[get_called_class()][$value])) {
99 8
            static::$instances[get_called_class()][$value] = new static($value);
100
        }
101
102 64
        return static::$instances[get_called_class()][$value];
103
    }
104
105
    /**
106
     *
107
     *
108
     * @param iterable|self[] $enumList
109
     * @return bool
110
     */
111 4
    public function isAnyOf(iterable $enumList): bool
112
    {
113 4
        foreach ($enumList as $enum) {
114 3
            if ($enum === $this) {
115 2
                return true;
116
            }
117
        }
118 3
        return false;
119
    }
120
121 1
    public function __clone()
122
    {
123 1
        throw new OperationNotAllowedException('Singleton cannot be cloned.');
124
    }
125
126 1
    public function __sleep()
127
    {
128 1
        throw new OperationNotAllowedException('Singleton cannot be serialized.');
129
    }
130
131 1
    public function __wakeup()
132
    {
133 1
        throw new OperationNotAllowedException('Singleton cannot be serialized');
134
    }
135
}
136