|
| 1 | +Laravel DB Normalizer |
| 2 | +===================== |
| 3 | + |
| 4 | +This [Laravel](http://www.laravel.com) package allows you to easily swap out your repository implementations, by providing a unified interface to your database / persistance layer results. |
| 5 | + |
| 6 | +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! |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Getting Started |
| 11 | + |
| 12 | +This package can be installed through [Composer](http://www.getcomposer.org), just add it to your composer.json file: |
| 13 | + |
| 14 | +```json |
| 15 | +{ |
| 16 | + "require": { |
| 17 | + "stidges/laravel-db-normalizer": "0.*" |
| 18 | + } |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +After you have added it to your composer.json file, make sure you update your dependencies: |
| 23 | + |
| 24 | +```sh |
| 25 | +composer install |
| 26 | +``` |
| 27 | + |
| 28 | +Next, you can do either of these two: |
| 29 | + |
| 30 | +##### 1. Enable auto-normalization: |
| 31 | + |
| 32 | +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: |
| 33 | + |
| 34 | +```php |
| 35 | +'Stidges\LaravelDbNormalizer\DbNormalizerServiceProvider', |
| 36 | +``` |
| 37 | + |
| 38 | +When using Eloquent models, they should extend the `NormalizableModel` class: |
| 39 | + |
| 40 | +```php |
| 41 | +use Stidges\LaravelDbNormalizer\NormalizableModel; |
| 42 | + |
| 43 | +class User extends NormalizableModel |
| 44 | +{ |
| 45 | + // ... |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | + |
| 50 | +##### 2. Disable auto-normalization: |
| 51 | + |
| 52 | +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!** |
| 53 | + |
| 54 | +```php |
| 55 | +use Stidges\LaravelDbNormalizer\Normalizer; |
| 56 | + |
| 57 | +//... |
| 58 | + |
| 59 | +// Example using the query builder |
| 60 | +$result = DB::table('users')->get(); |
| 61 | +// ... Do stuff with the result. |
| 62 | +$normalized = with(new Normalizer)->normalize($result); |
| 63 | + |
| 64 | +// Example using Eloquent |
| 65 | +$user = User::find(1); |
| 66 | +// ... Do stuff with the user. |
| 67 | +$normalized = with(new Normalizer)->normalize($user->toArray()); |
| 68 | + |
| 69 | +// Example using Eloquent collection |
| 70 | +$users = User::all(); |
| 71 | +// ... Do stuff with the users. |
| 72 | +$normalized = with(new Normalizer)->normalize($users->toArray()); |
| 73 | +``` |
| 74 | + |
| 75 | +## Using the normalized results |
| 76 | + |
| 77 | +This package provides 2 classes: |
| 78 | + |
| 79 | +1. A `Collection` class. As it currently stand, this class just extends Laravel's `Illuminate\Support\Collection` class. |
| 80 | +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. |
| 81 | + |
| 82 | +## Example usage |
| 83 | + |
| 84 | +For examples on how to use the package, please view the `examples` directory! |
| 85 | + |
| 86 | +## Contributing |
| 87 | + |
| 88 | +All suggestions and pull requests are welcome! If you make any substantial changes, please provide tests along with your pull requests! |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +Copyright (c) 2014 Stidges - Released under the [MIT license](LICENSE). |
0 commit comments