1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LeKoala\Encrypt; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Forms\HiddenField; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A simple extension over EncryptedDBText that supports json |
9
|
|
|
* as a datastructure |
10
|
|
|
* The data is stored in a text field |
11
|
|
|
* |
12
|
|
|
* If you want to access array stuff, you need to use |
13
|
|
|
* $model->dbObject('myField')->toArray() or any other method |
14
|
|
|
* |
15
|
|
|
* This field is a great way to store serialized encrypted data |
16
|
|
|
*/ |
17
|
|
|
class EncryptedDBJson extends EncryptedDBText |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* We cannot search on json fields |
21
|
|
|
* |
22
|
|
|
* @param string $title |
23
|
|
|
* @return HiddenField |
24
|
|
|
*/ |
25
|
|
|
public function scaffoldSearchField($title = null) |
26
|
|
|
{ |
27
|
|
|
return HiddenField::create($this->getName()); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Json data is not easily displayed |
32
|
|
|
* |
33
|
|
|
* @param string $title |
34
|
|
|
* @param string $params |
35
|
|
|
* @return HiddenField |
36
|
|
|
*/ |
37
|
|
|
public function scaffoldFormField($title = null, $params = null) |
38
|
|
|
{ |
39
|
|
|
return HiddenField::create($this->getName()); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function decode() |
46
|
|
|
{ |
47
|
|
|
if (!$this->value) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
return json_decode($this->value); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public function decodeArray() |
57
|
|
|
{ |
58
|
|
|
if (!$this->value) { |
59
|
|
|
return []; |
60
|
|
|
} |
61
|
|
|
return json_decode($this->value, JSON_OBJECT_AS_ARRAY); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
public function toArray() |
68
|
|
|
{ |
69
|
|
|
return $this->decodeArray(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return string |
74
|
|
|
*/ |
75
|
|
|
public function pretty() |
76
|
|
|
{ |
77
|
|
|
return json_encode(json_decode($this->value), JSON_PRETTY_PRINT); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
*/ |
83
|
|
|
public function saveInto($dataObject) |
84
|
|
|
{ |
85
|
|
|
if ($this->value && is_array($this->value)) { |
86
|
|
|
$this->value = json_encode($this->value); |
87
|
|
|
} |
88
|
|
|
parent::saveInto($dataObject); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add a value |
93
|
|
|
* |
94
|
|
|
* @link https://stackoverflow.com/questions/7851590/array-set-value-using-dot-notation |
95
|
|
|
* @param string|array $key |
96
|
|
|
* @param string $value |
97
|
|
|
* @return $this |
98
|
|
|
*/ |
99
|
|
|
public function addValue($key, $value) |
100
|
|
|
{ |
101
|
|
|
$currentValue = $this->decodeArray(); |
102
|
|
|
|
103
|
|
|
if (!is_array($key)) { |
104
|
|
|
$key = [$key]; |
105
|
|
|
} |
106
|
|
|
$arr = &$currentValue; |
107
|
|
|
foreach ($key as $idx) { |
108
|
|
|
if (!isset($arr[$idx])) { |
109
|
|
|
$arr[$idx] = []; |
110
|
|
|
} |
111
|
|
|
$arr = &$arr[$idx]; |
112
|
|
|
} |
113
|
|
|
$arr = $value; |
114
|
|
|
return $this->setValue($currentValue); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Internally, the value is always a json string |
119
|
|
|
* |
120
|
|
|
* @param mixed $value |
121
|
|
|
* @param DataObject $record |
|
|
|
|
122
|
|
|
* @param boolean $markChanged |
123
|
|
|
* @return $this |
124
|
|
|
*/ |
125
|
|
|
public function setValue($value, $record = null, $markChanged = true) |
126
|
|
|
{ |
127
|
|
|
if (is_array($value)) { |
128
|
|
|
$value = json_encode($value); |
129
|
|
|
} |
130
|
|
|
return parent::setValue($value, $record, $markChanged); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @inheritDoc |
135
|
|
|
*/ |
136
|
|
|
public function prepValueForDB($value) |
137
|
|
|
{ |
138
|
|
|
if (is_array($value)) { |
139
|
|
|
$value = json_encode($value); |
140
|
|
|
} |
141
|
|
|
return parent::prepValueForDB($value); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* We return false because we can accept array and convert it to string |
146
|
|
|
* @return boolean |
147
|
|
|
*/ |
148
|
|
|
public function scalarValueOnly() |
149
|
|
|
{ |
150
|
|
|
return false; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|