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
|
|
|
* @var KeyIdentity |
31
|
|
|
*/ |
32
|
|
|
private $key; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* EncryptedColumn constructor. |
36
|
|
|
* @param string $classname |
37
|
|
|
* @param string $data |
38
|
|
|
* @param EncryptorIdentity $encryptor |
39
|
|
|
* @param SerializerIdentity $serializer |
40
|
|
|
* @param KeyIdentity $key |
41
|
|
|
*/ |
42
|
7 |
|
public function __construct( |
43
|
|
|
string $classname, |
44
|
|
|
string $data, |
45
|
|
|
EncryptorIdentity $encryptor, |
46
|
|
|
SerializerIdentity $serializer, |
47
|
|
|
KeyIdentity $key |
48
|
|
|
) { |
49
|
7 |
|
$this->classname = $classname; |
50
|
7 |
|
$this->data = $data; |
51
|
7 |
|
$this->encryptor = $encryptor; |
52
|
7 |
|
$this->serializer = $serializer; |
53
|
7 |
|
$this->key = $key; |
54
|
7 |
|
} |
55
|
|
|
|
56
|
6 |
|
public static function fromArray(array $data) |
57
|
|
|
{ |
58
|
6 |
|
return new self( |
59
|
6 |
|
$data['classname'], |
60
|
6 |
|
$data['data'], |
61
|
6 |
|
new EncryptorIdentity($data['encryptor']), |
62
|
6 |
|
new SerializerIdentity($data['serializer']), |
63
|
6 |
|
new KeyIdentity($data['keyid']) |
64
|
|
|
); |
65
|
|
|
} |
66
|
|
|
|
67
|
6 |
|
public function jsonSerialize(): array |
68
|
|
|
{ |
69
|
|
|
return [ |
70
|
6 |
|
'classname' => $this->classname, |
71
|
6 |
|
'data' => $this->data, |
72
|
6 |
|
'encryptor' => $this->encryptor->toString(), |
73
|
6 |
|
'serializer' => $this->serializer->toString(), |
74
|
6 |
|
'keyid' => $this->key->toString(), |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
6 |
|
public function getClassname(): string |
82
|
|
|
{ |
83
|
6 |
|
return $this->classname; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
4 |
|
public function getData(): string |
90
|
|
|
{ |
91
|
4 |
|
return $this->data; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return EncryptorIdentity |
96
|
|
|
*/ |
97
|
6 |
|
public function getEncryptorIdentifier(): EncryptorIdentity |
98
|
|
|
{ |
99
|
6 |
|
return $this->encryptor; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return SerializerIdentity |
104
|
|
|
*/ |
105
|
6 |
|
public function getSerializerIdentifier(): SerializerIdentity |
106
|
|
|
{ |
107
|
6 |
|
return $this->serializer; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return KeyIdentity |
112
|
|
|
*/ |
113
|
6 |
|
public function getKeyIdentifier(): KeyIdentity |
114
|
|
|
{ |
115
|
6 |
|
return $this->key; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function needsReencryption(EncryptorIdentity $encryptor, SerializerIdentity $serializer): bool |
119
|
|
|
{ |
120
|
|
|
return $encryptor->equals($this->encryptor) && $serializer->equals($this->serializer); |
121
|
|
|
} |
122
|
|
|
} |