Completed
Push — master ( 7b356c...8ed289 )
by Julián
12:12
created

AbstractIdentity   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 1
dl 0
loc 85
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isEqualTo() 0 6 3
A getValue() 0 4 1
A __toString() 0 4 1
A serialize() 0 4 1
A unserialize() 0 4 1
A jsonSerialize() 0 4 1
A getAllowedInterfaces() 0 4 1
1
<?php
2
3
/*
4
 * identity (https://github.com/phpgears/identity).
5
 * Identity objects for PHP.
6
 *
7
 * @license MIT
8
 * @link https://github.com/phpgears/identity
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
declare(strict_types=1);
13
14
namespace Gears\Identity;
15
16
use Gears\Immutability\ImmutabilityBehaviour;
17
18
/**
19
 * Base immutable identity.
20
 */
21
abstract class AbstractIdentity implements Identity
22
{
23
    use ImmutabilityBehaviour;
24
25
    /**
26
     * Identity value.
27
     *
28
     * @var string
29
     */
30
    private $value;
31
32
    /**
33
     * AbstractIdentity constructor.
34
     *
35
     * @param string $value
36
     */
37
    final protected function __construct(string $value)
38
    {
39
        $this->checkImmutability();
40
41
        $this->value = $value;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    final public function isEqualTo($identity): bool
48
    {
49
        return \is_object($identity)
50
            && \get_class($identity) === static::class
51
            && $identity->getValue() === $this->getValue();
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    final public function getValue(): string
58
    {
59
        return $this->value;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    final public function __toString(): string
66
    {
67
        return $this->value;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function serialize(): string
74
    {
75
        return \serialize($this->value);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     *
81
     * @param mixed $serialized
82
     */
83
    public function unserialize($serialized): void
84
    {
85
        $this->value = \unserialize($serialized, [static::class]);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    final public function jsonSerialize(): string
92
    {
93
        return $this->value;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     *
99
     * @return string[]
100
     */
101
    final protected function getAllowedInterfaces(): array
102
    {
103
        return [Identity::class];
104
    }
105
}
106