Skip to content

Commit 0660982

Browse files
committed
Feat: Allow to provide repository with logics about data quering
1 parent 565979d commit 0660982

File tree

1 file changed

+222
-0
lines changed

1 file changed

+222
-0
lines changed

src/Logics/QueryLogic.php

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<?php
2+
3+
namespace Mawuekom\Repository\Logics;
4+
5+
trait QueryLogic
6+
{
7+
/**
8+
* Get all records with trashed
9+
*
10+
* @param boolean $paginate
11+
* @param boolean $toAPI
12+
* @param string|null $sortableColumn
13+
* @param array $columns
14+
* @param array $relations
15+
*
16+
* @return mixed
17+
*/
18+
public function getRecordsWithTrashed($paginate = true, $toAPI = false, $sortableColumn = null, $columns = ['*'], $relations = [])
19+
{
20+
$data = $this->getDatasQueryBuilder($toAPI, $sortableColumn, $relations)
21+
->withTrashed();
22+
23+
$results = $this ->resolveAndGetDatas($data, $paginate, $toAPI, $columns);
24+
25+
return $this ->validateRecords($results);
26+
}
27+
28+
/**
29+
* Get all records without trashed
30+
*
31+
* @param boolean $paginate
32+
* @param boolean $toAPI
33+
* @param string|null $sortableColumn
34+
* @param array $columns
35+
* @param array $relations
36+
*
37+
* @return mixed
38+
*/
39+
public function getRecordsWithoutTrashed($paginate = true, $toAPI = false, $sortableColumn = null, $columns = ['*'], $relations = [])
40+
{
41+
$data = $this->getDatasQueryBuilder($toAPI, $sortableColumn, $relations);
42+
43+
$results = $this ->resolveAndGetDatas($data, $paginate, $toAPI, $columns);
44+
45+
return $this ->validateRecords($results);
46+
}
47+
48+
/**
49+
* Get only trashed records
50+
*
51+
* @param boolean $paginate
52+
* @param boolean $toAPI
53+
* @param string|null $sortableColumn
54+
* @param array $columns
55+
* @param array $relations
56+
*
57+
* @return mixed
58+
*/
59+
public function getOnlyTrashedRecords($paginate = true, $toAPI = false, $sortableColumn = null, $columns = ['*'], $relations = [])
60+
{
61+
$data = $this->getDatasQueryBuilder($toAPI, $sortableColumn, $relations)
62+
->onlyTrashed();
63+
64+
$results = $this ->resolveAndGetDatas($data, $paginate, $toAPI, $columns);
65+
66+
return $this ->validateRecords($results);
67+
}
68+
69+
/**
70+
* Get results of searched records.
71+
*
72+
* @param string $searchTerm
73+
* @param bool $paginate
74+
* @param bool $toAPI
75+
* @param string|null $sortableColumn
76+
* @param array $columns
77+
* @param array $relations
78+
*
79+
* @return mixed
80+
*/
81+
public function getResultsOfSearchedRecords(string $searchTerm, $paginate = true, $toAPI = false, $sortableColumn = null, $columns = ['*'], $relations = [])
82+
{
83+
$data = $this->getDatasQueryBuilder($toAPI, $sortableColumn, $relations)
84+
->whereLike($this ->searchableFields(), $searchTerm);
85+
86+
$results = $this ->resolveAndGetDatas($data, $paginate, $toAPI, $columns);
87+
88+
return $this ->validateRecords($results);
89+
}
90+
91+
/**
92+
* Get data row by field's value
93+
*
94+
* @param string $field
95+
* @param string|null $value
96+
* @param bool $inTrashed
97+
* @param bool $toAPI
98+
* @param array $columns
99+
* @param array $relations
100+
*
101+
* @return void
102+
*/
103+
public function getDataRowByFieldValue(string $field, string $value = null, $inTrashed = false, $toAPI = false, $columns = ['*'], $relations = [])
104+
{
105+
$data = $this->getDatasQueryBuilder($toAPI, null, $relations)
106+
->where($field, '=', $value);
107+
108+
$result = ($inTrashed)
109+
? $data ->withTrashed() ->first($columns)
110+
: $data ->first($columns);
111+
112+
return $this ->validateDataRow($result);
113+
}
114+
115+
/**
116+
* Get Records Query Builder
117+
*
118+
* @param boolean $toAPI
119+
* @param string|null $sortableColumn
120+
* @param array $relations
121+
*
122+
* @return mixed
123+
*/
124+
public function getDatasQueryBuilder($toAPI = false, $sortableColumn = null, $relations = [])
125+
{
126+
$result = ($toAPI) ? $this ->toAPI() : $this ->applyModelMethods();
127+
128+
if ($sortableColumn !== null) {
129+
if (str_starts_with($sortableColumn, '-')) {
130+
$column = substr($sortableColumn, strlen('-')).'';
131+
$result = $result->orderBy($column, 'ASC');
132+
}
133+
134+
else {
135+
$result = $result->orderBy($sortableColumn, 'ASC');
136+
}
137+
}
138+
139+
$result = $result ->with($relations);
140+
141+
return $result;
142+
}
143+
144+
/**
145+
* Apply model's methods
146+
*
147+
* @return mixed
148+
*/
149+
public function applyModelMethods()
150+
{
151+
$this->applyCriteria();
152+
$this->applyScope();
153+
154+
$result = $this ->model;
155+
156+
$this->resetModel();
157+
$this->resetScope();
158+
159+
return $result;
160+
}
161+
162+
/**
163+
* Resolve and get datas
164+
*
165+
* @param mixed $data
166+
* @param bool $paginate
167+
* @param bool $toAPI
168+
* @param array $columns
169+
*
170+
* @return mixed
171+
*/
172+
public function resolveAndGetDatas($data, $paginate = true, $toAPI = false, $columns = ['*'])
173+
{
174+
if ($paginate) {
175+
$results = ($toAPI)
176+
? $data ->jsonPaginate() ->get($columns)
177+
: $data ->paginate(null, $columns);
178+
}
179+
180+
else {
181+
$results = $data ->get($columns);
182+
}
183+
184+
return $results;
185+
}
186+
187+
/**
188+
* Validate gotten records
189+
*
190+
* @param mixed $recordsCollection
191+
*
192+
* @return array
193+
*/
194+
public function validateRecords($recordsCollection): array
195+
{
196+
if ($recordsCollection ->count() > 0) {
197+
return success_response($recordsCollection);
198+
}
199+
200+
else {
201+
return failure_response();
202+
}
203+
}
204+
205+
/**
206+
* Validate gotten data row
207+
*
208+
* @param mixed $dataRow
209+
*
210+
* @return array
211+
*/
212+
public function validateDataRow($dataRow)
213+
{
214+
if ($dataRow !== null) {
215+
return success_response($dataRow);
216+
}
217+
218+
else {
219+
return failure_response();
220+
}
221+
}
222+
}

0 commit comments

Comments
 (0)