1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Carnage\EncryptedColumn\ValueObject; |
4
|
|
|
|
5
|
|
|
use Carnage\EncryptedColumn\Encryptor\HaliteEncryptor; |
6
|
|
|
use Carnage\EncryptedColumn\Serializer\PhpSerializer; |
7
|
|
|
|
8
|
|
|
class EncryptedColumn implements \JsonSerializable |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $classname; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $data; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var EncryptorIdentity |
22
|
|
|
*/ |
23
|
|
|
private $encryptor; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var SerializerIdentity |
27
|
|
|
*/ |
28
|
|
|
private $serializer; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var KeyIdentity |
32
|
|
|
*/ |
33
|
|
|
private $key; |
34
|
|
|
|
35
|
6 |
|
public function __construct( |
36
|
|
|
string $classname, |
37
|
|
|
string $data, |
38
|
|
|
EncryptorIdentity $encryptor, |
39
|
|
|
SerializerIdentity $serializer, |
40
|
|
|
KeyIdentity $key |
41
|
6 |
|
) { |
42
|
6 |
|
$this->classname = $classname; |
43
|
6 |
|
$this->data = $data; |
44
|
6 |
|
$this->encryptor = $encryptor; |
45
|
6 |
|
$this->serializer = $serializer; |
46
|
|
|
$this->key = $key; |
47
|
5 |
|
} |
48
|
|
|
|
49
|
|
|
public static function fromArray(array $data) |
50
|
|
|
{ |
51
|
5 |
|
return new self( |
52
|
|
|
$data['classname'], |
53
|
|
|
$data['data'], |
54
|
|
|
new EncryptorIdentity($data['encryptor']), |
55
|
|
|
new SerializerIdentity($data['serializer']), |
56
|
|
|
new KeyIdentity($data['keyid']) |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
|
60
|
5 |
|
public function jsonSerialize(): array |
61
|
5 |
|
{ |
62
|
5 |
|
return [ |
63
|
5 |
|
'classname' => $this->classname, |
64
|
5 |
|
'data' => $this->data, |
65
|
|
|
'encryptor' => $this->encryptor->toString(), |
66
|
|
|
'serializer' => $this->serializer->toString(), |
67
|
|
|
'keyid' => $this->key->toString(), |
68
|
5 |
|
]; |
69
|
|
|
} |
70
|
|
|
|
71
|
5 |
|
/** |
72
|
5 |
|
* @return mixed |
73
|
5 |
|
*/ |
74
|
5 |
|
public function getClassname(): string |
75
|
|
|
{ |
76
|
|
|
return $this->classname; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return mixed |
81
|
5 |
|
*/ |
82
|
|
|
public function getData(): string |
83
|
5 |
|
{ |
84
|
|
|
return $this->data; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return EncryptorIdentity |
89
|
3 |
|
*/ |
90
|
|
|
public function getEncryptorIdentifier(): EncryptorIdentity |
91
|
3 |
|
{ |
92
|
|
|
return $this->encryptor; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return SerializerIdentity |
97
|
5 |
|
*/ |
98
|
|
|
public function getSerializerIdentifier(): SerializerIdentity |
99
|
5 |
|
{ |
100
|
|
|
return $this->serializer; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return KeyIdentity |
105
|
5 |
|
*/ |
106
|
|
|
public function getKeyIdentifier(): KeyIdentity |
107
|
5 |
|
{ |
108
|
|
|
return $this->key; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function needsReencryption(EncryptorIdentity $encryptor, SerializerIdentity $serializer): bool |
112
|
|
|
{ |
113
|
|
|
return $encryptor->equals($this->encryptor) && $serializer->equals($this->serializer); |
114
|
|
|
} |
115
|
|
|
} |