feat: add hideEmail transformer for PII masking#42
Merged
viniciusvts merged 1 commit intoSSolWEB:mainfrom Apr 22, 2026
Merged
Conversation
Issue SSolWEB#25 asks for an email-masking helper similar to the existing Brazilian-format maskers (maskBrCpf / maskBrPhone / etc) so callers don't have to re-implement the explode-and-substr boilerplate every time they need to log a user email in a compliance-sensitive context. Add HideEmailTransformer that: - Returns the input unchanged when filter_var rejects it as an email or when the domain has no dot (nothing to preserve). - Splits on the first dot in the domain so compound suffixes like .co.uk survive intact (contoso.co.uk -> co*****.co.uk). - Keeps the first two characters of the local part and host, replaces the remainder with '*', and preserves the TLD chain. - Short local / host parts (<=2 chars) keep whatever prefix they have plus a single '*' so the mask stays visible for edge cases like [email protected] -> a*@x*.io. Examples: [email protected] -> jo********@ex*****.com [email protected] -> al***@co*****.co.uk [email protected] -> a*@x*.io not-an-email -> not-an-email Wires the new method through the existing dynamic-transformer pattern: adds @method hints on StringMorpher and StringMorpherInstance, and a dedicated HideEmailTransformerTest covering the happy path, short-segment fallback, invalid input passthrough, and the SM::hideEmail facade. Fixes SSolWEB#25
👷 Deploy request for string-morpher pending review.Visit the deploys page to approve it
|
Member
|
Thank you for your contribution! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
hideEmail- a new transformer that masks an email address for privacy / compliance logging (LGPD / GDPR), matching the existing Brazilian-formatmaskBrCpf/maskBrPhone/ etc. helpers.Why this matters
Issue #25 laid out the use case (audit logs, "reset link sent to j***@gm***.com", customer-support dashboards) and the existing workaround - every caller manually
explode('@', $email)+substr(), which drifts in style and edge-case handling. One reusable transformer removes that surface.Changes
src/Transformers/HideEmailTransformer.php: new final class implementingStringTransformerInterface.filter_var(..., FILTER_VALIDATE_EMAIL)short-circuits: invalid input returns unchanged (matches howMaskBrCpfreturns early when digits don't match).contoso.co.uk->co*****.co.uk, not the.uk-only resultstrrposwould give).mask()keeps the first two characters and pads the rest with*. Segments of length ≤ 2 keep their prefix plus one*so the mask stays visible for short addresses like[email protected].src/StringMorpher.phpandsrc/Instances/StringMorpherInstance.php: add the@method hideEmail(...)hints alongside the existing mask helpers, so IDE autocomplete and static analysis pick it up.tests/Transformers/HideEmailTransformerTest.php: mirror theMaskBrCpfTransformerTestshape - directtransform()cases, short-segment behaviour, invalid-input passthrough, and theSM::hideEmailfacade.Testing
All existing tests still pass. The new transformer is registered through the library's dynamic
__callloader - no changes toStringMorpherInstance::__callwere needed.Examples
Fixes #25
This contribution was developed with AI assistance (Claude Code).