|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Insight; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Illuminate\Support\Facades\Config; |
|
8
|
|
|
use Mcamara\LaravelLocalization\Facades\LaravelLocalization; |
|
9
|
|
|
|
|
10
|
|
|
class InsightRepository implements InsightRepositoryInterface |
|
11
|
|
|
{ |
|
12
|
|
|
public function getAll( |
|
13
|
1 |
|
int $recordsPerPage = null, |
|
14
|
|
|
array $searchParameters = null |
|
15
|
|
|
) { |
|
16
|
|
|
$query = Insight::orderBy('created_at', 'desc'); |
|
17
|
|
|
|
|
18
|
1 |
|
if (!is_null($searchParameters)) { |
|
19
|
|
|
if (!empty($searchParameters['title'])) { |
|
20
|
1 |
|
$query->where( |
|
21
|
|
|
'title', |
|
22
|
|
|
'like', |
|
23
|
|
|
'%' . $searchParameters['title'] . '%' |
|
24
|
|
|
); |
|
25
|
|
|
} |
|
26
|
|
|
if (!is_null($searchParameters['is_published'])) { |
|
27
|
|
|
$query->where('is_published', $searchParameters['is_published']); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
1 |
|
|
|
31
|
1 |
|
if ($recordsPerPage) { |
|
|
|
|
|
|
32
|
|
|
$results = $query->paginate($recordsPerPage)->withQueryString(); |
|
33
|
|
|
} else { |
|
34
|
|
|
$results = $query->get(); |
|
35
|
|
|
} |
|
36
|
1 |
|
|
|
37
|
|
|
return $results; |
|
38
|
|
|
} |
|
39
|
2 |
|
|
|
40
|
|
|
public function getById($insightId) |
|
41
|
2 |
|
{ |
|
42
|
|
|
return Insight::findOrFail($insightId); |
|
43
|
|
|
} |
|
44
|
1 |
|
|
|
45
|
|
|
public function store(array $data) |
|
46
|
1 |
|
{ |
|
47
|
1 |
|
$insight = new Insight(); |
|
48
|
|
|
$insight = self::assignDataAttributes($insight, $data); |
|
|
|
|
|
|
49
|
1 |
|
|
|
50
|
|
|
$insight->save(); |
|
51
|
1 |
|
|
|
52
|
1 |
|
$insight->tags()->sync($data['tag_ids'] ?? null); |
|
53
|
|
|
|
|
54
|
1 |
|
return $insight->fresh(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
1 |
|
public function update(array $data, int $id) |
|
58
|
|
|
{ |
|
59
|
1 |
|
$insight = $this->getById($id); |
|
60
|
1 |
|
$insight = self::assignDataAttributes($insight, $data); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
1 |
|
$insight->update(); |
|
63
|
|
|
|
|
64
|
1 |
|
return $insight; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
1 |
|
public function delete(int $id) |
|
68
|
|
|
{ |
|
69
|
1 |
|
Insight::destroy($id); |
|
70
|
1 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Assign the attributes of the data array to the object |
|
74
|
|
|
* |
|
75
|
|
|
* @param \App\Models\Insight $insight |
|
76
|
|
|
* @param array $data |
|
77
|
|
|
* |
|
78
|
|
|
* @return \App\Models\Insight |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function assignDataAttributes(Insight $insight, array $data): Insight |
|
81
|
|
|
{ |
|
82
|
2 |
|
$insight->title = $data['title'] ?? null; |
|
83
|
2 |
|
$insight->body = $data['body'] ?? null; |
|
84
|
|
|
$insight->is_published = (isset($data['is_published'])) ? 1 : 0; |
|
85
|
|
|
|
|
86
|
2 |
|
return $insight; |
|
87
|
2 |
|
} |
|
88
|
2 |
|
|
|
89
|
2 |
|
/** |
|
90
|
|
|
* Get the last 5 Insights. |
|
91
|
|
|
* |
|
92
|
|
|
* @param int $numberOfInsights |
|
93
|
2 |
|
* |
|
94
|
|
|
* @return Collection |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getLatest(int $numberOfInsights): Collection |
|
97
|
|
|
{ |
|
98
|
|
|
return Insight::orderBy('created_at', 'desc')->take($numberOfInsights)->get(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: