Skip to content

Commit b92c480

Browse files
committed
+feature(Added Commands):
-make:repository -repository:model -repository:controller -repository:interface +feature(Added Migration):
1 parent a0404c5 commit b92c480

17 files changed

Lines changed: 1087 additions & 0 deletions

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "faiverson/gateway-pattern",
3+
"description": "Layer to use repository pattern or gateway pattern in Laravel 5",
4+
"license": "Apache",
5+
"authors": [
6+
{
7+
"name": "Fabian Torres",
8+
"email": "fa.iverson@gmail.com"
9+
}
10+
],
11+
"require": {
12+
"laravel/framework": "5.3"
13+
},
14+
"autoload": {
15+
"psr-4": {
16+
"faiverson\\gateways\\": "src/"
17+
}
18+
},
19+
"minimum-stability": "dev",
20+
"prefer-stable": true
21+
}

resources/config/repositories.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
return [
3+
// directories where you want to put your classes
4+
'path' => [
5+
'repositories' => 'Repositories',
6+
'interfaces' => 'Repositories/Interfaces',
7+
'models' => 'Models',
8+
'gateways' => 'Repositories/Gateways',
9+
]
10+
];
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class RepositoryServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* There is no need to bind classes into the container if they do not depend on any interfaces.
11+
* The container does not need to be instructed on how to build these objects,
12+
* since it can automatically resolve such "concrete" objects using PHP's reflection services.
13+
*
14+
*/
15+
public function register()
16+
{
17+
// example line:
18+
//$this->app->bind('App\Repositories\Contracts\UserInterface', 'App\Repositories\UserRepository');
19+
}
20+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
namespace faiverson\gateways\abstracts;
4+
5+
use Illuminate\Validation\Factory;
6+
use faiverson\gateways\contracts\Validable;
7+
8+
abstract class AbstractValidator implements Validable
9+
{
10+
/**
11+
* Validator factory
12+
*
13+
* @var object
14+
*/
15+
protected $validator;
16+
17+
/**
18+
* Validator object
19+
*
20+
* @var object
21+
*/
22+
protected $factory;
23+
24+
/**
25+
* Data to be validated
26+
*
27+
* @var array
28+
*/
29+
protected $data = array();
30+
31+
/**
32+
* Validation Rules
33+
*
34+
* @var array
35+
*/
36+
protected $rules = array();
37+
38+
/**
39+
* Messages
40+
*
41+
* @var array
42+
*/
43+
protected $messages = array();
44+
45+
/**
46+
* Validation errors
47+
*
48+
* @var array
49+
*/
50+
protected $errors = array();
51+
52+
public function __construct(Factory $validator)
53+
{
54+
$this->factory = $validator;
55+
}
56+
57+
/**
58+
* Set data to validate
59+
*
60+
* @param array $data
61+
* @return self
62+
*/
63+
public function with(array $data)
64+
{
65+
$this->data = $data;
66+
67+
return $this;
68+
}
69+
70+
/**
71+
* Return errors
72+
*
73+
* @return array
74+
*/
75+
public function errors()
76+
{
77+
return $this->errors;
78+
}
79+
80+
/**
81+
* Pass the data and the rules to the validator
82+
*
83+
* @return boolean
84+
*/
85+
public function passes()
86+
{
87+
$this->custom();
88+
$this->validator = $this->factory->make($this->data, $this->rules, $this->messages);
89+
$this->validator->after(function ($validator) {
90+
$this->after($validator);
91+
});
92+
93+
if ($this->validator->fails()) {
94+
$this->errors = $this->validator->messages();
95+
return false;
96+
}
97+
98+
return true;
99+
}
100+
101+
public function custom()
102+
{
103+
}
104+
public function after($validator)
105+
{
106+
}
107+
}

src/abstracts/Repository.php

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<?php
2+
3+
namespace faiverson\gateways\abstracts;
4+
5+
use faiverson\gateways\exceptions\RepositoryException;
6+
use faiverson\gateways\contracts\RepositoryInterface;
7+
use Illuminate\Database\Eloquent\Model;
8+
9+
/**
10+
* Class Repository
11+
*/
12+
abstract class Repository implements RepositoryInterface
13+
{
14+
protected $model;
15+
16+
public function __construct()
17+
{
18+
$this->app = app();
19+
$this->model = $this->app->make($this->model());
20+
if (!$this->model instanceof Model) {
21+
throw new RepositoryException(
22+
"Class {$this->model()} must be an instance of Illuminate\\Database\\Eloquent\\Model"
23+
);
24+
}
25+
}
26+
27+
/**
28+
* Specify Model class name
29+
*
30+
* @return mixed
31+
*/
32+
abstract public function model();
33+
34+
/**
35+
* @param array $columns
36+
* @return mixed
37+
*/
38+
public function all(
39+
$data = null,
40+
$columns = ['*'],
41+
$limit = null,
42+
$offset = null,
43+
$order_by = null,
44+
$filters = [],
45+
$with = []
46+
) {
47+
$query = $this->model;
48+
foreach ($with as $join) {
49+
$query = $query->with($join);
50+
}
51+
$query = $this->setFilters($query, $filters);
52+
if ($limit != null) {
53+
$query = $query->take($limit);
54+
}
55+
56+
if ($offset != null) {
57+
$query = $query->skip($offset);
58+
}
59+
60+
if ($order_by != null) {
61+
foreach ($order_by as $column => $dir) {
62+
$query = $query->orderBy($column, $dir);
63+
}
64+
}
65+
return $query->get($columns);
66+
}
67+
68+
public function total($filters = array())
69+
{
70+
$query = $this->model;
71+
$query = $this->setFilters($query, $filters);
72+
return $query->count();
73+
}
74+
75+
/**
76+
* @param array $data
77+
* @return mixed
78+
*/
79+
public function create(array $data)
80+
{
81+
return $this->model->create($this->setAttributes($data));
82+
}
83+
84+
/**
85+
* @param array $data
86+
* @return mixed
87+
*/
88+
public function firstOrCreate(array $data)
89+
{
90+
return $this->model->firstOrCreate($this->setAttributes($data));
91+
}
92+
93+
/**
94+
* @param array $data
95+
* @return mixed
96+
*/
97+
public function firstOrNew(array $data)
98+
{
99+
return $this->model->firstOrNew($this->setAttributes($data));
100+
}
101+
102+
/**
103+
* @param array $data
104+
* @return mixed
105+
*/
106+
public function updateOrCreate(array $data, array $extra)
107+
{
108+
return $this->model->updateOrCreate($this->setAttributes($data), $this->setAttributes($extra));
109+
}
110+
111+
/**
112+
* @param array $data
113+
* @param $id
114+
* @param string $attribute
115+
* @return mixed
116+
*/
117+
public function update(array $data, $id)
118+
{
119+
return $this->model->find($id)->update($this->setAttributes($data));
120+
}
121+
122+
/**
123+
* @param $id
124+
* @return mixed
125+
*/
126+
public function destroy($id)
127+
{
128+
return $this->model->destroy($id);
129+
}
130+
131+
/**
132+
* @param $id
133+
* @param array $columns
134+
* @return mixed
135+
*/
136+
public function find($id, $columns = ['*'])
137+
{
138+
return $this->model->find($id, $columns);
139+
}
140+
141+
/**
142+
* @param $attribute
143+
* @param $value
144+
* @param array $columns
145+
* @return mixed
146+
*/
147+
public function findBy($attribute, $value, $columns = ['*'], $limit = null, $offset = null, $order_by = null, $with = [])
148+
{
149+
$query = $this->model;
150+
151+
if (!empty($with) && count($with) > 0) {
152+
foreach ($with as $join) {
153+
$query = $query->with($join);
154+
}
155+
}
156+
157+
if ($limit != null) {
158+
$query = $query->take($limit);
159+
}
160+
161+
if ($offset != null) {
162+
$query = $query->skip($offset);
163+
}
164+
165+
if ($order_by != null) {
166+
foreach ($order_by as $column => $dir) {
167+
$query = $query->orderBy($column, $dir);
168+
}
169+
}
170+
return $query->where($attribute, $value)->get($columns);
171+
}
172+
173+
public function setAttributes(array $data)
174+
{
175+
$data = array_map(function ($value) {
176+
return is_array($value) || is_object($value) ? $this->setAttributes($value) : trim($value);
177+
}, $data);
178+
179+
$data = array_filter($data, function ($value) {
180+
return ($value !== null && $value !== '');
181+
});
182+
183+
return $data;
184+
}
185+
186+
public function setFilters($query, Array $filters)
187+
{
188+
return $query;
189+
}
190+
}

0 commit comments

Comments
 (0)