PHP code example of n3xt0r / mysql-data-anonymizer
1. Go to this page and download the library: Download n3xt0r/mysql-data-anonymizer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
obalis\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');
// Add a global filter to the queries.
// Only string is accepted so you need to write down the complete WHERE statement here.
$table->globalWhere('email4 != email5 AND id != 10');
// Replace with static data.
$table->column('email1')->replaceWith('[email protected]');
// Use #row# template to get "[email protected]", "[email protected]", "[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 for a specific column.
// 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) {
return strtolower($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!';
namespace Globalis\MysqlDataAnonymizer\Helpers; //Default namespace, should always use this one
class StrHelper //Class name needs to be the same as file name
{
public static function toLower($string)
{
return strtolower($string);
}
}
obalis\MysqlDataAnonymizer\Anonymizer;
use Globalis\MysqlDataAnonymizer\Helpers;
$anonymizer = new Anonymizer();
$anonymizer->table('users', function ($table) {
$table->primary('id');
$table->column('name')->replaceByFields(function ($rowData, $generator) {
return Helpers\StrHelper::toLower(($rowData['name']));
});
}
namespace Globalis\MysqlDataAnonymizer\Provider; //Default namespace, should always use this one
class EnumProvider extends \Faker\Provider\Base //Class name needs to be the same as file name, and provider classes need to extend \Faker\Provider\Base
{
//This simple method returns a fruit randomly from the list
public function fruit()
{
$enum = ['apple', 'orange', 'banana'];
return $enum[rand(0, 2)];
}
}
obalis\MysqlDataAnonymizer\Anonymizer;
$anonymizer = new Anonymizer();
$anonymizer->table('users', function ($table) {
$table->primary('id');
$table->column('favorite_fruit')->replaceWith(function ($generator) {
return $generator->fruit;
});
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.