Skip to content

Commit 1b496cb

Browse files
committed
Merge branch 'release/1.0.0'
2 parents b59be3d + 40afed2 commit 1b496cb

File tree

4 files changed

+54
-46
lines changed

4 files changed

+54
-46
lines changed

src/Actions/PaginateResults.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
namespace HDSSolutions\Laravel\API\Actions;
44

55
use Closure;
6-
use HDSSolutions\Laravel\API\Contracts\ResourceRequest;
76
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Collection;
8+
use Illuminate\Http\Request;
9+
use Illuminate\Pagination\LengthAwarePaginator;
810

911
final class PaginateResults {
1012

1113
public function __construct(
12-
private ResourceRequest $request,
14+
private Request $request,
1315
) {}
1416

15-
public function handle(Builder $query, Closure $next): void {
16-
$next($this->request->boolean('all')
17+
public function handle(Builder $query, Closure $next): Collection | LengthAwarePaginator {
18+
return $next($this->request->boolean('all')
1719
? $query->get()
1820
: $query->paginate()->withQueryString()
1921
);

src/ResourceFilters.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Closure;
66
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Collection;
78
use Illuminate\Http\Request;
89
use Illuminate\Http\Response;
10+
use Illuminate\Pagination\LengthAwarePaginator;
911
use RuntimeException;
1012

1113
abstract class ResourceFilters {
@@ -23,7 +25,7 @@ abstract class ResourceFilters {
2325
'gte' => '>=',
2426
'ne' => '!=',
2527
'has' => 'like',
26-
'in' => null,
28+
'in' => 'in',
2729
];
2830

2931
/**
@@ -56,7 +58,7 @@ final public function __construct(
5658
protected Request $request,
5759
) {}
5860

59-
final public function handle(Builder $query, Closure $next): void {
61+
final public function handle(Builder $query, Closure $next): Builder | Collection | LengthAwarePaginator {
6062
foreach ($this->allowed_columns as $column => $operators) {
6163
// ignore filter if not specified in params
6264
if (is_null($param = $this->request->query($column))) {
@@ -99,7 +101,7 @@ final public function handle(Builder $query, Closure $next): void {
99101
}
100102
}
101103

102-
$next($query);
104+
return $next($query);
103105
}
104106

105107
private function addQueryFilter(Builder $query, string $column, string $operator, $value): void {
@@ -117,7 +119,7 @@ private function addQueryFilter(Builder $query, string $column, string $operator
117119
} elseif ($operator === 'in') {
118120
$query->whereIn(
119121
column: $this->column_mappings[ $column ] ?? $column,
120-
values: $value,
122+
values: $this->parseValue($operator, $value),
121123
);
122124

123125
} else {
@@ -138,6 +140,10 @@ private function parseValue(string $operator, $value) {
138140
return "%$value%";
139141
}
140142

143+
if ($operator === 'in' && !is_array($value)) {
144+
return explode(',', $value);
145+
}
146+
141147
return $value;
142148
}
143149

src/ResourceOrders.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use Closure;
66
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Collection;
78
use Illuminate\Http\Request;
89
use Illuminate\Http\Response;
10+
use Illuminate\Pagination\LengthAwarePaginator;
911
use InvalidArgumentException;
1012

1113
abstract class ResourceOrders {
@@ -28,15 +30,13 @@ final public function __construct(
2830
protected Request $request,
2931
) {}
3032

31-
final public function handle(Builder $query, Closure $next): void {
33+
final public function handle(Builder $query, Closure $next): Builder | Collection | LengthAwarePaginator {
3234
// check if query param was not defined
3335
if (null === $order = $this->request->query('order')) {
3436
// add default sorting fields
3537
$this->setDefaultOrder($query);
3638

37-
$next($query);
38-
39-
return;
39+
return $next($query);
4040
}
4141

4242
// must follow the syntax order[{index}][{direction}]={field}
@@ -56,7 +56,7 @@ final public function handle(Builder $query, Closure $next): void {
5656
$this->addQueryOrder($query, $value[$direction], $direction);
5757
}
5858

59-
$next($query);
59+
return $next($query);
6060
}
6161

6262
private function clean(array &$order): void {

src/ResourceRelations.php

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Closure;
66
use Illuminate\Database\Eloquent\Builder;
7+
use Illuminate\Database\Eloquent\Collection;
78
use Illuminate\Database\Eloquent\Relations\Relation;
89
use Illuminate\Http\Request;
910
use Illuminate\Http\Response;
11+
use Illuminate\Pagination\LengthAwarePaginator;
1012
use InvalidArgumentException;
1113

1214
abstract class ResourceRelations {
@@ -36,43 +38,41 @@ final public function __construct(
3638
protected Request $request,
3739
) {}
3840

39-
final public function handle(Builder $query, Closure $next): void {
41+
final public function handle(Builder $query, Closure $next): Builder | Collection | LengthAwarePaginator {
4042
// check if query param wasn't defined and just return
41-
if (null === $with = $this->request->query('with')) {
42-
$next($query);
43-
44-
return;
45-
}
46-
47-
// convert to array if it is a coma separated string
48-
if (is_string($with) && str_contains($with, ',')) {
49-
$with = explode(',', $with);
50-
}
51-
52-
// must be an array
53-
if ( !is_array($with)) {
54-
throw new InvalidArgumentException(
55-
message: 'Parameter "with" must be an array.',
56-
code: Response::HTTP_BAD_REQUEST,
57-
);
58-
}
59-
60-
foreach ($this->allowed_relations as $mapping => $relation_name) {
61-
if (is_int($mapping)) {
62-
$mapping = $relation_name;
43+
if (null !== $with = $this->request->query('with')) {
44+
// convert to array if it is a coma separated string
45+
if (is_string($with) && str_contains($with, ',')) {
46+
$with = explode(',', $with);
6347
}
6448

65-
// ignore relation if not specified in params
66-
if ( !in_array($mapping, $with, true)) {
67-
continue;
49+
// must be an array
50+
if ( !is_array($with)) {
51+
throw new InvalidArgumentException(
52+
message: 'Parameter "with" must be an array.',
53+
code: Response::HTTP_BAD_REQUEST,
54+
);
6855
}
6956

70-
// check if a method with the relation name exists
71-
if (method_exists($this, $method = explode('.', $mapping, 2)[0])) {
72-
// redirect relation to the custom method implementation
73-
$this->with[$relation_name] = fn(Relation $relation) => $this->$method($relation);
74-
} else {
75-
$this->with[] = $relation_name;
57+
foreach ($this->allowed_relations as $mapping => $relation_name) {
58+
if (is_int($mapping)) {
59+
$mapping = $relation_name;
60+
}
61+
62+
// ignore relation if not specified in params
63+
if ( !in_array($mapping, $with, true)) {
64+
continue;
65+
}
66+
67+
foreach ((array) $relation_name as $relationship_name) {
68+
// check if a method with the relation name exists
69+
if (method_exists($this, $method = explode('.', $mapping, 2)[0])) {
70+
// redirect relation to the custom method implementation
71+
$this->with[$relation_name] = fn(Relation $relation) => $this->$method($relation);
72+
} else {
73+
$this->with[] = $relationship_name;
74+
}
75+
}
7676
}
7777
}
7878

@@ -84,7 +84,7 @@ final public function handle(Builder $query, Closure $next): void {
8484
// append relation counts to the query
8585
$query->withCount($this->with_count);
8686

87-
$next($query);
87+
return $next($query);
8888
}
8989

9090
private function parseWiths(): void {

0 commit comments

Comments
 (0)