Completed
Push — master ( 5bd6ed...10650f )
by Julián
05:37
created

AbstractIdentity::__serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
     * {@inheritdoc}
64
     */
65
    final public function __toString(): string
66
    {
67
        return $this->value;
68
    }
69
70
    /**
71
     * @return array<string, mixed>
72
     */
73
    final public function __serialize(): array
74
    {
75
        return ['value' => $this->value];
76
    }
77
78
    /**
79
     * @param array<string, mixed> $data
80
     */
81
    public function __unserialize(array $data): void
82
    {
83
        $this->assertImmutable();
84
85
        $this->value = $data['value'];
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    final public function serialize(): string
92
    {
93
        return \serialize($this->value);
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     *
99
     * @param mixed $serialized
100
     */
101
    final public function unserialize($serialized): void
102
    {
103
        $this->assertImmutable();
104
105
        $this->value = \unserialize($serialized, ['allowed_classes' => false]);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    final public function jsonSerialize(): string
112
    {
113
        return $this->value;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     *
119
     * @return string[]
120
     */
121
    final protected function getAllowedInterfaces(): array
122
    {
123
        return [Identity::class];
124
    }
125
}
126