Skip to content

Commit 007bcea

Browse files
author
Lupacescu Eduard
authored
Merge pull request #52 from binaryk/search-coverage
Search coverage
2 parents 867974f + be8748d commit 007bcea

21 files changed

+748
-312
lines changed

src/Commands/stubs/repository.stub

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,4 @@ class DummyClass extends Repository
1313
* @var string
1414
*/
1515
public static $model = 'DummyFullModel';
16-
17-
/**
18-
* The single value that should be used to represent the repository when being displayed.
19-
*
20-
* @var string
21-
*/
22-
public static $title = 'id';
23-
24-
/**
25-
* The columns that should be searched.
26-
*
27-
* @var array
28-
*/
29-
public static $search = [
30-
'id',
31-
];
32-
33-
/**
34-
* Get the fields displayed by the repository.
35-
*
36-
* @param \Illuminate\Http\Request $request
37-
* @return array
38-
*/
39-
public function fields(Request $request)
40-
{
41-
return [
42-
ID::make()->sortable(),
43-
];
44-
}
4516
}

src/Contracts/RestifySearchable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Binaryk\LaravelRestify\Contracts;
44

5-
use Illuminate\Http\Request;
5+
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
66

77
/**
88
* @author Eduard Lupacescu <[email protected]>
@@ -16,11 +16,11 @@ interface RestifySearchable
1616
const MATCH_INTEGER = 'integer';
1717

1818
/**
19-
* @param Request $request
19+
* @param RestifyRequest $request
2020
* @param array $fields
2121
* @return array
2222
*/
23-
public function serializeForIndex(Request $request, array $fields = []);
23+
public function serializeForIndex(RestifyRequest $request, array $fields = []);
2424

2525
/**
2626
* @return array

src/Controllers/RestController.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
66
use Binaryk\LaravelRestify\Exceptions\Guard\EntityNotFoundException;
77
use Binaryk\LaravelRestify\Exceptions\Guard\GatePolicy;
8+
use Binaryk\LaravelRestify\Exceptions\InstanceOfException;
89
use Binaryk\LaravelRestify\Http\Requests\RestifyRequest;
10+
use Binaryk\LaravelRestify\Repositories\Repository;
911
use Binaryk\LaravelRestify\Services\Search\SearchService;
1012
use Binaryk\LaravelRestify\Traits\PerformsQueries;
11-
use Illuminate\Config\Repository;
1213
use Illuminate\Config\Repository as Config;
1314
use Illuminate\Container\Container;
1415
use Illuminate\Contracts\Auth\Access\Gate;
@@ -84,8 +85,8 @@ public function config()
8485
{
8586
$container = Container::getInstance();
8687

87-
if (($this->config instanceof Repository) === false) {
88-
$this->config = $container->make(Repository::class);
88+
if (($this->config instanceof Config) === false) {
89+
$this->config = $container->make(Config::class);
8990
}
9091

9192
return $this->config;
@@ -130,18 +131,27 @@ protected function response($data = null, $status = 200, array $headers = [])
130131
* @param array $filters
131132
* @return array
132133
* @throws BindingResolutionException
134+
* @throws InstanceOfException
133135
*/
134136
public function search($modelClass, $filters = [])
135137
{
136138
$results = SearchService::instance()
137139
->setPredefinedFilters($filters)
138-
->search($this->request(), new $modelClass);
140+
->search($this->request(), $modelClass instanceof Repository ? $modelClass->model() : new $modelClass);
141+
139142
$results->tap(function ($query) {
140143
static::indexQuery($this->request(), $query);
141144
});
142145

146+
/**
147+
* @var \Illuminate\Pagination\Paginator
148+
*/
143149
$paginator = $results->paginate($this->request()->get('perPage') ?? ($modelClass::$defaultPerPage ?? RestifySearchable::DEFAULT_PER_PAGE));
144-
$items = $paginator->getCollection()->map->serializeForIndex($this->request());
150+
if ($modelClass instanceof Repository) {
151+
$items = $paginator->getCollection()->mapInto(get_class($modelClass))->map->serializeForIndex($this->request());
152+
} else {
153+
$items = $paginator->getCollection()->map->serializeForIndex($this->request());
154+
}
145155

146156
return array_merge($paginator->toArray(), [
147157
'data' => $items,
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Binaryk\LaravelRestify\Exceptions;
4+
5+
use Exception;
6+
7+
/**
8+
* @author Eduard Lupacescu <[email protected]>
9+
*/
10+
class InstanceOfException extends Exception
11+
{
12+
}

src/Http/Controllers/RepositoryIndexController.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ class RepositoryIndexController extends RepositoryController
1818
*/
1919
public function handle(RestifyRequest $request)
2020
{
21-
$resource = $request->repository();
22-
23-
$data = $this->search($resource::newModel());
21+
$data = $this->search($request->newRepository());
2422

2523
return $this->respond($data);
2624
}

src/Http/Requests/InteractWithRepositories.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,44 @@ public function rules()
6363
//
6464
];
6565
}
66+
67+
/**
68+
* Get the route handling the request.
69+
*
70+
* @param string|null $param
71+
* @param mixed $default
72+
* @return \Illuminate\Routing\Route|object|string
73+
*/
74+
abstract public function route($param = null, $default = null);
75+
76+
/**
77+
* Get a new instance of the repository being requested.
78+
*
79+
* @return Repository
80+
* @throws EntityNotFoundException
81+
* @throws UnauthorizedException
82+
*/
83+
public function newRepository()
84+
{
85+
$repository = $this->repository();
86+
87+
return new $repository($repository::newModel());
88+
}
89+
90+
/**
91+
* Check if the route is resolved by the Repository class, or it uses the classical Models.
92+
* @return bool
93+
*/
94+
public function isResolvedByRestify()
95+
{
96+
try {
97+
$this->repository();
98+
99+
return true;
100+
} catch (EntityNotFoundException $e) {
101+
return false;
102+
} catch (UnauthorizedException $e) {
103+
return true;
104+
}
105+
}
66106
}

src/Repositories/Repository.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,45 @@
22

33
namespace Binaryk\LaravelRestify\Repositories;
44

5-
use Binaryk\LaravelRestify\Traits\AuthorizableModels;
5+
use Binaryk\LaravelRestify\Contracts\RestifySearchable;
6+
use Binaryk\LaravelRestify\Traits\InteractWithSearch;
67
use Illuminate\Database\Eloquent\Builder;
78
use Illuminate\Database\Eloquent\Model;
8-
use Illuminate\Http\Request;
9+
use Illuminate\Http\Resources\DelegatesToResource;
910
use Illuminate\Support\Str;
1011

1112
/**
1213
* @author Eduard Lupacescu <[email protected]>
1314
*/
14-
abstract class Repository
15+
abstract class Repository implements RestifySearchable
1516
{
16-
use AuthorizableModels;
17+
use InteractWithSearch,
18+
DelegatesToResource;
1719

1820
/**
21+
* This is named `resource` because of the forwarding properties from DelegatesToResource trait.
1922
* @var Model
2023
*/
21-
public $modelInstance;
24+
public $resource;
2225

2326
/**
2427
* Create a new resource instance.
2528
*
26-
* @param \Illuminate\Database\Eloquent\Model $resource
29+
* @param \Illuminate\Database\Eloquent\Model $model
2730
*/
28-
public function __construct($resource)
31+
public function __construct($model)
2932
{
30-
$this->modelInstance = $resource;
33+
$this->resource = $model;
3134
}
3235

33-
/**
34-
* Get the fields displayed by the resource.
35-
*
36-
* @param \Illuminate\Http\Request $request
37-
* @return array
38-
*/
39-
abstract public function fields(Request $request);
40-
4136
/**
4237
* Get the underlying model instance for the resource.
4338
*
4439
* @return \Illuminate\Database\Eloquent\Model
4540
*/
4641
public function model()
4742
{
48-
return $this->modelInstance;
43+
return $this->resource;
4944
}
5045

5146
/**
@@ -77,4 +72,14 @@ public static function query()
7772
{
7873
return static::newModel()->query();
7974
}
75+
76+
/**
77+
* @return array
78+
*/
79+
public function toArray()
80+
{
81+
$model = $this->model();
82+
83+
return $model->toArray();
84+
}
8085
}

0 commit comments

Comments
 (0)