StringIdentity::equals()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 2
eloc 2
nc 2
nop 1
1
<?php
2
3
namespace Domain\Identity;
4
5
/**
6
 * @author Sebastiaan Hilbers <[email protected]>
7
 */
8
abstract class StringIdentity implements Identity
9
{
10
    /**
11
     * @var string
12
     */
13
    private $string;
14
15
    public function __construct($string)
16
    {
17
        self::guardString($string);
18
        $this->string = $string;
19
    }
20
21
    /**
22
     * Creates an identifier object from a string representation
23
     *
24
     * @param $string
25
     * @return static
26
     */
27
    public static function fromString($string)
28
    {
29
        return new static($string);
30
    }
31
32
    /**
33
     * Returns a string that can be parsed by fromString()
34
     *
35
     * @return string
36
     */
37
    public function __toString()
38
    {
39
        return (string) $this->string;
40
    }
41
42
    /**
43
     * Compares the object to another IdentifiesAggregate object. Returns true if both have the same type and value.
44
     *
45
     * @param Identity $other
46
     * @return boolean
47
     */
48
    public function equals(Identity $other)
49
    {
50
        return $other instanceof static && $this->string == $other->string;
51
    }
52
53
    /**
54
     * Make sure that the input is in fact a string
55
     *
56
     * @param $string
57
     */
58
    private static function guardString($string)
59
    {
60
        if (!is_string($string) || empty($string)) {
61
            throw new \InvalidArgumentException("String expected");
62
        }
63
    }
64
}
65