czogori/dami
czogori/dami is a small Laravel/PHP package that provides basic DAMI-related functionality and helpers. Lightweight and easy to drop into an existing app, it aims to streamline common tasks without heavy configuration or dependencies.
Installation
composer require czogori/dami
Add the service provider to config/app.php under providers:
Czogori\Dami\DamiServiceProvider::class,
Publish Config (Optional)
php artisan vendor:publish --provider="Czogori\Dami\DamiServiceProvider"
Check config/dami.php for default settings (e.g., API keys, default models).
First Use Case: Basic Data Masking
use Czogori\Dami\Facades\Dami;
// Mask a sensitive field (e.g., email)
$maskedEmail = Dami::mask('user@example.com', 'email');
// Output: `*****@example.com`
// Mask a phone number
$maskedPhone = Dami::mask('+1234567890', 'phone');
// Output: `+1*****890`
Key Facade Methods
Dami::mask($value, $type): Mask data by type (e.g., email, phone, credit_card).Dami::config(): Access or modify runtime config.Dami::extend(): Add custom masking rules (see Implementation Patterns).Model Integration (Eager Loading)
Use the HasMaskable trait to auto-mask attributes when retrieved:
use Czogori\Dami\Traits\HasMaskable;
class User extends Model
{
use HasMaskable;
protected $maskable = ['email', 'phone']; // Fields to mask by default
}
// Usage:
$user = User::find(1);
// $user->email is automatically masked in responses (e.g., API, Blade).
API Response Filtering
Override AppServiceProvider to mask responses globally:
public function boot()
{
Dami::extend('api', function ($value, $type) {
return Dami::mask($value, $type);
});
Response::macro('masked', function () {
return $this->setContent(Dami::mask($this->content, 'api'));
});
}
Dynamic Masking in Controllers
public function show(User $user)
{
return response()->json([
'email' => Dami::mask($user->email, 'email'),
'phone' => Dami::mask($user->phone, 'phone'),
]);
}
Blade Directives Add a Blade directive for templates:
// In AppServiceProvider@boot()
Blade::directive('mask', function ($expression) {
return "<?php echo Czogori\Dami\Facades\Dami::mask({$expression}, '{$expression}'); ?>";
});
// Usage in Blade:
@mask($user->email)
Laravel Scout: Mask sensitive search results by extending the toSearchableArray() method:
public function toSearchableArray()
{
return array_map(function ($value, $key) {
return in_array($key, $this->maskable) ? Dami::mask($value, $key) : $value;
}, $this->attributes, array_keys($this->attributes));
}
Laravel Nova: Use the maskable trait in Nova resources to hide sensitive fields in the UI:
public static $maskable = ['ssn', 'credit_card'];
public function fields(Request $request)
{
$fields = parent::fields($request);
foreach ($this->$maskable as $field) {
$fields->push( new Text($field, function () {
return Dami::mask($this->{$field}, $field);
}));
}
return $fields;
}
Logging: Mask sensitive data in logs:
use Czogori\Dami\Facades\Dami;
Log::info('User action', [
'user_email' => Dami::mask(auth()->user()->email, 'email'),
'ip' => request()->ip(),
]);
Performance Overhead
AppServiceProvider) can slow down responses.Config Overrides
config/dami.php. Overriding them globally may affect unexpected areas.Dami::config(['type' => 'custom_rule']) sparingly or per-request.Custom Types Not Triggering
license_plate) but it doesn’t work, ensure:
config/dami.php under types.Dami::mask($value, 'license_plate')).Database Storage
Blade Caching
@mask directives, clear Blade cache after adding new directives:
php artisan view:clear
Check Masked Output Dump the masked value to verify:
dd(Dami::mask('test@email.com', 'email')); // Should show `*****@email.com`
Inspect Config
dd(Dami::config());
Enable Debug Mode
Set debug to true in config/dami.php to log masking operations:
'debug' => env('DAMI_DEBUG', false),
Custom Masking Rules Extend the package by adding new types:
Dami::extend('license_plate', function ($value) {
return '****' . substr($value, -3);
});
Conditional Masking Use closures for dynamic rules:
Dami::extend('dynamic', function ($value, $type) {
return auth()->check() ? $value : Dami::mask($value, $type);
});
Override Default Rules Replace existing types (e.g., for stricter email masking):
Dami::extend('email', function ($value) {
return '*****' . substr($value, strpos($value, '@'));
});
Event-Based Masking
Listen for dami.masking events to log or modify masking:
event(new Czogori\Dami\Events\MaskingEvent($value, $type, $maskedValue));
Environment-Specific Rules
Use config/dami.php to define different rules per environment:
'types' => [
'email' => [
'dev' => '*****@{domain}',
'production' => '****@{domain}',
],
],
Partial Masking For partial masking (e.g., show first 3 digits of a phone):
Dami::extend('phone_partial', function ($value) {
return '+' . substr($value, 0, 3) . '****' . substr($value, -2);
});
Testing Mock the facade in tests:
$this->mock(Dami::class)->shouldReceive('mask')
->with('test@example.com', 'email')
->andReturn('*****@example.com');
How can I help you explore Laravel packages today?