1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: mustafa |
5
|
|
|
* Date: 27/04/17 |
6
|
|
|
* Time: 06:57 م |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace GuardiansLabs\Repository\Repositories; |
10
|
|
|
|
11
|
|
|
use GuardiansLabs\Repository\Contracts\RepositoryContract; |
12
|
|
|
use GuardiansLabs\Repository\Exceptions\RepositoryException; |
13
|
|
|
use Illuminate\Database\Eloquent\Model; |
14
|
|
|
use Illuminate\Support\Collection; |
15
|
|
|
use App\User; |
16
|
|
|
|
17
|
|
|
class BaseRepository implements RepositoryContract |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
protected $model ; |
21
|
|
|
/** |
22
|
|
|
* @param Model $model |
23
|
|
|
* @return object |
24
|
|
|
*/ |
25
|
|
|
public function setModel(Model $model) |
26
|
|
|
{ |
27
|
|
|
$this->model = $model; |
28
|
|
|
return $this; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return Model |
33
|
|
|
*/ |
34
|
|
|
public function getModel() |
35
|
|
|
{ |
36
|
|
|
return $this->model; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Collection |
41
|
|
|
*/ |
42
|
|
|
public function findAll() |
43
|
|
|
{ |
44
|
|
|
return $this->model->get(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param array $data |
49
|
|
|
* @return Collection |
50
|
|
|
* @throws RepositoryException |
51
|
|
|
*/ |
52
|
|
|
public function createNew(array $data) |
53
|
|
|
{ |
54
|
|
|
if (!$data) { |
|
|
|
|
55
|
|
|
throw new RepositoryException("No Given Data To Insert"); |
56
|
|
|
} |
57
|
|
|
$this->model->fill($data); |
58
|
|
|
return $this->model->save(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param $itemId |
63
|
|
|
* @return Collection |
64
|
|
|
* @throws RepositoryException |
65
|
|
|
*/ |
66
|
|
|
public function findItemById($itemId) |
67
|
|
|
{ |
68
|
|
|
$item = $this->model->find($itemId); |
69
|
|
|
if (!$item) { |
70
|
|
|
throw new RepositoryException("Item Not Found"); |
71
|
|
|
} |
72
|
|
|
return $item; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $itemId |
77
|
|
|
* @param array $data |
78
|
|
|
* @throws RepositoryException |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
|
|
public function update($itemId, array $data) |
82
|
|
|
{ |
83
|
|
|
$item = $this->model->find($itemId); |
84
|
|
|
if (!$data) { |
|
|
|
|
85
|
|
|
throw new RepositoryException("No Given Data To Update"); |
86
|
|
|
} |
87
|
|
|
if (!$item) { |
88
|
|
|
throw new RepositoryException("No Item Found To Update"); |
89
|
|
|
} |
90
|
|
|
$item->fill($data); |
91
|
|
|
return $item->save(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $itemId |
96
|
|
|
* @return mixed |
97
|
|
|
*/ |
98
|
|
|
public function delete($itemId) |
99
|
|
|
{ |
100
|
|
|
// TODO: Implement delete() method. |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $where |
105
|
|
|
* @param array $attributes |
106
|
|
|
* @return Collection |
107
|
|
|
*/ |
108
|
|
|
public function findWhere($where, $attributes = ['*']) |
109
|
|
|
{ |
110
|
|
|
// TODO: Implement findWhere() method. |
111
|
|
|
} |
112
|
|
|
} |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.