1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repositories; |
4
|
|
|
|
5
|
|
|
use App\Models\Glossary; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
8
|
|
|
|
9
|
|
|
class GlossaryRepository implements GlossaryRepositoryInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Get all Glossary terms. |
14
|
|
|
* |
15
|
|
|
* @param int|null $recordsPerPage |
16
|
|
|
* @param array|null $searchParameters |
17
|
|
|
* |
18
|
|
|
* @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator |
19
|
|
|
*/ |
20
|
1 |
|
public function getAll(int $recordsPerPage = null, array $searchParameters = null) |
21
|
|
|
{ |
22
|
1 |
|
$query = Glossary::orderBy('created_at', 'desc'); |
23
|
|
|
|
24
|
1 |
|
if (!is_null($searchParameters)) { |
25
|
|
|
if (!empty($searchParameters['term'])) { |
26
|
|
|
$query->where( |
27
|
|
|
'term', |
28
|
|
|
'like', |
29
|
|
|
'%' . $searchParameters['term'] . '%' |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
if (!is_null($searchParameters['is_published'])) { |
33
|
|
|
$query->where('is_published', $searchParameters['is_published']); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
if ($recordsPerPage) { |
|
|
|
|
38
|
1 |
|
$results = $query->paginate($recordsPerPage)->withQueryString(); |
39
|
|
|
} else { |
40
|
|
|
$results = $query->get(); |
41
|
|
|
} |
42
|
|
|
|
43
|
1 |
|
return $results; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get all Glossary terms with their variants. |
48
|
|
|
* |
49
|
|
|
* @param int|null $recordsPerPage |
50
|
|
|
* @param array|null $searchParameters |
51
|
|
|
* |
52
|
2 |
|
* @return \Illuminate\Support\Collection|\Illuminate\Contracts\Pagination\LengthAwarePaginator |
53
|
|
|
*/ |
54
|
2 |
|
public function getAllWithVariants() |
55
|
|
|
{ |
56
|
|
|
return Glossary::with('variants') |
57
|
|
|
->where('is_published', 1) |
58
|
|
|
->get(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get Glossary term by id |
63
|
1 |
|
* |
64
|
|
|
* @param int $id |
65
|
1 |
|
* @return Glossary |
66
|
1 |
|
*/ |
67
|
|
|
public function getById(int $id): Glossary |
68
|
1 |
|
{ |
69
|
|
|
return Glossary::findOrFail($id); |
70
|
1 |
|
} |
71
|
|
|
|
72
|
1 |
|
/** |
73
|
|
|
* Get glossary term by slug |
74
|
|
|
* |
75
|
|
|
* @param string $glossarySlug |
76
|
|
|
* @return Glossary |
77
|
|
|
*/ |
78
|
|
|
public function getBySlug(string $glossarySlug): Glossary |
79
|
|
|
{ |
80
|
|
|
return Glossary::where('slug', $glossarySlug)->first(); |
81
|
|
|
} |
82
|
1 |
|
|
83
|
|
|
/** |
84
|
1 |
|
* Store Glossary term |
85
|
1 |
|
* |
86
|
|
|
* @param array $data |
87
|
1 |
|
* @return Glossary |
88
|
|
|
*/ |
89
|
1 |
|
public function store(array $data): Glossary |
90
|
1 |
|
{ |
91
|
1 |
|
$glossary = new Glossary(); |
92
|
|
|
$glossary = self::assignDataAttributes($glossary, $data); |
|
|
|
|
93
|
|
|
|
94
|
1 |
|
$glossary->save(); |
95
|
|
|
|
96
|
|
|
return $glossary->fresh(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Update Glossary term |
101
|
|
|
* |
102
|
|
|
* @param array $data |
103
|
1 |
|
* @param int $id |
104
|
|
|
* @return Glossary |
105
|
1 |
|
*/ |
106
|
1 |
|
public function update(array $data, int $id): Glossary |
107
|
|
|
{ |
108
|
|
|
$glossary = $this->getById($id); |
109
|
|
|
$glossary = self::assignDataAttributes($glossary, $data); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$glossary->update(); |
112
|
|
|
|
113
|
|
|
return $glossary; |
114
|
|
|
} |
115
|
|
|
|
116
|
2 |
|
/** |
117
|
|
|
* Delete Glossary term |
118
|
2 |
|
* |
119
|
2 |
|
* @param int $id |
120
|
2 |
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function delete(int $id): void |
123
|
2 |
|
{ |
124
|
2 |
|
Glossary::destroy($id); |
125
|
2 |
|
} |
126
|
2 |
|
|
127
|
2 |
|
/** |
128
|
|
|
* Assign the attributes of the data array to the object |
129
|
|
|
* |
130
|
|
|
* @param \App\Models\Glossary $glossary |
131
|
2 |
|
* @param array $data |
132
|
|
|
* |
133
|
|
|
* @return \App\Models\Glossary |
134
|
|
|
*/ |
135
|
|
|
public function assignDataAttributes(Glossary $glossary, array $data): Glossary |
136
|
|
|
{ |
137
|
|
|
$glossary->term = $data['term']; |
138
|
|
|
$glossary->definition = $data['definition']; |
139
|
|
|
$glossary->body = $data['body']; |
140
|
|
|
$glossary->question_type = $data['question_type']; |
141
|
|
|
$glossary->is_published = (isset($data['is_published'])) ? 1 : 0; |
142
|
|
|
|
143
|
|
|
// Translations |
144
|
|
|
foreach (LaravelLocalization::getSupportedLocales() as $countryCode => $countryAvTrans) { |
145
|
|
|
if ($countryCode != Config::get('app.fallback_locale')) { |
146
|
|
|
$glossary->setTranslation('term', $countryCode, $data['term_' . $countryCode] ?? null); |
147
|
|
|
$glossary->setTranslation('definition', $countryCode, $data['definition_' . $countryCode] ?? null); |
148
|
|
|
$glossary->setTranslation('body', $countryCode, $data['body_' . $countryCode] ?? null); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return $glossary; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
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: