1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repositories; |
4
|
|
|
|
5
|
|
|
use App\Models\Teacher; |
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Illuminate\Support\Facades\Auth; |
8
|
|
|
|
9
|
|
|
class TeacherRepository implements TeacherRepositoryInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Get all Teachers. |
14
|
|
|
* |
15
|
|
|
* @param int|null $recordsPerPage |
16
|
|
|
* @param array|null $searchParameters |
17
|
|
|
* |
18
|
|
|
* @return \App\Models\Teacher[]|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Collection |
19
|
|
|
*/ |
20
|
1 |
|
public function getAll(int $recordsPerPage = null, array $searchParameters = null) |
21
|
|
|
{ |
22
|
1 |
|
$query = Teacher::orderBy('name', 'desc'); |
23
|
|
|
|
24
|
1 |
|
if (!is_null($searchParameters)) { |
25
|
|
|
if (!empty($searchParameters['name'])) { |
26
|
|
|
$query->where( |
27
|
|
|
'name', |
28
|
|
|
'like', |
29
|
|
|
'%' . $searchParameters['name'] . '%' |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
if (!empty($searchParameters['surname'])) { |
33
|
|
|
$query->where( |
34
|
|
|
'surname', |
35
|
|
|
'like', |
36
|
|
|
'%' . $searchParameters['surname'] . '%' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
if (!empty($searchParameters['countryId'])) { |
40
|
|
|
$query->where('country_id', $searchParameters['countryId']); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
if ($recordsPerPage) { |
|
|
|
|
45
|
1 |
|
$results = $query->paginate($recordsPerPage)->withQueryString(); |
46
|
|
|
} else { |
47
|
|
|
$results = $query->get(); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
return $results; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get Teacher by id |
55
|
|
|
* |
56
|
|
|
* @param int $id |
57
|
|
|
* |
58
|
|
|
* @return Teacher |
59
|
|
|
*/ |
60
|
2 |
|
public function getById(int $id): Teacher |
61
|
|
|
{ |
62
|
2 |
|
return Teacher::findOrFail($id); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get Teacher by slug |
67
|
|
|
* |
68
|
|
|
* @param string $teacherSlug |
69
|
|
|
* @return Teacher |
70
|
|
|
*/ |
71
|
|
|
public function getBySlug(string $teacherSlug): Teacher |
72
|
1 |
|
{ |
73
|
|
|
return Teacher::where('slug', $teacherSlug)->first(); |
74
|
1 |
|
} |
75
|
1 |
|
|
76
|
|
|
/** |
77
|
|
|
* Store Teacher |
78
|
1 |
|
* |
79
|
|
|
* @param array $data |
80
|
1 |
|
* |
81
|
|
|
* @return Teacher |
82
|
1 |
|
*/ |
83
|
|
|
public function store(array $data): Teacher |
84
|
|
|
{ |
85
|
|
|
$teacher = new Teacher(); |
86
|
|
|
$teacher = self::assignDataAttributes($teacher, $data); |
|
|
|
|
87
|
|
|
|
88
|
|
|
// Creator - Logged user id or 1 for factories |
89
|
|
|
$teacher->user_id = !is_null(Auth::id()) ? Auth::id() : 1; |
|
|
|
|
90
|
|
|
|
91
|
|
|
$teacher->save(); |
92
|
|
|
|
93
|
1 |
|
return $teacher->fresh(); |
94
|
|
|
} |
95
|
1 |
|
|
96
|
1 |
|
/** |
97
|
|
|
* Update Teacher |
98
|
1 |
|
* |
99
|
|
|
* @param array $data |
100
|
1 |
|
* @param int $id |
101
|
|
|
* |
102
|
|
|
* @return Teacher |
103
|
|
|
*/ |
104
|
|
|
public function update(array $data, int $id): Teacher |
105
|
|
|
{ |
106
|
|
|
$teacher = $this->getById($id); |
107
|
|
|
$teacher = self::assignDataAttributes($teacher, $data); |
|
|
|
|
108
|
|
|
|
109
|
1 |
|
$teacher->update(); |
110
|
|
|
|
111
|
1 |
|
return $teacher; |
112
|
1 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Delete Teacher |
116
|
|
|
* |
117
|
|
|
* @param int $id |
118
|
|
|
* @return void |
119
|
|
|
*/ |
120
|
|
|
public function delete(int $id): void |
121
|
|
|
{ |
122
|
2 |
|
Teacher::destroy($id); |
123
|
|
|
} |
124
|
2 |
|
|
125
|
2 |
|
/** |
126
|
2 |
|
* Assign the attributes of the data array to the object |
127
|
2 |
|
* |
128
|
2 |
|
* @param \App\Models\Teacher $teacher |
129
|
2 |
|
* @param array $data |
130
|
2 |
|
* |
131
|
2 |
|
* @return \App\Models\Teacher |
132
|
2 |
|
*/ |
133
|
|
|
public function assignDataAttributes(Teacher $teacher, array $data): Teacher |
134
|
2 |
|
{ |
135
|
|
|
$teacher->country_id = $data['country_id'] ?? null; |
|
|
|
|
136
|
|
|
$teacher->name = $data['name']; |
137
|
|
|
$teacher->surname = $data['surname'] ?? null; |
138
|
|
|
$teacher->bio = $data['bio'] ?? null; |
139
|
|
|
$teacher->year_starting_practice = $data['year_starting_practice'] ?? null; |
140
|
|
|
$teacher->year_starting_teach = $data['year_starting_teach'] ?? null; |
141
|
|
|
$teacher->significant_teachers = $data['significant_teachers'] ?? null; |
142
|
|
|
$teacher->website = $data['website'] ?? null; |
143
|
|
|
$teacher->facebook = $data['facebook'] ?? null; |
144
|
|
|
|
145
|
|
|
return $teacher; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: