Completed
Pull Request — master (#7)
by Ondrej
05:55
created

SetTrait::getIterator()   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 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace SpareParts\Enum\Set;
3
4
use SpareParts\Enum\Enum;
5
use SpareParts\Enum\Exception\EnumSetMustContainEnumsException;
6
use SpareParts\Enum\Exception\InvalidEnumClassException;
7
8
trait SetTrait
9
{
10
    /**
11
     * @var Enum[]
12
     */
13
    protected $set = [];
14
15
    /**
16
     * @var string
17
     */
18
    protected $enumClass;
19
20
    /**
21
     * @param string $enumClass
22
     * @param Enum[] $set
23
     */
24 41
    public function __construct($enumClass, $set = [])
25
    {
26
        // if enum class was sent, we must make sure it is valid
27 41
        $this->setEnumClass($enumClass);
28
29 39
        if (!is_array($set)) {
30 2
            throw new EnumSetMustContainEnumsException(sprintf("Enum set must be initialized with array of enums."));
31
        }
32 37
        foreach ($set as $enum) {
33 35
            if (!($enum instanceof $this->enumClass)) {
34 6
                throw new EnumSetMustContainEnumsException(sprintf("Expected %s, got %s", $this->enumClass, get_class($enum)));
35
            }
36 31
            $this->set[(string) $enum] = $enum;
37 33
        }
38 31
    }
39
40
    /**
41
     * @return \ArrayIterator
42
     */
43 16
    public function getIterator()
44
    {
45 16
        return new \ArrayIterator(array_values($this->set));
46
    }
47
48
    /**
49
     * @return int
50
     */
51 14
    public function count()
52
    {
53 14
        return count($this->set);
54
    }
55
56
    /**
57
     * @param Enum $value
58
     * @return bool
59
     */
60 16
    public function contains(Enum $value)
61
    {
62 16
        if (in_array($value, $this->set)) {
63 16
            return true;
64
        }
65 10
        return false;
66
    }
67
68
    /**
69
     * @param string $enumClass
70
     */
71 41 View Code Duplication
    protected function setEnumClass($enumClass)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73 41
        if (!is_null($enumClass) && !is_subclass_of($enumClass, Enum::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \SpareParts\Enum\Enum::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
74 2
            throw new InvalidEnumClassException("Class ${enumClass} does not implement Enum (as it should). Maybe you forgot to specify correct enum class in constructor?");
75
        }
76
        $this->enumClass = $enumClass;
77
    }
78
}
79