AbstractIdentity   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 11
eloc 17
c 4
b 0
f 0
dl 0
loc 95
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getValue() 0 3 1
A isEqualTo() 0 5 3
A unserialize() 0 5 1
A __serialize() 0 3 1
A serialize() 0 3 1
A getAllowedInterfaces() 0 3 1
A __unserialize() 0 5 1
A jsonSerialize() 0 3 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->assertImmutable();
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
     * @return array<string, mixed>
64
     */
65
    final public function __serialize(): array
66
    {
67
        return ['value' => $this->value];
68
    }
69
70
    /**
71
     * @param array<string, mixed> $data
72
     */
73
    final public function __unserialize(array $data): void
74
    {
75
        $this->assertImmutable();
76
77
        $this->value = $data['value'];
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83
    final public function serialize(): string
84
    {
85
        return \serialize($this->value);
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     *
91
     * @param mixed $serialized
92
     */
93
    final public function unserialize($serialized): void
94
    {
95
        $this->assertImmutable();
96
97
        $this->value = \unserialize($serialized, ['allowed_classes' => false]);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    final public function jsonSerialize(): string
104
    {
105
        return $this->value;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     *
111
     * @return string[]
112
     */
113
    final protected function getAllowedInterfaces(): array
114
    {
115
        return [Identity::class];
116
    }
117
}
118