1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Repositories; |
4
|
|
|
|
5
|
|
|
use App\Models\Quote; |
6
|
|
|
use Illuminate\Support\Facades\Config; |
7
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
8
|
|
|
|
9
|
|
|
class QuoteRepository implements QuoteRepositoryInterface |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Get all Quote 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 = Quote::orderBy('created_at', 'desc'); |
23
|
|
|
|
24
|
1 |
|
if (!is_null($searchParameters)) { |
25
|
|
|
if (!empty($searchParameters['author'])) { |
26
|
|
|
$query->where( |
27
|
|
|
'author', |
28
|
|
|
'like', |
29
|
|
|
'%' . $searchParameters['author'] . '%' |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
if (!empty($searchParameters['description'])) { |
33
|
|
|
$query->where( |
34
|
|
|
'description', |
35
|
|
|
'like', |
36
|
|
|
'%' . $searchParameters['description'] . '%' |
37
|
|
|
); |
38
|
|
|
} |
39
|
|
|
if (!is_null($searchParameters['is_published'])) { |
40
|
|
|
$query->where('is_published', $searchParameters['is_published']); |
41
|
|
|
} |
42
|
|
|
if (!is_null($searchParameters['show_where'])) { |
43
|
|
|
$query->where('show_where', $searchParameters['show_where']); |
44
|
1 |
|
} |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
if ($recordsPerPage) { |
|
|
|
|
48
|
|
|
$results = $query->paginate($recordsPerPage)->withQueryString(); |
49
|
|
|
} else { |
50
|
1 |
|
$results = $query->get(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $results; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get Quote term by id |
58
|
|
|
* |
59
|
2 |
|
* @param int $id |
60
|
|
|
* @return Quote |
61
|
2 |
|
*/ |
62
|
|
|
public function getById(int $id): Quote |
63
|
|
|
{ |
64
|
|
|
return Quote::findOrFail($id); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Store Quote term |
69
|
|
|
* |
70
|
1 |
|
* @param array $data |
71
|
|
|
* @return Quote |
72
|
1 |
|
*/ |
73
|
1 |
|
public function store(array $data): Quote |
74
|
|
|
{ |
75
|
1 |
|
$quote = new Quote(); |
76
|
|
|
$quote = self::assignDataAttributes($quote, $data); |
|
|
|
|
77
|
1 |
|
|
78
|
|
|
$quote->save(); |
79
|
|
|
|
80
|
|
|
return $quote->fresh(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Update Quote term |
85
|
|
|
* |
86
|
|
|
* @param array $data |
87
|
1 |
|
* @param int $id |
88
|
|
|
* @return Quote |
89
|
1 |
|
*/ |
90
|
1 |
|
public function update(array $data, int $id): Quote |
91
|
|
|
{ |
92
|
1 |
|
$quote = $this->getById($id); |
93
|
|
|
$quote = self::assignDataAttributes($quote, $data); |
|
|
|
|
94
|
1 |
|
|
95
|
|
|
$quote->update(); |
96
|
|
|
|
97
|
|
|
return $quote; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Delete Quote term |
102
|
|
|
* |
103
|
1 |
|
* @param int $id |
104
|
|
|
* @return void |
105
|
1 |
|
*/ |
106
|
1 |
|
public function delete(int $id): void |
107
|
|
|
{ |
108
|
|
|
Quote::destroy($id); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Assign the attributes of the data array to the object |
113
|
|
|
* |
114
|
|
|
* @param \App\Models\Quote $quote |
115
|
|
|
* @param array $data |
116
|
2 |
|
* |
117
|
|
|
* @return \App\Models\Quote |
118
|
2 |
|
*/ |
119
|
2 |
|
public function assignDataAttributes(Quote $quote, array $data): Quote |
120
|
|
|
{ |
121
|
|
|
$quote->author = $data['author']; |
122
|
2 |
|
$quote->description = $data['description']; |
123
|
2 |
|
$quote->is_published = (isset($data['is_published'])) ? 1 : 0; |
124
|
2 |
|
$quote->show_where = $data['show_where']; |
125
|
|
|
|
126
|
|
|
// Translations |
127
|
|
|
foreach (LaravelLocalization::getSupportedLocales() as $countryCode => $countryAvTrans) { |
128
|
2 |
|
if ($countryCode != Config::get('app.fallback_locale')) { |
129
|
|
|
$quote->setTranslation('description', $countryCode, $data['description_' . $countryCode] ?? null); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $quote; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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: