dominik-eller/laravel-data-normalizer

A Laravel package to normalize and format data such as phone numbers, emails, and other input into standardized formats.

v1.1.0 2024-09-26 11:25 UTC

This package is auto-updated.

Last update: 2024-10-26 11:41:38 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.