Skip to content

Commit d850d1e

Browse files
committed
Initial commit
0 parents  commit d850d1e

35 files changed

+2277
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/vendor
2+
composer.phar
3+
composer.lock
4+
.DS_STORE

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 Stidges <info@stidges.com>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
![Result](http://i.imgur.com/Y0rEyYq.jpg)
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).

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "stidges/laravel-db-normalizer",
3+
"description": "Normalize all database results to one unified interface, to make swapping repositories a breeze.",
4+
"version": "0.1.0",
5+
"homepage": "https://github.com/stidges/laravel-db-normalizer",
6+
"authors": [
7+
{
8+
"name": "Stidges",
9+
"email": "info@stidges.com",
10+
"homepage": "http://twitter.com/stidges"
11+
}
12+
],
13+
"keywords": [
14+
"db",
15+
"normalizer",
16+
"laravel",
17+
"eloquent",
18+
"repositories"
19+
],
20+
"require": {
21+
"php": ">=5.4.0",
22+
"illuminate/database": "4.1.*"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "4.0.*",
26+
"mockery/mockery": "dev-master"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"Stidges\\LaravelDbNormalizer\\": "src/"
31+
}
32+
},
33+
"config": {
34+
"preferred-install": "dist"
35+
},
36+
"minimum-stability": "stable",
37+
"license": "MIT"
38+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace Stidges\Examples\AutoNormalized;
4+
5+
use Stidges\Examples\UserRepositoryInterface;
6+
use Illuminate\Database\DatabaseManager;
7+
use Stidges\LaravelDbNormalizer\Normalizer;
8+
9+
class DbUserRepository implements UserRepositoryInterface
10+
{
11+
/**
12+
* DatabaseManager instance.
13+
*
14+
* @var \Illuminate\Database\DatabaseManager
15+
*/
16+
protected $db;
17+
18+
/**
19+
* The table name for the users.
20+
*
21+
* @var string
22+
*/
23+
protected $table = 'users';
24+
25+
/**
26+
* The fields that are fillable.
27+
*
28+
* @var array
29+
*/
30+
protected $fillable = [ 'name', 'email' ];
31+
32+
/**
33+
* Create a new user repository.
34+
*
35+
* @param \Illuminate\Database\DatabaseManager $db
36+
* @return void
37+
*/
38+
public function __construct(DatabaseManager $db)
39+
{
40+
$this->db = $db;
41+
}
42+
43+
/**
44+
* Get all users.
45+
*
46+
* @return \Stidges\LaravelDbNormalizer\Collection
47+
*/
48+
public function getAll()
49+
{
50+
return $this->db->table($this->table)
51+
->get();
52+
}
53+
54+
/**
55+
* Get a user by its ID.
56+
*
57+
* @param mixed $id
58+
* @return \Stidges\LaravelDbNormalizer\Entity
59+
*/
60+
public function getById($id)
61+
{
62+
return $this->db->table($this->table)
63+
->where('id', $id)
64+
->first();
65+
}
66+
67+
/**
68+
* Create a new user.
69+
*
70+
* @param \Stidges\LaravelDbNormalizer\Entity $user
71+
* @return bool
72+
*/
73+
public function create(Entity $user)
74+
{
75+
$data = array_only($user->toArray(), $this->fillable);
76+
77+
return $this->db->table($this->table)
78+
->insert($data);
79+
}
80+
81+
/**
82+
* Update an existing user.
83+
*
84+
* @param \Stidges\LaravelDbNormalizer\Entity $user
85+
* @return bool
86+
*/
87+
public function update(Entity $user)
88+
{
89+
$data = array_only($user->toArray(), $this->fillable);
90+
91+
return (bool) $this->db->table($this->table)
92+
->where('id', $user->id)
93+
->update($data);
94+
}
95+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
namespace Stidges\Examples\AutoNormalized;
4+
5+
use Stidges\Examples\User;
6+
use Stidges\Examples\UserRepositoryInterface;
7+
8+
class EloquentUserRepository implements UserRepositoryInterface
9+
{
10+
/**
11+
* User model.
12+
*
13+
* @var \Stidges\Examples\User
14+
*/
15+
protected $model;
16+
17+
/**
18+
* Create a new user repository.
19+
*
20+
* @param \Stidges\Examples\User $model
21+
* @return void
22+
*/
23+
public function __construct(User $model)
24+
{
25+
$this->model = $model;
26+
}
27+
28+
/**
29+
* Get all users.
30+
*
31+
* @return \Stidges\LaravelDbNormalizer\Collection
32+
*/
33+
public function getAll()
34+
{
35+
return $this->model->all();
36+
}
37+
38+
/**
39+
* Get a user by its ID.
40+
*
41+
* @param mixed $id
42+
* @return \Stidges\LaravelDbNormalizer\Entity
43+
*/
44+
public function getById($id)
45+
{
46+
return $this->model->find($id);
47+
}
48+
49+
/**
50+
* Create a new user.
51+
*
52+
* @param \Stidges\LaravelDbNormalizer\Entity $user
53+
* @return bool
54+
*/
55+
public function create(Entity $user)
56+
{
57+
$model = $this->model->newInstance($user->toArray());
58+
59+
return $model->save();
60+
}
61+
62+
/**
63+
* Update an existing user.
64+
*
65+
* @param \Stidges\LaravelDbNormalizer\Entity $user
66+
* @return bool
67+
*/
68+
public function update(Entity $user)
69+
{
70+
$model = $this->model->newInstance($user->toArray(), true);
71+
$model->id = $user->id;
72+
73+
return $user->getDirtyAttributes() ? $model->save() : $model->touch();
74+
}
75+
}

0 commit comments

Comments
 (0)