dominik-eller / laravel-data-normalizer
A Laravel package to normalize and format data such as phone numbers, emails, and other input into standardized formats.
Fund package maintenance!
dominik-eller
Requires
- php: ^8.2
- giggsey/libphonenumber-for-php: ^8.13
- illuminate/contracts: ^11.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^9.0.0||^8.22.0
- pestphp/pest: ^3.0
- pestphp/pest-plugin-arch: ^3.0
- pestphp/pest-plugin-laravel: ^3.0
- phpstan/extension-installer: ^1.3
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- spatie/laravel-ray: ^1.35
README
A Laravel package to normalize and format data such as phone numbers, emails, and other input into standardized formats.
Installation
You can install the package via composer:
composer require dominik-eller/laravel-data-normalizer
You can publish the config file with:
php artisan vendor:publish --tag="laravel-data-normalizer-config"
This is the contents of the published config file:
return [ 'phone' => [ 'default_country' => 'DE', 'format' => 'E164', ], 'email' => [ 'trim_whitespace' => true, 'lowercase' => true, ], ];
Usage
Normalize a phone number
use Deller\DataNormalizer\Facades\PhoneNormalizer; $normalizedPhone = PhoneNormalizer::create('phone')->normalize('+49 (151) 123 45678'); // $normalizedPhone will be "+4915112345678" (E.164 format)
Format a phone number
use Deller\DataNormalizer\Facades\PhoneFormatter; $formattedPhone = PhoneFormatter::create('phone')->format('+4915112345678', ['format' => 'INTERNATIONAL']); // $formattedPhone will be "+49 151 1234 5678"
Normalize an email address
use Deller\DataNormalizer\Facades\EmailNormalizer; $normalizedEmail = EmailNormalizer::create('email')->normalize(' JohnDoe@Example.com '); // $normalizedEmail will be "johndoe@example.com"
Format an email address
use Deller\DataNormalizer\Facades\EmailFormatter; $formattedEmail = DataFormatter::create('email')->format(' JohnDoe@Example.com '); // $formattedEmail will be "johndoe@example.com"
Registering a custom formatter
use Deller\DataNormalizer\Factories\DataFormatterFactory; class CustomFormatter implements \Deller\DataNormalizer\DataFormatter { public function format($data) { return 'Formatted: ' . strtoupper($data); } } // Register the custom formatter type DataFormatterFactory::registerType('custom', CustomFormatter::class); // Use the custom formatter via the facade $customFormatter = \Deller\DataNormalizer\Facades\DataFormatter::create('custom'); echo $customFormatter->format('some data'); // Output: "Formatted: SOME DATA"
Registering a custom normalizer
use Deller\DataNormalizer\Factories\DataNormalizerFactory; class CustomNormalizer implements \Deller\DataNormalizer\DataFormatter { public function normalize($data) { return 'Normalized: ' . strtolower(trim($data)); } } // Register the custom normalizer type DataNormalizerFactory::registerType('custom', CustomNormalizer::class); // Use the custom normalizer via the facade $customNormalizer = \Deller\DataNormalizer\Facades\DataNormalizer::create('custom'); echo $customNormalizer->normalize(' SOME DATA '); // Output: "Normalized: some data"
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please report security vulnerabilities by email to me@dominik-eller.de instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.