Oihana Php Masking

masking

Table of Contents

Namespaces

enums

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
throws
RandomException
example
use function oihana\masking\maskCreditCard;

maskCreditCard( '4111-1111-1111-1111' ); // e.g. 4143300214110028
since
1.0.0
author

Marc Alcaraz

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
throws
RandomException
example
use function oihana\masking\maskDatetime;

maskDatetime( '2001-09-11' , '2019-01-01' , '2019-12-31' , '%yyyy-%mm-%dd' );
// e.g. "2019-06-17"

maskDatetime( '2001-09-11' ); // "" (no format)
since
1.0.0
author

Marc Alcaraz

Return values
string

maskDecimal()

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
throws
RandomException
example
use function oihana\masking\maskDecimal;

maskDecimal( 3.14 );                       // e.g. -0.42 (default -1..1, scale 2)
maskDecimal( 'x' , -0.3 , 0.3 , 2 );       // e.g. 0.17
since
1.0.0
author

Marc Alcaraz

Return values
float

maskDocument()

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 attribute name at the top level ;
  • "a.b" — the exact nested path ab (through objects only) ;
  • ".name" — every leaf attribute named name, 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
throws
InvalidArgumentException

When a rule has no type, or an unknown masker.

RandomException
example
use function oihana\masking\maskDocument;

$doc = [ '_key' => 'a' , 'email' => 'real@example.com' , 'profile' => [ 'name' => 'Jane' ] ] ;
$rules = [ [ 'path' => 'email' , 'type' => 'email' ] , [ 'path' => '.name' , 'type' => 'xifyFront' ] ] ;

maskDocument( $doc , $rules , [ '_key' ] ) ; // keep _key
// [ '_key' => 'a' , 'email' => 'aZ12.bY34@cX56.invalid' , 'profile' => [ 'name' => 'xxne' ] ]

maskDocument( $doc , $rules ) ; // nothing protected by default
since
1.0.0
author

Marc Alcaraz

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
throws
InvalidArgumentException

When a rule has no type, or an unknown masker.

RandomException
example
use function oihana\masking\maskDocumentList;

$rules = [ [ 'path' => '.name' , 'type' => 'xifyFront' , 'unmaskedLength' => 2 ] ] ;
maskDocumentList( [ [ 'name' => 'hugo' ] , 'egon' ] , $rules , 1 ) ;
// [ [ 'name' => 'xxgo' ] , 'egon' ]
since
1.0.0
author

Marc Alcaraz

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
throws
InvalidArgumentException

When a rule has no type, or an unknown masker.

RandomException
example
use function oihana\masking\maskDocumentNode;

$rules = [ [ 'path' => 'email' , 'type' => 'email' ] ] ;
maskDocumentNode( [ '_key' => 'a' , 'email' => 'real@example.com' ] , $rules , '' , 0 , [ '_key' ] ) ;
// [ '_key' => 'a' , 'email' => 'aZ12.bY34@cX56.invalid' ]
since
1.0.0
author

Marc Alcaraz

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
throws
RandomException
example
use function oihana\masking\maskEmail;

maskEmail( 'real.person@example.com' ); // e.g. "x7Bq.9aMz@Kp3R.invalid"
since
1.0.0
author

Marc Alcaraz

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
throws
RandomException
example
use function oihana\masking\maskInteger;

maskInteger( 9999 );          // e.g. 42   (default range -100..100)
maskInteger( 'x' , 0 , 10 );  // e.g. 7
since
1.0.0
author

Marc Alcaraz

Return values
int

maskPhone()

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
throws
RandomException
example
use function oihana\masking\maskPhone;

maskPhone( '+31 66-77-88' ); // e.g. "+75 10-79-52"
maskPhone( 1234 );           // "+1234567890" (default, non-string input)
since
1.0.0
author

Marc Alcaraz

Return values
string

maskRandom()

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 → stays null.
Parameters
$value : mixed

The original value.

Tags
throws
RandomException
example
use function oihana\masking\maskRandom;

maskRandom( 'hello' ); // e.g. "x7Bqz"
maskRandom( 42 );      // e.g. -738
maskRandom( 2.34 );    // e.g. 91.7
maskRandom( true );    // e.g. false
maskRandom( null );    // null
since
1.0.0
author

Marc Alcaraz

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
throws
RandomException
example
use function oihana\masking\maskRandomString;

maskRandomString( 'My Name' ); // e.g. "x7Bqz9a"
maskRandomString( 1234 );      // 1234 (non-string, unchanged)
since
1.0.0
author

Marc Alcaraz

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
throws
InvalidArgumentException

When the masker name is unknown.

RandomException
example
use function oihana\masking\maskValue;

maskValue( 'email' , 'real@example.com' );             // "x7Bq.9aMz@Kp3R.invalid"
maskValue( 'xifyFront' , 'secret' , [ 'unmaskedLength' => 3 ] ); // "xxxret"
maskValue( 'random' , [ 1 , 'two' , true ] );          // [ -42, "x9Bz", false ]
since
1.0.0
author

Marc Alcaraz

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
example
use function oihana\masking\maskXifyFront;

maskXifyFront( 'This is a test!Do you agree?' ); // "xxis is a xxst Do xou xxxee "
maskXifyFront( true );                           // "xxxx"
maskXifyFront( null );                           // null
maskXifyFront( 'secret' , 4 );                   // "xxcret"
since
1.0.0
author

Marc Alcaraz

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
throws
RandomException
example
use function oihana\masking\maskZip;

maskZip( '50674' );   // e.g. "98146"
maskZip( 'SA34-EA' ); // e.g. "OW91-JI"
maskZip( null );      // "12345" (default)
since
1.0.0
author

Marc Alcaraz

Return values
string

randomAlphaNumeric()

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
throws
RandomException
example
use function oihana\masking\randomAlphaNumeric;

randomAlphaNumeric( 4 ); // e.g. "x7Bq"
since
1.0.0
author

Marc Alcaraz

Return values
string

resolveMaskingRule()

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 is name, 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
example
use function oihana\masking\resolveMaskingRule;

$rules = [ [ 'path' => 'person.name' , 'type' => 'xifyFront' ] ] ;
resolveMaskingRule( $rules , 'name' , 'person.name' ); // the rule
resolveMaskingRule( $rules , 'name' , 'other.name' );  // null
since
1.0.0
author

Marc Alcaraz

Return values
array<string, mixed>|null

The matching rule, or null.

On this page

Search results