Code Monkey home page Code Monkey logo

laravel-normalizer's Introduction

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.

laravel-normalizer's People

Contributors

nwidart avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.