Passed
Push — master ( 913c0a...fd8403 )
by Ondřej
05:45
created

EnumItem::compareTo()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 10
nc 5
nop 1
1
<?php
2
declare(strict_types=1);
3
namespace Ivory\Value;
4
5
use Ivory\Exception\IncomparableException;
6
use Ivory\Value\Alg\IComparable;
7
8
class EnumItem implements IComparable
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
9
{
10
    private $typeSchema;
11
    private $typeName;
12
    private $value;
13
    private $ordinal;
14
15
    public static function forType(string $typeSchema, string $typeName, string $value, ?int $ordinal = null): EnumItem
16
    {
17
        return new EnumItem($typeSchema, $typeName, $value, $ordinal);
18
    }
19
20
    private function __construct(string $typeSchema, string $typeName, string $value, ?int $ordinal)
21
    {
22
        $this->typeSchema = $typeSchema;
23
        $this->typeName = $typeName;
24
        $this->value = $value;
25
        $this->ordinal = $ordinal;
26
    }
27
28
    public function getValue(): string
29
    {
30
        return $this->value;
31
    }
32
33
    public function getTypeSchema(): string
34
    {
35
        return $this->typeSchema;
36
    }
37
38
    public function getTypeName(): string
39
    {
40
        return $this->typeName;
41
    }
42
43
    public function __toString()
44
    {
45
        return $this->value;
46
    }
47
48
    public function equals($other): bool
49
    {
50
        return (
51
            $other instanceof EnumItem &&
52
            $this->typeSchema == $other->typeSchema &&
53
            $this->typeName == $other->typeName &&
54
            $this->value == $other->value
55
        );
56
    }
57
58
    public function compareTo($other): int
59
    {
60
        if (!$other instanceof EnumItem) {
61
            throw new IncomparableException();
62
        }
63
        if ($this->typeSchema != $other->typeSchema || $this->typeName != $other->typeName) {
64
            throw new IncomparableException('Comparing enum items of different enumerations.');
65
        }
66
67
        if ($this->value == $other->value) {
68
            return 0;
69
        }
70
        if ($this->ordinal === null || $other->ordinal === null) {
71
            throw new IncomparableException('Unspecified ordinal');
72
        }
73
        return ($this->ordinal - $other->ordinal);
74
    }
75
}
76