Skip to content

Commit 528d683

Browse files
committed
[Refactor] Add more changes with all tests now passing
1 parent a265f5e commit 528d683

File tree

22 files changed

+113
-63
lines changed

22 files changed

+113
-63
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"require": {
2525
"php": "^7.4|^8.0",
2626
"ext-json": "*",
27-
"laravel-json-api/neomerx-json-api": "^5.0",
27+
"laravel-json-api/neomerx-json-api": "dev-hotfix/5.0.1",
2828
"laravel/framework": "^8.76|^9.0",
2929
"nyholm/psr7": "^1.2",
3030
"ramsey/uuid": "^3.0|^4.0",

docs/basics/schemas.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ can locate a schema for each PHP class it encounters.
3535
## Creating Schemas
3636

3737
To generate a schema that extends, use the following command. The generated schema will extend
38-
`Neomerx\JsonApi\Schema\SchemaProvider`.
38+
`CloudCreativity\LaravelJsonApi\Schema\SchemaProvider`.
3939

4040
```bash
4141
php artisan make:json-api:schema <resource-type> [<api>]

docs/features/async.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ In the generated schema, you will need to add the `AsyncSchema` trait, for examp
7171

7272
```php
7373
use CloudCreativity\LaravelJsonApi\Queue\AsyncSchema;
74-
use Neomerx\JsonApi\Schema\SchemaProvider;
74+
use CloudCreativity\LaravelJsonApi\Schema\SchemaProvider;
7575

7676
class Schema extends SchemaProvider
7777
{

docs/fetching/inclusion.md

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

33
## Introduction
44

5-
This package supports the [inclusion of related resources](http://jsonapi.org/format/1.0/#fetching-includes).
5+
This package supports the [inclusion of related resources](http://jsonapi.org/format/1.0/#fetching-includes).
66
This allows a client to specify resources related to the primary data that should be included in the response.
77
The purpose is to allow the client to reduce the number of HTTP requests it needs to make to obtain all the data
88
it requires.
@@ -20,13 +20,13 @@ For this feature to work, you will need to:
2020
default validators do not allow include paths.
2121
2. Add relationships to the resource [Schema](../basics/schemas.md) and ensure they return data.
2222
3. For Eloquent models, define the translation of any JSON API include paths to eager load paths
23-
on the resource [Adapter](../basics/adapters.md).
23+
on the resource [Adapter](../basics/adapters.md).
2424

2525
These are all described in this chapter.
2626

2727
## The Include Query Parameter
2828

29-
Related resources are specified by the client using the `include` query parameter. This parameter
29+
Related resources are specified by the client using the `include` query parameter. This parameter
3030
contains a comma separated list of relationship paths that should be included. The response will be a
3131
[compound document](http://jsonapi.org/format/#document-compound-documents) where the primary data of the
3232
request is in the JSON's `data` member, and the related resources are in the `included` member.
@@ -37,7 +37,7 @@ resources in the same request:
3737
```http
3838
GET /api/posts?include=author,tags HTTP/1.1
3939
Accept: application/vnd.api+json
40-
```
40+
```
4141

4242
If these include paths are valid, then the client will receive the following response:
4343

@@ -79,7 +79,7 @@ Content-Type: application/vnd.api+json
7979
"links": {
8080
"self": "/api/posts/123/relationships/tags",
8181
"related": "/api/posts/123/tags"
82-
}
82+
}
8383
}
8484
},
8585
"links": {
@@ -130,7 +130,7 @@ relationship, the client could request the following:
130130
```http
131131
GET /api/posts?include=author.address,tags HTTP/1.1
132132
Accept: application/vnd.api+json
133-
```
133+
```
134134

135135
For this request, both the author `users` resource and the user's `addresses` resource would be present in
136136
the `included` member of the JSON document.
@@ -201,7 +201,7 @@ class Validators extends AbstractValidators
201201
'author.address',
202202
'tags'
203203
];
204-
204+
205205
// ...
206206
}
207207
```
@@ -235,7 +235,7 @@ the posts schema would have to return both the related author and the related ta
235235
```php
236236
namespace App\JsonApi\Posts;
237237

238-
use Neomerx\JsonApi\Schema\SchemaProvider;
238+
use CloudCreativity\LaravelJsonApi\Schema\SchemaProvider;
239239

240240
class Schema extends SchemaProvider
241241
{
@@ -278,7 +278,7 @@ method on the `users` schema.
278278

279279
## Eager Loading
280280

281-
The Eloquent adapter automatically converts JSON API include paths to Eloquent model
281+
The Eloquent adapter automatically converts JSON API include paths to Eloquent model
282282
[eager loading](https://laravel.com/docs/eloquent-relationships#eager-loading) paths.
283283
The JSON API path is converted to a camel-case path. For example, the JSON API path
284284
`author.current-address` is converted to the `author.currentAddress` Eloquent path.
@@ -299,7 +299,7 @@ class Adapter extends AbstractAdapter
299299
'author' => 'createdBy',
300300
'author.current-address' => 'createdBy.currentAddress',
301301
];
302-
302+
303303
// ...
304304
}
305305
```
@@ -324,7 +324,7 @@ requested as primary data:
324324
```php
325325
namespace App\JsonApi\Posts;
326326

327-
use Neomerx\JsonApi\Schema\SchemaProvider;
327+
use CloudCreativity\LaravelJsonApi\Schema\SchemaProvider;
328328

329329
class Schema extends SchemaProvider
330330
{
@@ -344,7 +344,7 @@ This would mean that the following request receive a response with `users` and `
344344
```http
345345
GET /api/posts HTTP/1.1
346346
Accept: application/vnd.api+json
347-
```
347+
```
348348

349349
### Default Path Eager Loading
350350

@@ -359,7 +359,7 @@ use CloudCreativity\LaravelJsonApi\Eloquent\AbstractAdapter;
359359
class Adapter extends AbstractAdapter
360360
{
361361
protected $defaultWith = ['author', 'tags'];
362-
362+
363363
// ...
364364
}
365365
```

src/Api/Api.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
use CloudCreativity\LaravelJsonApi\Resolver\NamespaceResolver;
3535
use GuzzleHttp\Client;
3636
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
37+
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
3738

3839
/**
3940
* Class Api
@@ -321,7 +322,7 @@ public function encoder($options = 0, $depth = 512)
321322
}
322323

323324
return $this->factory
324-
->createExtendedEncoder($this->getContainer())
325+
->createExtendedEncoder($this->getSchemaContainer())
325326
->withEncoderOptions($options);
326327
}
327328

@@ -358,7 +359,7 @@ public function client($clientHostOrOptions = [], array $options = [])
358359

359360
$client = ($clientHostOrOptions instanceof Client) ? $clientHostOrOptions : new Client($options);
360361

361-
return $this->factory->createClient($client, $this->getContainer(), $this->encoder());
362+
return $this->factory->createClient($client, $this->getSchemaContainer(), $this->encoder());
362363
}
363364

364365
/**
@@ -399,4 +400,13 @@ public function register(AbstractProvider $provider)
399400
$this->resolver->attach($provider->getResolver());
400401
}
401402

403+
/**
404+
* @return SchemaContainerInterface
405+
*/
406+
private function getSchemaContainer(): SchemaContainerInterface
407+
{
408+
return $this->factory->createLaravelSchemaContainer(
409+
$this->getContainer()
410+
);
411+
}
402412
}

src/Client/ClientSerializer.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function withFieldsets($resourceType, $fields)
143143
*/
144144
public function serialize($record, $meta = null, array $links = [])
145145
{
146-
$serializer = clone $this->serializer;
146+
$serializer = $this->setupSerializer();
147147
$serializer->withMeta($meta)->withLinks($links);
148148
$serialized = $serializer->serializeData($record, $this->createEncodingParameters());
149149
$resourceLinks = null;
@@ -173,7 +173,7 @@ public function serialize($record, $meta = null, array $links = [])
173173
*/
174174
public function serializeRelated($related, $meta = null, array $links = [])
175175
{
176-
$serializer = clone $this->serializer;
176+
$serializer = $this->setupSerializer();
177177
$serializer->withMeta($meta)->withLinks($links);
178178

179179
return $serializer->serializeIdentifiers($related);
@@ -291,4 +291,16 @@ protected function createEncodingParameters()
291291
$this->fieldsets
292292
);
293293
}
294+
295+
/**
296+
* @return SerializerInterface
297+
*/
298+
private function setupSerializer(): SerializerInterface
299+
{
300+
$serializer = clone $this->serializer;
301+
$serializer->withIncludedPaths($this->includePaths ?? []);
302+
$serializer->withFieldSets($this->fieldsets ?? []);
303+
304+
return $serializer;
305+
}
294306
}

src/Codec/Codec.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
use CloudCreativity\LaravelJsonApi\Contracts\ContainerInterface;
2121
use CloudCreativity\LaravelJsonApi\Encoder\Encoder;
2222
use CloudCreativity\LaravelJsonApi\Factories\Factory;
23+
use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
24+
use Illuminate\Support\Collection;
2325
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
24-
use Neomerx\JsonApi\Http\Headers\MediaType;
2526

2627
/**
2728
* Class Codec
@@ -133,9 +134,9 @@ public function encodes(string ...$mediaTypes): bool
133134
{
134135
$encoding = $this->getEncodingMediaType();
135136

136-
return collect($mediaTypes)->contains(function ($mediaType, $index) use ($encoding) {
137-
return $encoding->equalsTo(MediaType::parse($index, $mediaType));
138-
});
137+
return Collection::make($mediaTypes)->contains(
138+
fn($mediaType) => $encoding->equalsTo(MediaTypeParser::getInstance()->parse($mediaType))
139+
);
139140
}
140141

141142
/**
@@ -182,9 +183,9 @@ public function decodes(string ...$mediaTypes): bool
182183
return false;
183184
}
184185

185-
return collect($mediaTypes)->contains(function ($mediaType, $index) use ($decoding) {
186-
return $decoding->equalsTo(MediaType::parse($index, $mediaType));
187-
});
186+
return Collection::make($mediaTypes)->contains(
187+
static fn($mediaType) => $decoding->equalsTo(MediaTypeParser::getInstance()->parse($mediaType))
188+
);
188189
}
189190

190191
/**

src/Exceptions/HandlesErrors.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
use CloudCreativity\LaravelJsonApi\Utils\Helpers;
2424
use Illuminate\Http\Request;
2525
use Illuminate\Http\Response;
26-
use Neomerx\JsonApi\Contracts\Document\ErrorInterface;
26+
use Illuminate\Support\Collection;
27+
use Neomerx\JsonApi\Contracts\Schema\ErrorInterface;
2728
use Neomerx\JsonApi\Exceptions\JsonApiException;
2829
use Symfony\Component\HttpKernel\Exception\HttpException;
2930
use Throwable;
@@ -81,9 +82,9 @@ public function renderJsonApi($request, Throwable $e)
8182
*/
8283
protected function prepareJsonApiException(JsonApiException $ex)
8384
{
84-
$error = collect($ex->getErrors())->map(function (ErrorInterface $err) {
85-
return $err->getDetail() ?: $err->getTitle();
86-
})->filter()->first();
85+
$error = Collection::make($ex->getErrors())->map(
86+
fn(ErrorInterface $err) => $err->getDetail() ?: $err->getTitle()
87+
)->filter()->first();
8788

8889
return new HttpException($ex->getHttpCode(), $error, $ex);
8990
}

src/Schema/SchemaFields.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ public function __construct(iterable $paths = null, iterable $fieldSets = null)
102102

103103
if (null !== $fieldSets) {
104104
foreach ($fieldSets as $type => $fieldList) {
105-
foreach (\explode(static::FIELD_SEPARATOR, $fieldList) as $field) {
105+
$fieldList = \is_string($fieldList) ? \explode(static::FIELD_SEPARATOR, $fieldList) : $fieldList;
106+
foreach ($fieldList as $field) {
106107
$this->fastFields[$type][$field] = true;
107108
$this->fastFieldLists[$type][$field] = $field;
108109
}
@@ -147,4 +148,4 @@ public function getRequestedFields(string $type): ?array
147148
{
148149
return $this->fastFieldLists[$type] ?? null;
149150
}
150-
}
151+
}

stubs/abstract/schema.stub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace DummyNamespace;
44

5-
use Neomerx\JsonApi\Schema\SchemaProvider;
5+
use CloudCreativity\LaravelJsonApi\Schema\SchemaProvider;
66

77
class DummyClass extends SchemaProvider
88
{

0 commit comments

Comments
 (0)