stidges / laravel-db-normalizer by stidges

Normalize all database results to one unified interface, to make swapping repositories a breeze.
17
69
6
Package Data
Maintainer Username: stidges
Maintainer Contact: info@stidges.com (Stidges)
Package Create Date: 2014-03-12
Package Last Update: 2014-03-12
Language: PHP
License: MIT
Last Refreshed: 2024-11-20 03:00:28
Package Statistics
Total Downloads: 17
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 69
Total Watchers: 6
Total Forks: 5
Total Open Issues: 0

Laravel DB Normalizer

Latest Stable Version Total Downloads Build Status License

This Laravel package allows you to easily swap out your repository implementations, by providing a unified interface to your database results.

It intercepts your results and turns them into collections and entities. That way, whether you are using Eloquent, the Query Builder or any other implementation, the results will be the same!

Result

Getting Started

This package can be installed through Composer, just add it to your composer.json file:

{
    "require": {
        "stidges/laravel-db-normalizer": "0.*"
    }
}

After you have added it to your composer.json file, make sure you update your dependencies:

composer install

Next, you can do either of these two:

1. Enable auto-normalization:

By registering this package's ServiceProvider class, all the queries you run will be automatically normalized to the unified Collection and Entity classes. Add the following line to your app/config/app.php file:

'Stidges\LaravelDbNormalizer\DbNormalizerServiceProvider',

When using Eloquent models, they should extend the NormalizableModel class:

use Stidges\LaravelDbNormalizer\NormalizableModel;

class User extends NormalizableModel
{
    // ...
}
2. Disable auto-normalization:

If you would rather want some more control, don't register the ServiceProvider. That way you can control when the results get cast to the classes. To do this, use the Normalizer class. Make sure you always pass an array to the normalizer!

use Stidges\LaravelDbNormalizer\Normalizer;

//...

// Example using the query builder
$result = DB::table('users')->get();
// ... Do stuff with the result.
$normalized = with(new Normalizer)->normalize($result);

// Example using Eloquent
$user = User::find(1);
// ... Do stuff with the user.
$normalized = with(new Normalizer)->normalize($user->toArray());

// Example using Eloquent collection
$users = User::all();
// ... Do stuff with the users.
$normalized = with(new Normalizer)->normalize($users->toArray());

Using the normalized results

This package provides 2 classes:

  1. A Collection class. As it currently stand, this class just extends Laravel's Illuminate\Support\Collection class.
  2. An Entity class. This class can contain nested entities and collections (relations). It provides a fluent interface to accessing the attributes of the entity, and can be cast to an array or JSON using the familiar toJson and toArray methods. On top of that, it provides a getDirtyAttributes() function, which allows you to get all the attributes that were changed after creation.

Example usage

For examples on how to use the package, please view the examples directory!

Contributing

All suggestions and pull requests are welcome! If you make any substantial changes, please provide tests along with your pull requests!

License

Copyright (c) 2014 Stidges - Released under the MIT license.