masking
Table of Contents
Namespaces
Functions
- maskCreditCard() : int
- Replaces a value with a random, Luhn-valid credit-card number.
- maskDatetime() : string
- Replaces a value with a random date/time between two bounds, formatted.
- maskDecimal() : float
- Replaces a value with a random decimal in `[lower, upper]`.
- maskDocument() : array<string|int, mixed>
- Applies a list of attribute masking rules to a single document.
- maskDocumentList() : array<string|int, mixed>
- Applies the masking rules to every element of a JSON array (a PHP list).
- maskDocumentNode() : array<string|int, mixed>
- Walks an object: masks the matching leaves and descends into nested objects and arrays.
- maskEmail() : string
- Replaces a value with a random, non-routable email address.
- maskInteger() : int
- Replaces a value with a random integer in `[lower, upper]`.
- maskPhone() : string
- Replaces a phone number with a random one of the same shape.
- maskRandom() : mixed
- Replaces a leaf value with a random value of the **same kind**.
- maskRandomString() : mixed
- Replaces a string value with a random anonymized string.
- maskValue() : mixed
- Applies a single masker to a value.
- maskXifyFront() : mixed
- Masks the front of each word with `x`, keeping a few trailing characters.
- maskZip() : string
- Replaces a zip / postal code with a random one of the same shape.
- randomAlphaNumeric() : string
- Returns a random alphanumeric string of the given length.
- resolveMaskingRule() : array<string, mixed>|null
- Returns the first masking rule (in declaration order) that targets a leaf.
Functions
maskCreditCard()
Replaces a value with a random, Luhn-valid credit-card number.
maskCreditCard([mixed $value = null ]) : int
Implements the conventional creditCard masker: it returns a random 16-digit
number (as an integer) whose check digit satisfies the
Luhn algorithm. The
original value is never reflected in the output.
Parameters
- $value : mixed = null
-
The original value (ignored — replaced wholesale).
Tags
Return values
int —A Luhn-valid 16-digit number.
maskDatetime()
Replaces a value with a random date/time between two bounds, formatted.
maskDatetime([mixed $value = null ][, string $begin = '1970-01-01T00:00:00.000' ][, string $end = '' ][, string $format = '' ]) : string
Implements the conventional datetime masker: a random instant is picked in
[begin, end] and rendered with format. The format uses DATE_FORMAT()-style
tokens — the common ones are supported: %yyyy, %yy,
%mm, %m, %dd, %d, %hh, %h, %ii, %i, %ss, %s, %fff and
%%. When format is empty (the default), an empty string is returned.
Parameters
- $value : mixed = null
-
The original value (ignored — replaced wholesale).
- $begin : string = '1970-01-01T00:00:00.000'
-
Earliest instant (ISO 8601). Defaults to the epoch.
- $end : string = ''
-
Latest instant (ISO 8601). Empty means "now".
- $format : string = ''
-
The DATE_FORMAT-style pattern. Empty returns "".
Tags
Return values
stringmaskDecimal()
Replaces a value with a random decimal in `[lower, upper]`.
maskDecimal([mixed $value = null ][, float $lower = -1.0 ][, float $upper = 1.0 ][, int $scale = 2 ]) : float
Implements the conventional decimal masker: the value is replaced whatever
its original type (string, boolean and null included), rounded to scale
fraction digits. The bounds are inclusive; they are swapped if given in the
wrong order.
Parameters
- $value : mixed = null
-
The original value (ignored — replaced wholesale).
- $lower : float = -1.0
-
Smallest value to return.
- $upper : float = 1.0
-
Largest value to return.
- $scale : int = 2
-
Maximum number of fraction digits.
Tags
Return values
floatmaskDocument()
Applies a list of attribute masking rules to a single document.
maskDocument(array<string|int, mixed> $doc, array<string|int, mixed> $maskings[, array<int, string> $protectedAttributes = [] ]) : array<string|int, mixed>
This is a portable, self-contained masking engine. Each rule is
{ "path": …, "type": <masker>, …params }; the supported path forms are:
"name"— a leaf attributenameat the top level ;"a.b"— the exact nested patha→b(through objects only) ;".name"— every leaf attribute namedname, at any depth ;"*"— every leaf attribute ;"`a.b`"— a literal attribute name containing dots (backtick/tick quoted).
A leaf is a value that is null, a scalar or a JSON array; objects are
descended into. When a matched leaf is an array, the masker is applied to its
elements individually (see maskValue()). When several rules match the
same leaf, the first one in the list wins.
The attributes named in $protectedAttributes are never masked at the
top level, even by a * rule — use this to preserve identity fields. The
default is an empty list (nothing protected), so the engine stays agnostic of
any particular data store: supply the identity fields of your model yourself
(e.g. ['_key','_id','_rev','_from','_to'] for ArangoDB, ['_id'] for
MongoDB, ['id'] for a relational row).
Parameters
- $doc : array<string|int, mixed>
-
The document (decoded JSON object).
- $maskings : array<string|int, mixed>
-
The list of rules for this collection.
- $protectedAttributes : array<int, string> = []
-
Top-level attribute names never masked. Default: none.
Tags
Return values
array<string|int, mixed> —The masked document.
maskDocumentList()
Applies the masking rules to every element of a JSON array (a PHP list).
maskDocumentList(array<string|int, mixed> $list, array<string|int, mixed> $maskings, int $depth[, array<int, string> $protectedAttributes = [] ]) : array<string|int, mixed>
Each element is handled on its own: a nested list recurses through this helper, a nested object is walked by maskDocumentNode(), and a bare scalar is left untouched (it is not a keyed attribute, so no path rule can target it directly — only the masker applied to the parent leaf reaches it). This mirrors the common "array elements are masked individually" rule.
Parameters
- $list : array<string|int, mixed>
-
The list to walk.
- $maskings : array<string|int, mixed>
-
The list of rules for this collection.
- $depth : int
-
The current depth (protected attributes are top-level only).
- $protectedAttributes : array<int, string> = []
-
Top-level attribute names never masked. Default: none.
Tags
Return values
array<string|int, mixed> —The masked list.
maskDocumentNode()
Walks an object: masks the matching leaves and descends into nested objects and arrays.
maskDocumentNode(array<string|int, mixed> $node, array<string|int, mixed> $maskings, string|null $exactPath, int $depth[, array<int, string> $protectedAttributes = [] ]) : array<string|int, mixed>
A leaf (scalar, null or a JSON array) is masked by the first matching rule
(resolveMaskingRule()); an array with no matching rule is walked deeper
(maskDocumentList()); a nested object recurses through this helper. The
attributes named in $protectedAttributes are never masked at the top
level ($depth === 0); the default is an empty list (nothing protected).
Parameters
- $node : array<string|int, mixed>
-
The object to walk.
- $maskings : array<string|int, mixed>
-
The list of rules for this collection.
- $exactPath : string|null
-
The dotted path of
$node(null once an array has been crossed). - $depth : int
-
The current depth (protected attributes are top-level only).
- $protectedAttributes : array<int, string> = []
-
Top-level attribute names never masked. Default: none.
Tags
Return values
array<string|int, mixed> —The masked object.
maskEmail()
Replaces a value with a random, non-routable email address.
maskEmail([mixed $value = null ]) : string
Implements the conventional email masker: the result has the shape
AAAA.BBBB@CCCC.invalid with random parts. The original value is never
reflected in the output (the content is replaced wholesale, not hashed
reversibly), so it is safe for anonymization. The .invalid TLD is reserved
(RFC 2606) and never resolves.
Parameters
- $value : mixed = null
-
The original value (ignored — replaced wholesale).
Tags
Return values
string —The anonymized email address.
maskInteger()
Replaces a value with a random integer in `[lower, upper]`.
maskInteger([mixed $value = null ][, int $lower = -100 ][, int $upper = 100 ]) : int
Implements the conventional integer masker: the value is replaced whatever
its original type (string, boolean and null included). The bounds are
inclusive; they are swapped if given in the wrong order.
Parameters
- $value : mixed = null
-
The original value (ignored — replaced wholesale).
- $lower : int = -100
-
Smallest value to return.
- $upper : int = 100
-
Largest value to return.
Tags
Return values
intmaskPhone()
Replaces a phone number with a random one of the same shape.
maskPhone(mixed $value[, string $default = '+1234567890' ]) : string
Implements the conventional phone masker: each digit is replaced by a
random digit, each letter by a random letter (case kept), and every other
character is left unchanged. When the value is not a string, the default
fallback is used.
Parameters
- $value : mixed
-
The original value.
- $default : string = '+1234567890'
-
Fallback when the value is not a string.
Tags
Return values
stringmaskRandom()
Replaces a leaf value with a random value of the **same kind**.
maskRandom(mixed $value) : mixed
Implements the conventional random masker:
- strings → a random string (see maskRandomString()) ;
- integers → a random integer between -1000 and 1000 ;
- floats → a random decimal between -1000 and 1000 ;
- booleans → a random boolean ;
null→ staysnull.
Parameters
- $value : mixed
-
The original value.
Tags
Return values
mixed —The randomized value, type-preserved.
maskRandomString()
Replaces a string value with a random anonymized string.
maskRandomString(mixed $value) : mixed
Implements the conventional randomString masker: only string values are
modified — any other type (number, boolean, null) is returned unchanged.
The replacement keeps the original length (with a minimum of 1).
The replacement is random, not a reversible hash of the input — the goal is anonymization, not byte-compatibility with any external tool.
Parameters
- $value : mixed
-
The original value.
Tags
Return values
mixed —The masked string, or the value unchanged when not a string.
maskValue()
Applies a single masker to a value.
maskValue(string $type, mixed $value[, array<string|int, mixed> $params = [] ]) : mixed
This is the dispatcher in front of the per-type maskers (maskEmail(),
maskXifyFront(), …). When the value is a JSON array (a PHP list),
the masker is applied to each element individually — sub-arrays recurse,
nested objects (associative arrays) are left untouched — mirroring the
common "array elements are masked individually" rule. Scalars and
null are masked directly.
Parameters
- $type : string
-
The masker name (Masker).
- $value : mixed
-
The value to mask.
- $params : array<string|int, mixed> = []
-
The masker parameters (e.g.
unmaskedLength,lower).
Tags
Return values
mixed —The masked value.
maskXifyFront()
Masks the front of each word with `x`, keeping a few trailing characters.
maskXifyFront(mixed $value[, int $unmaskedLength = 2 ][, bool $hash = false ][, int $seed = 0 ]) : mixed
Implements the conventional xifyFront masker. Within each word (a run of
alphanumeric, _ or - characters) every character except the last
unmaskedLength ones is replaced by x; words no longer than
unmaskedLength are left untouched. Every other character (spaces,
punctuation) becomes a blank. Non-string values (boolean, number) become the
fixed string "xxxx", and null stays null.
Parameters
- $value : mixed
-
The original value.
- $unmaskedLength : int = 2
-
How many trailing characters of each word to keep.
- $hash : bool = false
-
Append a short hash to reduce collisions.
- $seed : int = 0
-
Secret used by the hash (0 = unseeded).
Tags
Return values
mixed —The masked string, "xxxx" for non-strings, or null.
maskZip()
Replaces a zip / postal code with a random one of the same shape.
maskZip(mixed $value[, string $default = '12345' ]) : string
Implements the conventional zip masker: each digit becomes a random digit,
each letter a random letter (case kept), other characters are left
unchanged. When the value is not a string, the default fallback is used.
Parameters
- $value : mixed
-
The original value.
- $default : string = '12345'
-
Fallback when the value is not a string.
Tags
Return values
stringrandomAlphaNumeric()
Returns a random alphanumeric string of the given length.
randomAlphaNumeric(int $length) : string
A small building block shared by the string-producing maskers (maskEmail(), maskRandomString(), maskRandom()). It is not meant to be cryptographically reversible — its only purpose is to replace a value with anonymized, readable characters.
Parameters
- $length : int
-
The desired length (clamped to a minimum of 1).
Tags
Return values
stringresolveMaskingRule()
Returns the first masking rule (in declaration order) that targets a leaf.
resolveMaskingRule(array<string|int, mixed> $maskings, string $key, string|null $exactPath) : array<string, mixed>|null
A leaf is identified by its attribute name ($key) and its exact dotted path
($exactPath, which is null once an array has been crossed, disabling
exact-path matching). The supported path forms are:
"*"— matches every leaf ;"`a.b`"— a backtick/tick quoted literal attribute name ;".name"— any leaf whose attribute name isname, at any depth ;"a.b"— the exact dotted path.
The first matching rule wins, so a more specific rule listed earlier shadows a broader one listed later.
Parameters
- $maskings : array<string|int, mixed>
-
The list of rules for this collection.
- $key : string
-
The attribute name of the leaf.
- $exactPath : string|null
-
The exact dotted path of the leaf, or null.
Tags
Return values
array<string, mixed>|null —The matching rule, or null.