|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GuardiansLabs\Repository\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use GuardiansLabs\Repository\Contracts\RepositoryContract; |
|
6
|
|
|
use GuardiansLabs\Repository\Exceptions\RepositoryException; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
|
|
10
|
|
|
class BaseRepository implements RepositoryContract |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* The repository model. |
|
15
|
|
|
* @var Model |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $model; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param Model $model |
|
21
|
|
|
* @return object |
|
22
|
|
|
*/ |
|
23
|
|
|
public function setModel(Model $model) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->model = $model; |
|
26
|
|
|
return $this; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return Model |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getModel() |
|
33
|
|
|
{ |
|
34
|
|
|
return $this->model; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return Collection |
|
39
|
|
|
*/ |
|
40
|
|
|
public function findAll() |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->model->get(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param array $data |
|
47
|
|
|
* @return Collection |
|
48
|
|
|
* @throws RepositoryException |
|
49
|
|
|
*/ |
|
50
|
|
|
public function createNew(array $data) |
|
51
|
|
|
{ |
|
52
|
|
|
if (empty($data)) { |
|
53
|
|
|
throw new RepositoryException("No Given Data To Insert"); |
|
54
|
|
|
} |
|
55
|
|
|
$this->model->fill($data); |
|
56
|
|
|
return $this->model->save(); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $itemId |
|
61
|
|
|
* @throws RepositoryException |
|
62
|
|
|
* @return Collection |
|
63
|
|
|
*/ |
|
64
|
|
|
public function findItemById($itemId) |
|
65
|
|
|
{ |
|
66
|
|
|
$item = $this->model->find($itemId); |
|
67
|
|
|
if (!$item) { |
|
68
|
|
|
throw new RepositoryException("Item Not Found"); |
|
69
|
|
|
} |
|
70
|
|
|
return $item; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $itemId |
|
75
|
|
|
* @param array $data |
|
76
|
|
|
* @throws RepositoryException |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
*/ |
|
79
|
|
|
public function update($itemId, array $data) |
|
80
|
|
|
{ |
|
81
|
|
|
$item = $this->model->find($itemId); |
|
82
|
|
|
if (empty($data)) { |
|
83
|
|
|
throw new RepositoryException("No Given Data To Update"); |
|
84
|
|
|
} |
|
85
|
|
|
if (!$item) { |
|
86
|
|
|
throw new RepositoryException("No Item Found To Update"); |
|
87
|
|
|
} |
|
88
|
|
|
$item->fill($data); |
|
89
|
|
|
return $item->save(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param $itemId |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
* @throws RepositoryException |
|
96
|
|
|
*/ |
|
97
|
|
|
public function delete($itemId) |
|
98
|
|
|
{ |
|
99
|
|
|
$item = $this->model->find($itemId); |
|
100
|
|
|
if (!$item) { |
|
101
|
|
|
throw new RepositoryException("No Item Found To delete"); |
|
102
|
|
|
} |
|
103
|
|
|
return $item->delete(); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.