Skip to content

Commit 3fde242

Browse files
committed
Merge branch 'release/1.0.0-beta.3' into main
2 parents 0bbeef3 + 5471a9d commit 3fde242

File tree

19 files changed

+420
-29
lines changed

19 files changed

+420
-29
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
All notable changes to this project will be documented in this file. This project adheres to
44
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
55

6+
## [1.0.0-beta.3] - 2021-04-26
7+
8+
### Added
9+
10+
- [#14](https://github.com/laravel-json-api/laravel/issues/14) Additional sort parameters can now be added to Eloquent
11+
schemas. Previously only sortable attributes were supported. These new classes are added to schemas in the
12+
`sortables()` method.
13+
- Eloquent schemas now support a default sort order via the `$defaultSort` property.
14+
- New generator command `jsonapi:sort-field` to create a custom sort field class.
15+
- [#74](https://github.com/laravel-json-api/laravel/issues/74) Developers can now add default include paths to the query
16+
request classes (e.g. `PostQuery` and `PostCollectionQuery`) via the `$defaultIncludePaths` property. These include
17+
paths are used if the client does not provide any include paths.
18+
619
## [1.0.0-beta.2] - 2021-04-20
720

821
### Added

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
"require": {
2626
"php": "^7.4|^8.0",
2727
"ext-json": "*",
28-
"laravel-json-api/core": "^1.0.0-beta.2",
29-
"laravel-json-api/eloquent": "^1.0.0-beta.2",
28+
"laravel-json-api/core": "^1.0.0-beta.3",
29+
"laravel-json-api/eloquent": "^1.0.0-beta.4",
3030
"laravel-json-api/encoder-neomerx": "^1.0.0-beta.1",
3131
"laravel-json-api/exceptions": "^1.0.0-beta.2",
3232
"laravel-json-api/spec": "^1.0.0-beta.1",
33-
"laravel-json-api/validation": "^1.0.0-beta.1",
33+
"laravel-json-api/validation": "^1.0.0-beta.2",
3434
"laravel/framework": "^8.0"
3535
},
3636
"require-dev": {

src/Console/MakeSortField.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
/*
3+
* Copyright 2021 Cloud Creativity Limited
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
declare(strict_types=1);
19+
20+
namespace LaravelJsonApi\Laravel\Console;
21+
22+
use Illuminate\Console\GeneratorCommand as BaseGeneratorCommand;
23+
use Symfony\Component\Console\Input\InputOption;
24+
25+
class MakeSortField extends BaseGeneratorCommand
26+
{
27+
28+
use Concerns\ResolvesStub;
29+
30+
/**
31+
* @var string
32+
*/
33+
protected $name = 'jsonapi:sort-field';
34+
35+
/**
36+
* @var string
37+
*/
38+
protected $description = 'Create a new JSON:API sort field.';
39+
40+
/**
41+
* @var string
42+
*/
43+
protected $type = 'JSON:API sort field';
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
protected function getStub()
49+
{
50+
return $this->resolveStubPath('sort-field.stub');
51+
}
52+
53+
/**
54+
* @inheritDoc
55+
*/
56+
protected function getDefaultNamespace($rootNamespace)
57+
{
58+
$jsonApi = trim(config('jsonapi.namespace') ?: 'JsonApi', '\\');
59+
60+
return $rootNamespace . '\\' . $jsonApi . '\\' . 'Sorting';
61+
}
62+
63+
/**
64+
* Get the console command options.
65+
*
66+
* @return array
67+
*/
68+
protected function getOptions()
69+
{
70+
return [
71+
['force', null, InputOption::VALUE_NONE, 'Create the class even if the sort field already exists'],
72+
];
73+
}
74+
75+
}

src/Http/Controllers/Actions/FetchMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public function index(Route $route, StoreContract $store)
6161
$response = $this->searched($data, $request);
6262
}
6363

64-
return $response ?: new DataResponse($data);
64+
return $response ?: DataResponse::make($data)->withQueryParameters($request);
6565
}
6666
}

src/Http/Controllers/Actions/FetchOne.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,6 @@ public function show(Route $route, StoreContract $store)
6161
$response = $this->read($model, $request);
6262
}
6363

64-
return $response ?: new DataResponse($model);
64+
return $response ?: DataResponse::make($model)->withQueryParameters($request);
6565
}
6666
}

src/Http/Controllers/Actions/FetchRelated.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ public function showRelated(Route $route, StoreContract $store)
7676
$response = $this->{$hook}($model, $data, $request);
7777
}
7878

79-
return $response ?: new RelatedResponse(
79+
return $response ?: RelatedResponse::make(
8080
$model,
8181
$relation->name(),
8282
$data,
83-
);
83+
)->withQueryParameters($request);
8484
}
8585
}

src/Http/Controllers/Actions/FetchRelationship.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ public function showRelationship(Route $route, StoreContract $store)
7676
$response = $this->{$hook}($model, $data, $request);
7777
}
7878

79-
return $response ?: new RelationshipResponse(
79+
return $response ?: RelationshipResponse::make(
8080
$model,
8181
$relation->name(),
8282
$data
83-
);
83+
)->withQueryParameters($request);
8484
}
8585
}

src/Http/Controllers/Actions/Store.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,6 @@ public function store(Route $route, StoreContract $store)
7171
$response = $this->saved($model, $request, $query);
7272
}
7373

74-
return $response ?: new DataResponse($model);
74+
return $response ?: DataResponse::make($model)->withQueryParameters($query);
7575
}
7676
}

src/Http/Controllers/Actions/Update.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ public function update(Route $route, StoreContract $store)
7373
$response = $this->saved($model, $request, $query);
7474
}
7575

76-
return $response ?: new DataResponse($model);
76+
return $response ?: DataResponse::make($model)->withQueryParameters($query);
7777
}
7878
}

src/Http/Controllers/Actions/UpdateRelationship.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public function updateRelationship(Route $route, StoreContract $store)
8181
$response = $this->{$hook}($model, $result, $request, $query);
8282
}
8383

84-
return $response ?: new RelationshipResponse(
84+
return $response ?: RelationshipResponse::make(
8585
$model,
8686
$fieldName,
8787
$result
88-
);
88+
)->withQueryParameters($query);
8989
}
9090
}

src/Http/Requests/RequestResolver.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,24 @@ public function __invoke(string $resourceType, bool $allowNull = false): ?FormRe
9595
{
9696
$app = app();
9797

98-
try {
99-
$fqn = $this->custom($resourceType) ?: Str::replaceLast('Schema', $this->type, get_class(
100-
$app->make(SchemaContainer::class)->schemaFor($resourceType)
101-
));
102-
103-
if (!class_exists($fqn) && !$app->bound($fqn)) {
104-
if (true === $allowNull) {
105-
return null;
106-
} else if (isset(self::$defaults[$this->type])) {
107-
$fqn = self::$defaults[$this->type];
108-
}
98+
$fqn = $this->custom($resourceType) ?: Str::replaceLast('Schema', $this->type, get_class(
99+
$app->make(SchemaContainer::class)->schemaFor($resourceType)
100+
));
101+
102+
if (!class_exists($fqn) && !$app->bound($fqn)) {
103+
if (true === $allowNull) {
104+
return null;
105+
} else if (isset(self::$defaults[$this->type])) {
106+
$fqn = self::$defaults[$this->type];
109107
}
108+
}
110109

110+
try {
111111
return $app->make($fqn);
112112
} catch (BindingResolutionException $ex) {
113113
throw new LogicException(sprintf(
114-
'Unable to create request class of type [%s] for resource type %s.',
115-
$this->type,
114+
'Unable to create request class %s for resource type %s.',
115+
$fqn,
116116
$resourceType
117117
), 0, $ex);
118118
}

src/Http/Requests/ResourceQuery.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ class ResourceQuery extends FormRequest implements QueryParameters
5252
self::JSON_API_MEDIA_TYPE,
5353
];
5454

55+
/**
56+
* The include paths to use if the client provides none.
57+
*
58+
* @var string[]|null
59+
*/
60+
protected ?array $defaultIncludePaths = null;
61+
5562
/**
5663
* Specify the callback to use to guess the request class for querying many resources.
5764
*
@@ -149,7 +156,7 @@ public function includePaths(): ?IncludePaths
149156
return IncludePaths::fromString($data['include'] ?: '');
150157
}
151158

152-
return null;
159+
return IncludePaths::nullable($this->defaultIncludePaths());
153160
}
154161

155162
/**
@@ -222,6 +229,16 @@ public function unrecognisedParameters(): array
222229
])->all();
223230
}
224231

232+
/**
233+
* Get the default include paths to use if the client has provided none.
234+
*
235+
* @return string[]|null
236+
*/
237+
protected function defaultIncludePaths(): ?array
238+
{
239+
return $this->defaultIncludePaths;
240+
}
241+
225242
/**
226243
* @return void
227244
*/

src/ServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function boot(Router $router): void
5555
Console\MakeResource::class,
5656
Console\MakeSchema::class,
5757
Console\MakeServer::class,
58+
Console\MakeSortField::class,
5859
Console\StubPublish::class,
5960
]);
6061
}

stubs/sort-field.stub

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace {{ namespace }};
4+
5+
use LaravelJsonApi\Eloquent\Contracts\SortField;
6+
7+
class {{ class }} implements SortField
8+
{
9+
10+
/**
11+
* @var string
12+
*/
13+
private string $name;
14+
15+
/**
16+
* Create a new sort field.
17+
*
18+
* @param string $name
19+
* @param string|null $column
20+
* @return {{ class }}
21+
*/
22+
public static function make(string $name): self
23+
{
24+
return new static($name);
25+
}
26+
27+
/**
28+
* {{ class }} constructor.
29+
*
30+
* @param string $name
31+
*/
32+
public function __construct(string $name)
33+
{
34+
$this->name = $name;
35+
}
36+
37+
/**
38+
* Get the name of the sort field.
39+
*
40+
* @return string
41+
*/
42+
public function sortField(): string
43+
{
44+
return $this->name;
45+
}
46+
47+
/**
48+
* Apply the sort order to the query.
49+
*
50+
* @param \Illuminate\Database\Eloquent\Builder $query
51+
* @param string $direction
52+
* @return \Illuminate\Database\Eloquent\Builder
53+
*/
54+
public function sort($query, string $direction = 'asc')
55+
{
56+
// @TODO
57+
}
58+
59+
}

tests/dummy/app/JsonApi/V1/Posts/PostSchema.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
3535
use LaravelJsonApi\Eloquent\Schema;
3636
use LaravelJsonApi\Eloquent\SoftDeletes;
37+
use LaravelJsonApi\Eloquent\Sorting\SortCountable;
3738
use LaravelJsonApi\HashIds\HashId;
3839

3940
class PostSchema extends Schema
@@ -93,6 +94,16 @@ public function filters(): array
9394
];
9495
}
9596

97+
/**
98+
* @inheritDoc
99+
*/
100+
public function sortables(): iterable
101+
{
102+
return [
103+
SortCountable::make($this, 'comments'),
104+
];
105+
}
106+
96107
/**
97108
* @inheritDoc
98109
*/

0 commit comments

Comments
 (0)