Skip to content

Commit

Permalink
add Str macro: mask() and masEmail()
Browse files Browse the repository at this point in the history
  • Loading branch information
uyab committed Jan 13, 2020
1 parent 55e6a6c commit c60ab81
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/Mixin/StrMixin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Laravolt\Support\Mixin;

class StrMixin
{
public function mask()
{
return function ($str, $first, $last, $mask = '*') {
$len = strlen($str);
$toShow = $first + $last;

return substr($str, 0, $len <= $toShow ? 0 : $first) . str_repeat($mask,
$len - ($len <= $toShow ? 0 : $toShow)) . substr($str, $len - $last, $len <= $toShow ? 0 : $last);
};
}

public function maskEmail()
{
return function ($email) {
$mails = explode("@", $email);
$domain = $mails[1] ?? "";

$mails[0] = mask($mails[0], 3, 2);
$domain = mask($domain, 3, 2);
$mails[1] = $domain;

return implode("@", $mails);
};
}
}
3 changes: 3 additions & 0 deletions src/SupportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Laravolt\Support\Mixin\QueryBuilderMixin;
use Laravolt\Support\Mixin\StrMixin;

class SupportServiceProvider extends ServiceProvider
{
Expand All @@ -23,6 +25,7 @@ public function boot()
protected function registerMacro()
{
Builder::mixin(new QueryBuilderMixin());
Str::mixin(new StrMixin());

EloquentBuilder::macro('whereLike', function ($attributes, ?string $searchTerm) {
if ($searchTerm === null) {
Expand Down

0 comments on commit c60ab81

Please sign in to comment.