Passed
Push — master ( 7d3520...28b042 )
by Sebastian
24:23 queued 16:06
created

StringItem::equals()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
declare(strict_types=1);
3
/*
4
 * Copyright (C) 2019 Sebastian Böttger <[email protected]>
5
 * You may use, distribute and modify this code under the
6
 * terms of the MIT license.
7
 *
8
 * You should have received a copy of the MIT license with
9
 * this file. If not, please visit: https://opensource.org/licenses/mit-license.php
10
 */
11
12
namespace Seboettg\Forest\Item;
13
14
use Seboettg\Collection\Comparable\Comparable;
15
16
class StringItem implements ItemInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $value;
22
23 12
    public function __construct(string $value)
24
    {
25 12
        $this->value = $value;
26 12
    }
27
28
    public function setValue(string $value)
29
    {
30
        $this->value = $value;
31
    }
32
33 27
    public function getValue(): ?string
34
    {
35 27
        return $this->value;
36
    }
37
38
    /**
39
     * @param Comparable|StringItem $b
40
     * @return int
41
     */
42 26
    public function compareTo(Comparable $b): int
43
    {
44
        /** @var StringItem $b */
45 26
        $diff = strcasecmp($this->getValue(), $b->getValue());
46 26
        if ($diff === 0) {
47 11
            return $diff;
48
        }
49 26
        return $diff > 0 ? 1 : -1;
50
    }
51
52 10
    public function __toString()
53
    {
54 10
        return (string)$this->value;
55
    }
56
57
    /**
58
     * @param ItemInterface|StringItem $item
59
     * @return bool
60
     */
61
    public function equals(ItemInterface $item): bool
62
    {
63
        return $this->compareTo($item) === 0;
64
    }
65
}
66