forked from globalis-ms/mysql-data-anonymizer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.php
53 lines (40 loc) · 2.14 KB
/
example.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
require './vendor/autoload.php';
use Globalis\MysqlDataAnonymizer\Anonymizer;
$anonymizer = new Anonymizer();
// Describe `users` table.
$anonymizer->table('users', function ($table) {
// Specify a primary key of the table. An array should be passed in for composite key.
$table->primary('id');
// Set a global filter to the queries.
// Only string is accepted so you need to write down the comlete WHERE statement here.
$table->globalWhere('email4 != email5 AND id != 10');
// Replace with static data.
$table->column('email1')->replaceWith('[email protected]');
$table->column('email2')->replaceWith('email_#row#@example.com');
// To replace with dynamic data a $generator is needed.
// By default, a fzaninotto/Faker generator will be used.
// Any generator object can be set like that - `$anonymizer->setGenerator($generator);`
$table->column('email3')->replaceWith(function ($generator) {
return $generator->email;
});
// Use `where` to leave some data untouched.
// If you don't list a column here, it will be left untouched too.
$table->column('email4')->where('ID != 1')->replaceWith(function ($generator) {
return $generator->unique()->email;
});
// Use the values of current row to update a field
// This is a position sensitive operation, so the value of field 'email4' here is the updated value.
// So if you put this line before the previous one, the value of 'email4' here would be the valeu of 'email4' before update.
$table->column('email5')->replaceByFields(function ($rowData, $generator) {
return $rowData['email4'];
});
// Here we assume that there is a foreign key in the table 'class' on the column 'user_id'.
// To make sure 'user_id' get updated when we update 'id', use function 'synchronizeColumn'.
$table->column('id')->replaceWith(function ($generator) {
return $generator->unique()->uuid;
})->synchronizeColumn(['user_id', 'class']);
});
$anonymizer->run();
echo 'Anonymization has been completed!';