MMOframework
Why AI Playground Libraries Developers Agency
FR EN
Build a project live
Index

Anonymizer

MMO\mongodb · 1 symbol · generated 30 Nov 2026

class

Anonymizer

Anonymizer.php:43

A pseudonymised copy of a database (ISO/IEC 27001 A.8.11, A.8.33): what a developer's machine gets instead of a client's data. Driven by the classification of the entities (Classification): each value of a document is read with the level of its path (Schema::classificationOf), and only personnel and sante values change.

class MMO\mongodb\Anonymizer use MMO\core\classes\tSingleton
Namespace
MMO\mongodb
Uses traits
tSingleton
- a string becomes a pseudonym of the same shape: a letter for a letter (case kept), a digit for a digit, the rest (spaces, dashes, `@`, dots) as it was -- a NIR stays 15 digits, an e-mail address stays one (of no real domain); - `personnel`: word by word, deterministic with the key: `Dupont` gives the same pseudonym in every document of every collection, so what joins on a name, a registration number or an e-mail address still joins; - `sante`: the whole value at once (a description, a lesion's nature): no word of it can be recognised in another; - a date (a BSON date, or a string `YYYY-MM-DD...`) is moved back by the same number of weeks everywhere in the copy: the intervals (an accident within its contract, a stop after the accident) and the days of the week are kept; - what is kept: an enum's value (`risque`, `sexe`, `pays`...), a time (`09:00:00`), numbers, booleans, ids and references (`_id`, `{$ref, $id}`, `mongo://...`), `status`, `ctime`, `mtime`, and whatever is `interne`/`public`; - a collection of no known entity is read as [`$unclassified`](https://mmoframework.fr/en/documentation/mmo.mongodb/php/Anonymizer.md#anonymizer-unclassified) (`personnel`: every string pseudonymised); - a journal ([`LogQuery`](https://mmoframework.fr/en/documentation/mmo.core/php/entities/LogQuery.md#logquery): it holds the values written, in clear) and the collections of [`$empty`](https://mmoframework.fr/en/documentation/mmo.mongodb/php/Anonymizer.md#anonymizer-empty) are copied empty, with their indexes; the documents matched by [`$keep`](https://mmoframework.fr/en/documentation/mmo.mongodb/php/Anonymizer.md#anonymizer-keep) (a developer's own account) are copied as they are. The key ([`$keyFile`](https://mmoframework.fr/en/documentation/mmo.mongodb/php/Anonymizer.md#anonymizer-keyfile), 32 random bytes written on first use, readable by its owner only) stays on the machine that anonymises: without it a pseudonym cannot be traced back, with it the same names give the same pseudonyms from one copy to the next. It is never written into the copy, which holds a marker instead ([`MARKER`](https://mmoframework.fr/en/documentation/mmo.mongodb/php/Anonymizer.md#anonymizer-marker)). The copy goes into a NEW database: an existing one is refused, unless it is itself an anonymised copy (its marker) and $replace is given. The source is only read.
$report = Anonymizer::Shared()->anonymize($repository, 'kuehnenagel', 'kuehnenagel_anon'); $report['collections']['salaries']['pseudonymised']; // the strings replaced in salaries

Index

KEEP_KEYSthe keys kept whatever their level: ids, references, the framework's own fields
MARKERthe collection of the copy that says it is one: from what, when, with which key (its fingerprint)
TRIESthe tries at a pseudonym that matches the pattern of its schema (a NIR's first digit)
VERSION
actions
batch
empty
keep
key
keyFile
unclassified
words
action($path, $collection, $schema)What is done to the values of a path (cached per collection and path, its indexes as 0): keep, date (moved), words (personnel), text (sante), with the pattern of its schema
anonymize($repository, $from, $to, $replace, $dryRun, $overwrite, $progress)Copy $from into $to, pseudonymised.
copyCollection($repository, $from, $to, $name, $dryRun)
document($document, $collection, $schema, $stats)A document pseudonymised (value for each of its values)
Instantiate($config) static(Re)create the shared instance immediately, configured with $config merged into the class's existing config.
IsShared() static
key()
keyId()
marker($repository, $database)
pseudonym($value, $action, $pattern)
setKey($key)
shape($original, $bytes)
Shared() staticGet the shared instance, creating (and configuring) it on first call.
shiftDays()
shiftString($value)
stream($seed, $length)
text($value)
Unshare($serialize) staticRemove the shared instance, so the next Shared() call creates a fresh one.
value($value, $path, $collection, $schema, $stats)
words($value)

Constants4

KEEP_KEYS

the keys kept whatever their level: ids, references, the framework's own fields

const KEEP_KEYS = ['_id', 'id', '$ref', '$id', '$db', 'status', 'ctime', 'mtime', 'eid']

MARKER

the collection of the copy that says it is one: from what, when, with which key (its fingerprint)

const MARKER = '_anonymized'

TRIES

the tries at a pseudonym that matches the pattern of its schema (a NIR's first digit)

const TRIES = 64

VERSION

const VERSION = 1

Properties8

actions

#[MMO\core\attributes\Serialize(false)] protected array $actions = []
Type
array

batch

int $batch = 500
Type
int

empty

array $empty = []
Type
array

keep

array $keep = []
Type
array

key

#[MMO\core\attributes\Serialize(false)] protected ?string $key = null
Type
null | string

keyFile

string $keyFile = '{configs}/keys/anonymize.key'
Type
string

unclassified

string $unclassified = 'personnel'
Type
string

words

#[MMO\core\attributes\Serialize(false)] protected array $words = []
Type
array

Methods20

action($path, $collection, $schema)

Anonymizer.php:327

What is done to the values of a path (cached per collection and path, its indexes as 0): keep, date (moved), words (personnel), text (sante), with the pattern of its schema

protected function action(array $path, string $collection, ?MMO\core\schemas\Schema $schema): array
$patharray
$collectionstring
$schemanull | Schema
returnsarraystring, 1: ?string}

anonymize($repository, $from, $to, $replace, $dryRun, $overwrite, $progress)

Anonymizer.php:124

Copy $from into $to, pseudonymised.

function anonymize( MMO\mongodb\MongoRepository $repository, string $from, string $to, bool $replace = false, bool $dryRun = false, bool $overwrite = false, ?callable $progress = null ): array
$repositoryMongoRepositorythe server, and the entities of the collections (their classification)
$fromstringthe database read (never written)
$tostringthe database written: a new one, or an anonymised copy with $replace
$replace = falsebooldrop $to first when it is an anonymised copy (its marker); a database without one is refused
$dryRun = falseboolread and count, write nothing
$overwrite = falsebooldrop $to first whatever it holds: BackupTask's restore here, which replaced the local database before it was anonymised; never from the command line
$progress = nullnull | callablefn(string $collection, array $stats) after each collection
returnsarraystring, to: string, dryRun: bool, documents: int, seconds: float, collections: array<string, array<string, mixed>>}
throwswhen $to is $from, or a database that may not be written

copyCollection($repository, $from, $to, $name, $dryRun)

Anonymizer.php:180
protected function copyCollection( MMO\mongodb\MongoRepository $repository, string $from, string $to, string $name, bool $dryRun ): array
$repositoryMongoRepository
$fromstring
$tostring
$namestring
$dryRunbool
returnsarraymixed> documents, kept (copied as they are), emptied, left (the documents of a collection copied empty), unclassified (no entity), pseudonymised (strings), shifted (dates), seconds

document($document, $collection, $schema, $stats)

Anonymizer.php:245

A document pseudonymised (value for each of its values)

function document( array $document, string $collection, ?MMO\core\schemas\Schema $schema, array &$stats = [] ): array
$documentarray
$collectionstring
$schemanull | Schemaits entity's, null for a collection of no known entity
$stats = []array
returnsarraymixed>
Anonymizer::Shared()->document(['nom' => 'Dupont'], 'salaries', Salarie::Schema(), $stats)['nom']; // 'Kqzlmw'

Instantiate($config)

statictSingleton.php:66

(Re)create the shared instance immediately, configured with $config merged into the class's existing config.

static function Instantiate(array $config = []): staticUnlike a plain [`Shared()`](https://mmoframework.fr/en/documentation/mmo.mongodb/php/Anonymizer.md#anonymizer-shared) call (which lazily builds the instance from whatever config was already loaded), this lets you supply/override config right before the instance is built.
$config = []arrayconfig values merged in before the instance is constructed
returnsstaticthe newly (re)created shared instance
MyService::Instantiate(['greeting' => 'yo'])->greeting; // 'yo'

IsShared()

statictSingleton.php:54
static function IsShared(): bool
returnsbooltrue if the shared instance has already been created (a call to Shared() would not need to construct a new one)
MyService::IsShared(); // false until the first MyService::Shared() call

key()

Anonymizer.php:86
function key(): string
returnsstringthe key of $keyFile, written (32 random bytes, readable by its owner only) when missing
throwswhen it can be neither read nor written

keyId()

Anonymizer.php:101
function keyId(): string
returnsstringwhat identifies the key in a marker, not the key: the start of its hash

marker($repository, $database)

Anonymizer.php:164
function marker(MMO\mongodb\MongoRepository $repository, string $database): ?array
$repositoryMongoRepository
$databasestring
returnsnull | arraymixed>|null the marker of an anonymised copy, null for any other database

pseudonym($value, $action, $pattern)

Anonymizer.php:309
protected function pseudonym(string $value, string $action, ?string $pattern): string
$valuestring
$actionstringwords or text
$patternnull | stringthe pattern of its schema: the pseudonym of a value that matches it matches it too (another try, deterministic, until one does), so the copy validates as the source did
returnsstring

shape($original, $bytes)

Anonymizer.php:401
protected function shape(string $original, string $bytes): string
$originalstring
$bytesstringone per character of $original
returnsstringa letter for each letter of $original (its case kept, unaccented), a digit for each digit, the rest kept

Shared()

statictSingleton.php:22

Get the shared instance, creating (and configuring) it on first call.

static function Shared(): static
returnsstatic
MyService::Shared()->greeting; // 'hello'

shiftDays()

Anonymizer.php:106
function shiftDays(): int
returnsintthe days every date of a copy is moved back by: whole weeks, 1 to 52, the key's

shiftString($value)

Anonymizer.php:378
function shiftString(string $value): string
$valuestringa date YYYY-MM-DD, with or without a time
returnsstringthe date moved back by shiftDays, its time and form kept

stream($seed, $length)

Anonymizer.php:390
protected function stream(string $seed, int $length): string
$seedstring
$lengthint
returnsstring$length bytes derived from the key and $seed

text($value)

Anonymizer.php:370
function text(string $value): string
$valuestring
returnsstring$value replaced as a whole: same shape, no word recognisable from one value to another

Unshare($serialize)

statictSingleton.php:40

Remove the shared instance, so the next Shared() call creates a fresh one.

static function Unshare(bool $serialize = true): bool
See
serialize) and kept as the class's config, so the next instance created picks up where this one left off; if false, the instance is simply discarded and the next one starts from the class's original config
$serialize = trueboolif true (the default) and an instance currently exists, it is serialized first (via
returnsbooltrue if an instance actually existed and was removed
MyService::Unshare(); // true if MyService::Shared() had already been called; the next call rebuilds it

value($value, $path, $collection, $schema, $stats)

Anonymizer.php:261
protected function value( mixed $value, array $path, string $collection, ?MMO\core\schemas\Schema $schema, array &$stats ): mixed
$valuemixed
$patharraythe keys down to it
$collectionstring
$schemanull | Schema
$statsarray
returnsmixedthe value pseudonymised, or as it was

words($value)

Anonymizer.php:355
function words(string $value): string
$valuestring
returnsstring$value, each word replaced by its pseudonym (the same word, the same pseudonym, whatever its case)
Anonymizer::Shared()->words('Jean DUPONT'); // 'Kqal ZMWPOX': Dupont's pseudonym, in capitals