Laravel package to normalize your data before saving into the database.

Overview

laravel-normalizer

Latest Version on Packagist Software License Build Status Scrutinizer Coverage SensioLabsInsight Quality Score Total Downloads

This package helps you normalize your data in order to save them into the database. The Goal is to having separate classes that handle the data normalization, and thus can be tested independently.

Install

Via Composer

$ composer require nwidart/laravel-normalizer

Usage

1. Adding trait

Add the Nwidart\LaravelNormalizer\Traits\CanNormalizeData trait on the model(s) you wish data to be normalized.

2. Create Normalizer classes

Your normalizers classes need to implement the Nwidart\LaravelNormalizer\Contracts\Normalizer interface. This interface will add the normalize(array $data) method.

Example:

use Nwidart\LaravelNormalizer\Contracts\Normalizer;

final class CustomNormalizer implements Normalizer
{
    /**
     * Normalize the given data
     * @param array $data
     * @return array
     */
    public function normalize(array $data)
    {
        if (array_key_exists('name', $data)) {
            $data['name'] = strtoupper($data['name']);
        }

        return $data;
    }
}

This method needs to return the $data array. In here you can change the received data as you please.

3. Add normalizers class property

On that same model, add a protected $normalizers property. This is where you list your normalizers, in an array.

Example:

use Illuminate\Database\Eloquent\Model;
use Nwidart\LaravelNormalizer\Traits\CanNormalizeData;

class Product extends Model
{
    use CanNormalizeData;
    protected $normalizers = [CustomNormalizer::class];
}

4. Normalize your data on save/update

Now you can start normalizing your data. This can for instance be done in your repository class.

Example:

public function create($data)
{
    $data = $this->model->normalize($data);

    return $this->model->create($data);
}

Change log

Please see CHANGELOG for more information what has changed recently.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Laravel Responder - a package for building API responses, integrating Fractal into Laravel and Lumen
Laravel Responder - a package for building API responses, integrating Fractal into Laravel and Lumen

A Laravel Fractal package for building API responses, giving you the power of Fractal with Laravel's elegancy.

Laravel package for manage your URL redirects in database or other sources to get better SEO results

Laravel 8 and 9 package to manage URL redirections inside your Laravel application using different data sources. It allows a better SEO support for your Laravel site.

The query filter bundle allows you to filter data from QueryBuilder and the Database.
The query filter bundle allows you to filter data from QueryBuilder and the Database.

The query filter bundle allows you to filter data from QueryBuilder and the Database. you can filter multiple columns at the same time and also you can filter relation fields with two-level deep and without any join in your query builder.

Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Laravel Segment Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application. Installation You can install the pac

Laravel package integrating PHP Flasher into Livewire applications
Laravel package integrating PHP Flasher into Livewire applications

A powerful and flexible flash notifications system for PHP, Laravel, Symfony 👀 PHP Flasher helps you to add flash notifications to your PHP projects.

Package to parse DNA kit files, and import them into Laravel

Package to parse DNA kit files, and import them into Laravel

Laravel 5.* package to easily introduce a transformation layer for your data

Laraformer Laraformer is a laravel 5.* package that lets you easily introduce a transformation layer for your data. Laraformer (originated from Larave

Take a look into your Laravel views
Take a look into your Laravel views

Xray - Take a look into your Laravel views When your Laravel project grows, so do the Laravel views. Sometimes it might be hard to figure out, which p

Integrate likes, bookmarks, favorites, reactions and custom made marks into your application
Integrate likes, bookmarks, favorites, reactions and custom made marks into your application

Laravel Markable This package allows you to easily add the markable feature to your application, as for example likes, bookmarks, favorites and so on.

Releases(0.2)
Owner
Nicolas Widart
Freelance developer & consultant. Laravel specialist. Spring fan. React and Vue user. Open-source advocate. New technologies geek. Apple fan. @AsgardCms creator
Nicolas Widart
A Laravel URL Shortener package that provides URL redirects with optionally protected URL password, URL expiration, open limits before expiration

A Laravel URL Shortener package that provides URL redirects with optionally protected URL password, URL expiration, open limits before expiration, ability to set feature activation dates, and click tracking out of the box for your Laravel applications.

YorCreative 53 Jan 4, 2023
Renamify is a package for Laravel used to rename a file before uploaded to prevent replacing exists file which has the same name to this new uploaded file.

Renamify Laravel package for renaming file before uploaded on server. Renamify is a package for Laravel used to rename a file before uploaded to preve

MB'DUSENGE Callixte 2 Oct 11, 2022
Generate previous attributes when saving Eloquent models

This package provides a trait that will generate previous attributes when saving any Eloquent model.

Ricardo Sawir 33 Nov 6, 2022
Smeify is a Stable Automated Solution for Airtime and Data businesses in Nigeria, this package helps you integrate smeify easily into your laravel application.

Smeify is a Stable Automated Solution for Airtime and Data businesses in Nigeria, this package helps you integrate smeify easily into your laravel application.

Ogundiran Adewale Charles 2 Jul 27, 2022
LERN is a Laravel package that will record exceptions into a database and will notify you via Email, Pushover or Slack.

LERN is a Laravel package that will record exceptions into a database and will notify you via Email, Pushover or Slack.

Tyler Arbon 437 Nov 17, 2022
Easily integrate single-database multi tenant features into your Laravel application

Laravel Tenant Aware Easily integrate single-database multi tenant features into your Laravel application. Installation You can install the package vi

H-FARM Innovation 9 Dec 21, 2022
Save Model is a Laravel package that allows you to save data in the database in a new way.

Save Model is a Laravel package that allows you to save data in the database in a new way. No need to worry about $guarded and $fillable properties in the model anymore. Just relax an use Save Model package.

Laratips 27 Mar 2, 2022
Convert remote api response data into laravel model

laravel remote model Create remote driver to convert remote api request into laravel model. 中文文档 日本語文書 overview Install the version between laravel5.5

张子彬 15 Aug 11, 2022
Laravel package that converts your application into a static HTML website

phpReel Static Laravel Package phpReel Static is a simple Laravel Package created and used by phpReel that converts your Laravel application to a stat

phpReel 16 Jul 7, 2022
Collection of the Laravel/Eloquent Model classes that allows you to get data directly from a Magento 2 database.

Laragento LAravel MAgento Micro services Magento 2 has legacy code based on abandoned Zend Framework 1 with really ugly ORM on top of outdated Zend_DB

Egor Shitikov 87 Nov 26, 2022