PHP code example of prowebce / database-anonymizer-bundle
1. Go to this page and download the library: Download prowebce/database-anonymizer-bundle 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/ */
prowebce / database-anonymizer-bundle example snippets
use Doctrine\ORM\Mapping as ORM;
use WebnetFr\DatabaseAnonymizerBundle\Annotation as Anonymize;
/**
* @ORM\Table(name="orders")
* @ORM\Entity
*
* This annotation marks the entities to anonymize.
* @Anonymize\Table()
*/
class Orders
{
/**
* @ORM\Column(name="id", type="integer", nullable=false, options={"unsigned"=true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* @ORM\Column(name="address", type="string", length=256, nullable=true)
* @Anonymize\Field(generator="faker", formatter="address")
*/
public $address;
/**
* @ORM\Column(name="zip_code", type="string", length=10, nullable=true)
* @Anonymize\Field(generator="faker", formatter="postcode")
*/
public $zipCode;
/**
* @ORM\Column(name="comment", type="text", length=0, nullable=true)
* @Anonymize\Field(generator="faker", formatter="text", arguments={300})
*/
public $comment;
/**
* @ORM\Column(name="created_at", type="datetime", nullable=true)
* @Anonymize\Field(generator="faker", formatter="dateTime", date_format="Y-m-d H:i:s")
*/
public $createdAt;
/**
* @ORM\Column(name="comment_history", type="array", nullable=true)
*
* A custom generator with its custom arguments.
* @Anonymize\Field(generator="comment_history", max_messages_nb=5)
*/
public $commentHistory;
}
use Doctrine\ORM\Mapping as ORM;
/**
* Users' orders.
*
* @ORM\Table(name="orders")
* @ORM\Entity
*/
class Orders
{
/**
* History of all user's comments.
* @var string[]
*
* @ORM\Column(name="comments", type="array", nullable=true)
*/
public $comments;
}
namespace App\DatabaseAnonymizer;
use Faker\Factory;
use WebnetFr\DatabaseAnonymizer\Exception\UnsupportedGeneratorException;
use WebnetFr\DatabaseAnonymizer\Generator\GeneratorInterface;
use WebnetFr\DatabaseAnonymizer\GeneratorFactory\GeneratorFactoryInterface;
/**
* The factory that creates a generator out of provided configuration.
* It is a Symfony service.
*/
class CommentHistoryGeneratorFactory implements GeneratorFactoryInterface
{
/**
* @param array $config
* An array of the configuration for field to anonymize. It contains
* all specified entries, like "generator", "unique", "date_format",
* "my_custom_entry", etc.
*
* @throws \WebnetFr\DatabaseAnonymizer\Exception\UnsupportedGeneratorException
* The factory MUST throw "UnsupportedGeneratorException" if it is
* impossible to create the generator for provided configuration.
*
* @return GeneratorInterface
*/
public function getGenerator($config): GeneratorInterface
{
// Check if the field should be anonymized with "comment_history" encoder.
$generatorKey = $config['generator'];
if ('comment_history' !== $generatorKey) {
throw new UnsupportedGeneratorException($generatorKey.' generator is not known');
}
// Retrieve any configuration values you need.
$locale = $config['locale'] ?? 'en_US';
$minMessagesNb = $config['min_messages_nb'] ?? 1;
$maxMessagesNb = $config['max_messages_nb'] ?? 10;
// Create and configure generator.
// Usually there is ONE generator instance for ONE field to anonymize
// because there could be different config values for differet fields
// even though these fields are anoymized with the same
// "comment_history" generator.
$faker = Factory::create($locale);
$generator = new CommentHistoryGenerator($faker);
$generator->setMinMessagesNb($minMessagesNb);
$generator->setMaxMessagesNb($maxMessagesNb);
return $generator;
}
}
namespace App\DatabaseAnonymizer;
use Faker\Generator;
use WebnetFr\DatabaseAnonymizer\Generator\GeneratorInterface;
/**
* Anonmyizer generator that generates comment history.
*/
class CommentHistoryGenerator implements GeneratorInterface
{
/**
* Faker generator
* @var Generator
*/
private $faker;
/**
* Minimum number of comments in history.
* @var int
*/
private $minMessagesNb = 1;
/**
* Maximum number of comments in history.
* @var int
*/
private $maxMessagesNb = 10;
// Constructors, setters.
/**
* Generates new random value for each line.
*/
public function generate()
{
$comments = [];
foreach (range(0, mt_rand(1, 10)) as $i) {
$comments[] = $this->faker->realText();
};
return serialize($comments);
}
}
bash
php bin/console webnet-fr:anonymizer:anonymize --connection=<name of connection> --config=<config file path>
bash
php bin/console webnet-fr:anonymizer:anonymize --connection=<name of connection>