|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entities\UserEntity; |
|
6
|
|
|
use App\Models\UserModel; |
|
7
|
|
|
|
|
8
|
|
|
class UserRepository |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var UserModel |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $userModel; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param UserModel $userModel |
|
18
|
|
|
*/ |
|
19
|
|
|
public function __construct(UserModel $userModel) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->setUserModel($userModel); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
public function all(): array |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->getUserModel()->all(); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param int $id |
|
34
|
|
|
* @return UserEntity |
|
35
|
|
|
*/ |
|
36
|
|
|
public function find(int $id): UserEntity |
|
37
|
|
|
{ |
|
38
|
|
|
return $this->getUserModel()->find($id); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param $user |
|
43
|
|
|
* @return \Nette\Database\Table\ActiveRow |
|
44
|
|
|
*/ |
|
45
|
|
|
public function create($user) |
|
46
|
|
|
{ |
|
47
|
|
|
unset($user->id); |
|
48
|
|
|
|
|
49
|
|
|
return $this->getUserModel() |
|
|
|
|
|
|
50
|
|
|
->save( |
|
51
|
|
|
$this->populate($user) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $id |
|
57
|
|
|
* @param $user |
|
58
|
|
|
* @return \Nette\Database\Table\ActiveRow |
|
59
|
|
|
*/ |
|
60
|
|
|
public function update($id, $user) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
return $this->getUserModel() |
|
|
|
|
|
|
63
|
|
|
->save( |
|
64
|
|
|
$this->populate($user) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param $id |
|
70
|
|
|
* @param $user |
|
71
|
|
|
* @return \Nette\Database\Table\ActiveRow |
|
72
|
|
|
*/ |
|
73
|
|
|
public function save($user) |
|
74
|
|
|
{ |
|
75
|
|
|
return $this->getUserModel() |
|
|
|
|
|
|
76
|
|
|
->save( |
|
77
|
|
|
$this->populate($user) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param $id |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
public function delete($id): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->getUserModel()->delete($id); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param array $values |
|
92
|
|
|
* @return UserEntity |
|
93
|
|
|
*/ |
|
94
|
|
|
protected function populate($values): UserEntity |
|
95
|
|
|
{ |
|
96
|
|
|
$model = $this->getUserModel(); |
|
97
|
|
|
$user = $model->hydrate($values); |
|
98
|
|
|
|
|
99
|
|
|
return $user; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return UserModel |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getUserModel(): UserModel |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->userModel; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param UserModel $model |
|
112
|
|
|
* @return $this |
|
113
|
|
|
*/ |
|
114
|
|
|
protected function setUserModel(UserModel $model): self |
|
115
|
|
|
{ |
|
116
|
|
|
$this->userModel = $model; |
|
117
|
|
|
|
|
118
|
|
|
return $this; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
|
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.