Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimpf committed Aug 18, 2023
2 parents b59be3d + 40afed2 commit 1b496cb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
10 changes: 6 additions & 4 deletions src/Actions/PaginateResults.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
namespace HDSSolutions\Laravel\API\Actions;

use Closure;
use HDSSolutions\Laravel\API\Contracts\ResourceRequest;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;

final class PaginateResults {

public function __construct(
private ResourceRequest $request,
private Request $request,
) {}

public function handle(Builder $query, Closure $next): void {
$next($this->request->boolean('all')
public function handle(Builder $query, Closure $next): Collection | LengthAwarePaginator {
return $next($this->request->boolean('all')
? $query->get()
: $query->paginate()->withQueryString()
);
Expand Down
14 changes: 10 additions & 4 deletions src/ResourceFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Pagination\LengthAwarePaginator;
use RuntimeException;

abstract class ResourceFilters {
Expand All @@ -23,7 +25,7 @@ abstract class ResourceFilters {
'gte' => '>=',
'ne' => '!=',
'has' => 'like',
'in' => null,
'in' => 'in',
];

/**
Expand Down Expand Up @@ -56,7 +58,7 @@ final public function __construct(
protected Request $request,
) {}

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

$next($query);
return $next($query);
}

private function addQueryFilter(Builder $query, string $column, string $operator, $value): void {
Expand All @@ -117,7 +119,7 @@ private function addQueryFilter(Builder $query, string $column, string $operator
} elseif ($operator === 'in') {
$query->whereIn(
column: $this->column_mappings[ $column ] ?? $column,
values: $value,
values: $this->parseValue($operator, $value),
);

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

if ($operator === 'in' && !is_array($value)) {
return explode(',', $value);
}

return $value;
}

Expand Down
10 changes: 5 additions & 5 deletions src/ResourceOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Pagination\LengthAwarePaginator;
use InvalidArgumentException;

abstract class ResourceOrders {
Expand All @@ -28,15 +30,13 @@ final public function __construct(
protected Request $request,
) {}

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

$next($query);

return;
return $next($query);
}

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

$next($query);
return $next($query);
}

private function clean(array &$order): void {
Expand Down
66 changes: 33 additions & 33 deletions src/ResourceRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Pagination\LengthAwarePaginator;
use InvalidArgumentException;

abstract class ResourceRelations {
Expand Down Expand Up @@ -36,43 +38,41 @@ final public function __construct(
protected Request $request,
) {}

final public function handle(Builder $query, Closure $next): void {
final public function handle(Builder $query, Closure $next): Builder | Collection | LengthAwarePaginator {
// check if query param wasn't defined and just return
if (null === $with = $this->request->query('with')) {
$next($query);

return;
}

// convert to array if it is a coma separated string
if (is_string($with) && str_contains($with, ',')) {
$with = explode(',', $with);
}

// must be an array
if ( !is_array($with)) {
throw new InvalidArgumentException(
message: 'Parameter "with" must be an array.',
code: Response::HTTP_BAD_REQUEST,
);
}

foreach ($this->allowed_relations as $mapping => $relation_name) {
if (is_int($mapping)) {
$mapping = $relation_name;
if (null !== $with = $this->request->query('with')) {
// convert to array if it is a coma separated string
if (is_string($with) && str_contains($with, ',')) {
$with = explode(',', $with);
}

// ignore relation if not specified in params
if ( !in_array($mapping, $with, true)) {
continue;
// must be an array
if ( !is_array($with)) {
throw new InvalidArgumentException(
message: 'Parameter "with" must be an array.',
code: Response::HTTP_BAD_REQUEST,
);
}

// check if a method with the relation name exists
if (method_exists($this, $method = explode('.', $mapping, 2)[0])) {
// redirect relation to the custom method implementation
$this->with[$relation_name] = fn(Relation $relation) => $this->$method($relation);
} else {
$this->with[] = $relation_name;
foreach ($this->allowed_relations as $mapping => $relation_name) {
if (is_int($mapping)) {
$mapping = $relation_name;
}

// ignore relation if not specified in params
if ( !in_array($mapping, $with, true)) {
continue;
}

foreach ((array) $relation_name as $relationship_name) {
// check if a method with the relation name exists
if (method_exists($this, $method = explode('.', $mapping, 2)[0])) {
// redirect relation to the custom method implementation
$this->with[$relation_name] = fn(Relation $relation) => $this->$method($relation);
} else {
$this->with[] = $relationship_name;
}
}
}
}

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

$next($query);
return $next($query);
}

private function parseWiths(): void {
Expand Down

0 comments on commit 1b496cb

Please sign in to comment.