Total Complexity | 9 |
Total Lines | 50 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
9 | class JsonEncrypter implements IJsonDecrypter |
||
10 | { |
||
11 | private $encrypter; |
||
12 | |||
13 | private $decryptAsObject; |
||
14 | |||
15 | public function __construct(IOriginalEncrypter $encrypter, $decryptAsObject = false) |
||
16 | { |
||
17 | $this->encrypter = $encrypter; |
||
18 | |||
19 | $this->decryptAsObject = $decryptAsObject; |
||
20 | } |
||
21 | |||
22 | public function encrypt($value) |
||
23 | { |
||
24 | return $this->encrypter->encrypt($this->asJson($value), false); |
||
25 | } |
||
26 | |||
27 | public function decrypt(string $value) |
||
28 | { |
||
29 | return $this->fromJson($this->encrypter->decrypt($value, false)); |
||
30 | } |
||
31 | |||
32 | public function decryptAsObject() |
||
33 | { |
||
34 | $this->decryptAsObject = true; |
||
35 | |||
36 | return new static($this->encrypter, $this->decryptAsObject); |
||
37 | } |
||
38 | |||
39 | protected function asJson($value) |
||
40 | { |
||
41 | if (!is_string($value)) { |
||
42 | return json_encode($value); |
||
43 | } |
||
44 | |||
45 | return $value; |
||
46 | } |
||
47 | |||
48 | protected function fromJson($value) |
||
59 | } |
||
60 | } |