1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Inet Data Anonymization |
4
|
|
|
* |
5
|
|
|
* PHP Version 5.3 -> 7.0 |
6
|
|
|
* |
7
|
|
|
* @author Emmanuel Dyan |
8
|
|
|
* @author Rémi Sauvat |
9
|
|
|
* @copyright 2005-2015 iNet Process |
10
|
|
|
* |
11
|
|
|
* @package inetprocess/neuralyzer |
12
|
|
|
* |
13
|
|
|
* @license GNU General Public License v2.0 |
14
|
|
|
* |
15
|
|
|
* @link http://www.inetprocess.com |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Inet\Neuralyzer; |
19
|
|
|
|
20
|
|
|
use Inet\Neuralyzer\Exception\InetAnonGuesserException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Guesser to map field type to Faker Class |
24
|
|
|
*/ |
25
|
|
|
class Guesser implements GuesserInterface |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the version of your guesser |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
*/ |
33
|
6 |
|
public function getVersion() |
34
|
|
|
{ |
35
|
6 |
|
return '1.0.0b'; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns an array of fieldName => Faker class |
40
|
|
|
* |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
7 |
|
public function getColsNameMapping() |
44
|
|
|
{ |
45
|
|
|
// can contain regexp |
46
|
|
|
return array( |
47
|
|
|
// Adress and coordinates |
48
|
|
|
'.*\..*street.*' => array( |
49
|
7 |
|
'method' => 'streetAddress', |
50
|
7 |
|
), |
51
|
|
|
'.*\..*postalcode.*' => array( |
52
|
7 |
|
'method' => 'postcode', |
53
|
7 |
|
), |
54
|
|
|
'.*\..*city.*' => array( |
55
|
7 |
|
'method' => 'city', |
56
|
7 |
|
), |
57
|
|
|
'.*\..*state.*' => array( |
58
|
7 |
|
'method' => 'state', |
59
|
7 |
|
), |
60
|
|
|
'.*\..*country.*' => array( |
61
|
7 |
|
'method' => 'country', |
62
|
7 |
|
), |
63
|
|
|
'.*\..*phone.*' => array( |
64
|
7 |
|
'method' => 'phoneNumber', |
65
|
7 |
|
), |
66
|
|
|
// Internet |
67
|
|
|
'.*\.email_address' => array( |
68
|
7 |
|
'method' => 'email', |
69
|
7 |
|
), |
70
|
|
|
'.*\.url' => array( |
71
|
7 |
|
'method' => 'url', |
72
|
7 |
|
), |
73
|
|
|
// Text |
74
|
|
|
'.*\.(comments|description)' => array( |
75
|
7 |
|
'method' => 'sentence', |
76
|
7 |
|
'params' => array(20), |
77
|
7 |
|
), |
78
|
|
|
// Person |
79
|
|
|
'.*\.first_name' => array( |
80
|
7 |
|
'method' => 'firstName', |
81
|
7 |
|
), |
82
|
|
|
'.*\.last_name' => array( |
83
|
7 |
|
'method' => 'lastName', |
84
|
7 |
|
), |
85
|
7 |
|
); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retruns an array of fieldType => Faker class |
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
7 |
|
public function getColsTypeMapping() |
94
|
|
|
{ |
95
|
|
|
return array( |
96
|
|
|
// Strings |
97
|
|
|
'char' => array( |
98
|
7 |
|
'method' => 'sentence', |
99
|
7 |
|
'params' => array(8), |
100
|
7 |
|
), |
101
|
|
|
'varchar' => array( |
102
|
7 |
|
'method' => 'sentence', |
103
|
7 |
|
'params' => array(8), |
104
|
7 |
|
), |
105
|
|
|
// Text & Blobs |
106
|
|
|
'text' => array( |
107
|
7 |
|
'method' => 'sentence', |
108
|
7 |
|
'params' => array(20), |
109
|
7 |
|
), |
110
|
|
|
'blob' => array( |
111
|
7 |
|
'method' => 'sentence', |
112
|
7 |
|
'params' => array(20), |
113
|
7 |
|
), |
114
|
|
|
'longtext' => array( |
115
|
7 |
|
'method' => 'sentence', |
116
|
7 |
|
'params' => array(70), |
117
|
7 |
|
), |
118
|
|
|
'longblob' => array( |
119
|
7 |
|
'method' => 'sentence', |
120
|
7 |
|
'params' => array(70), |
121
|
7 |
|
), |
122
|
|
|
// DateTime |
123
|
|
|
'date' => array( |
124
|
7 |
|
'method' => 'date', |
125
|
7 |
|
'params' => array('Y-m-d', 'now') |
126
|
7 |
|
), |
127
|
|
|
'datetime' => array( |
128
|
7 |
|
'method' => 'date', |
129
|
7 |
|
'params' => array('Y-m-d H:i:s', 'now') |
130
|
7 |
|
), |
131
|
|
|
'time' => array( |
132
|
7 |
|
'method' => 'time', |
133
|
7 |
|
), |
134
|
|
|
// Integer |
135
|
|
|
'tinyint' => array( |
136
|
7 |
|
'method' => 'randomNumber', |
137
|
7 |
|
'params' => array(2), |
138
|
7 |
|
), |
139
|
|
|
'smallint' => array( |
140
|
7 |
|
'method' => 'randomNumber', |
141
|
7 |
|
'params' => array(4), |
142
|
7 |
|
), |
143
|
|
|
'mediumint' => array( |
144
|
7 |
|
'method' => 'randomNumber', |
145
|
7 |
|
'params' => array(6), |
146
|
7 |
|
), |
147
|
|
|
'int' => array( |
148
|
7 |
|
'method' => 'randomNumber', |
149
|
7 |
|
'params' => array(9), |
150
|
7 |
|
), |
151
|
|
|
'bigint' => array( |
152
|
7 |
|
'method' => 'randomNumber', |
153
|
7 |
|
'params' => array(15), |
154
|
7 |
|
), |
155
|
|
|
// Decimal |
156
|
|
|
'float' => array( |
157
|
7 |
|
'method' => 'randomFloat', |
158
|
7 |
|
'params' => array(2, 0, 999999), |
159
|
7 |
|
), |
160
|
|
|
'decimal' => array( |
161
|
7 |
|
'method' => 'randomFloat', |
162
|
7 |
|
'params' => array(2, 0, 999999), |
163
|
7 |
|
), |
164
|
|
|
'double' => array( |
165
|
7 |
|
'method' => 'randomFloat', |
166
|
7 |
|
'params' => array(2, 0, 999999), |
167
|
7 |
|
), |
168
|
7 |
|
); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Will map cols first by looking for field name then by looking for field type |
173
|
|
|
* if the first returned nothing |
174
|
|
|
* |
175
|
|
|
* @param string $table |
176
|
|
|
* @param string $name |
177
|
|
|
* @param string $type |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
6 |
|
public function mapCol($table, $name, $type, $len) |
182
|
|
|
{ |
183
|
|
|
// Try to find by colsName |
184
|
6 |
|
$colsName = $this->getColsNameMapping(); |
185
|
6 |
|
foreach ($colsName as $colRegex => $params) { |
186
|
6 |
|
preg_match("/^$colRegex\$/", $table. '.' . $name, $matches); |
187
|
6 |
|
if (!empty($matches)) { |
188
|
|
|
return $params; |
189
|
|
|
} |
190
|
6 |
|
} |
191
|
|
|
|
192
|
|
|
// Hardcoded types |
193
|
6 |
|
if ($type === 'enum') { |
194
|
|
|
return array( |
195
|
|
|
'method' => 'randomElement', |
196
|
|
|
'params' => explode(',', $len), |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
// Try to find by fieldType |
201
|
6 |
|
$colsType = $this->getColsTypeMapping(); |
202
|
6 |
|
if (!array_key_exists($type, $colsType)) { |
203
|
|
|
throw new InetAnonGuesserException("Can't guess the type $type"); |
204
|
|
|
} |
205
|
|
|
|
206
|
6 |
|
return $colsType[$type]; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|