Skip to content

Commit a1feda6

Browse files
committed
[Refactor] Initial work upgrading schemas
1 parent 9378a25 commit a1feda6

File tree

39 files changed

+971
-287
lines changed

39 files changed

+971
-287
lines changed

src/Codec/Decoding.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use CloudCreativity\LaravelJsonApi\Contracts\Decoder\DecoderInterface;
2121
use CloudCreativity\LaravelJsonApi\Decoder\JsonApiDecoder;
2222
use CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException;
23+
use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
2324
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
2425
use Neomerx\JsonApi\Http\Headers\MediaType;
2526

@@ -51,7 +52,7 @@ class Decoding
5152
public static function create($mediaType, $decoder): self
5253
{
5354
if (is_string($mediaType)) {
54-
$mediaType = MediaType::parse(0, $mediaType);
55+
$mediaType = MediaTypeParser::getInstance()->parse($mediaType);
5556
}
5657

5758
if (!$mediaType instanceof MediaTypeInterface) {

src/Codec/DecodingList.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
namespace CloudCreativity\LaravelJsonApi\Codec;
1919

20-
use Neomerx\JsonApi\Contracts\Http\Headers\HeaderInterface;
20+
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\HeaderInterface;
21+
use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
2122
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
22-
use Neomerx\JsonApi\Http\Headers\MediaType;
2323

2424
/**
2525
* Class DecodingList
@@ -142,7 +142,7 @@ public function unless(bool $test, $decodings): self
142142
*/
143143
public function find(string $mediaType): ?Decoding
144144
{
145-
return $this->equalsTo(MediaType::parse(0, $mediaType));
145+
return $this->equalsTo(MediaTypeParser::getInstance()->parse($mediaType));
146146
}
147147

148148
/**

src/Codec/Encoding.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
namespace CloudCreativity\LaravelJsonApi\Codec;
1919

2020
use CloudCreativity\LaravelJsonApi\Encoder\EncoderOptions;
21+
use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
2122
use Neomerx\JsonApi\Contracts\Http\Headers\AcceptMediaTypeInterface;
2223
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
23-
use Neomerx\JsonApi\Http\Headers\MediaType;
2424

2525
/**
2626
* Class Encoding
@@ -29,16 +29,15 @@
2929
*/
3030
class Encoding
3131
{
32-
3332
/**
3433
* @var MediaTypeInterface
3534
*/
36-
private $mediaType;
35+
private MediaTypeInterface $mediaType;
3736

3837
/**
3938
* @var EncoderOptions|null
4039
*/
41-
private $options;
40+
private ?EncoderOptions $options;
4241

4342
/**
4443
* Create an encoding that will encode JSON API content.
@@ -57,7 +56,7 @@ public static function create(
5756
): self
5857
{
5958
if (!$mediaType instanceof MediaTypeInterface) {
60-
$mediaType = MediaType::parse(0, $mediaType);
59+
$mediaType = MediaTypeParser::getInstance()->parse($mediaType);
6160
}
6261

6362
return new self($mediaType, new EncoderOptions($options, $urlPrefix, $depth));
@@ -90,7 +89,7 @@ public static function jsonApi(int $options = 0, string $urlPrefix = null, int $
9089
public static function custom($mediaType): self
9190
{
9291
if (!$mediaType instanceof MediaTypeInterface) {
93-
$mediaType = MediaType::parse(0, $mediaType);
92+
$mediaType = MediaTypeParser::getInstance()->parse($mediaType);
9493
}
9594

9695
return new self($mediaType, null);
@@ -161,8 +160,8 @@ public function hasOptions(): bool
161160
*/
162161
public function is(string ...$mediaTypes): bool
163162
{
164-
$mediaTypes = collect($mediaTypes)->map(function ($mediaType, $index) {
165-
return MediaType::parse($index, $mediaType);
163+
$mediaTypes = collect($mediaTypes)->map(function ($mediaType) {
164+
return MediaTypeParser::getInstance()->parse($mediaType);
166165
});
167166

168167
return $this->any(...$mediaTypes);

src/Codec/EncodingList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
namespace CloudCreativity\LaravelJsonApi\Codec;
1919

2020
use CloudCreativity\LaravelJsonApi\Contracts\Http\Headers\AcceptHeaderInterface;
21+
use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
2122
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
22-
use Neomerx\JsonApi\Http\Headers\MediaType;
2323

2424
/**
2525
* Class EncodingList
@@ -190,7 +190,7 @@ public function optional($encoding): self
190190
*/
191191
public function find(string $mediaType): ?Encoding
192192
{
193-
return $this->matchesTo(MediaType::parse(0, $mediaType));
193+
return $this->matchesTo(MediaTypeParser::getInstance()->parse($mediaType));
194194
}
195195

196196
/**

src/Container.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222
use CloudCreativity\LaravelJsonApi\Contracts\ContainerInterface;
2323
use CloudCreativity\LaravelJsonApi\Contracts\Http\ContentNegotiatorInterface;
2424
use CloudCreativity\LaravelJsonApi\Contracts\Resolver\ResolverInterface;
25+
use CloudCreativity\LaravelJsonApi\Contracts\Schema\SchemaProviderInterface;
2526
use CloudCreativity\LaravelJsonApi\Contracts\Validation\ValidatorFactoryInterface;
2627
use CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException;
28+
use CloudCreativity\LaravelJsonApi\Schema\Schema;
2729
use Illuminate\Contracts\Container\Container as IlluminateContainer;
30+
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
2831
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface;
2932

3033
/**
@@ -344,10 +347,17 @@ protected function setCreatedSchema($resourceType, SchemaInterface $schema): voi
344347
* @param string $className
345348
* @return SchemaInterface
346349
*/
347-
protected function createSchemaFromClassName($className): SchemaInterface
350+
protected function createSchemaFromClassName(string $className): SchemaInterface
348351
{
349352
$schema = $this->create($className);
350353

354+
if ($schema instanceof SchemaProviderInterface) {
355+
return new Schema(
356+
$this->container->make(FactoryInterface::class),
357+
$schema,
358+
);
359+
}
360+
351361
if (!$schema instanceof SchemaInterface) {
352362
throw new RuntimeException("Class [$className] is not a schema provider.");
353363
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/*
3+
* Copyright 2022 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 CloudCreativity\LaravelJsonApi\Contracts\Schema;
21+
22+
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
23+
24+
interface SchemaProviderInterface
25+
{
26+
public const SHOW_SELF = 'showSelf';
27+
public const SHOW_RELATED = 'related';
28+
public const SHOW_DATA = 'showData';
29+
public const DATA = 'data';
30+
public const META = 'meta';
31+
public const LINKS = 'links';
32+
33+
/**
34+
* Set the current context.
35+
*
36+
* @param ContextInterface|null $context
37+
* @return void
38+
*/
39+
public function setContext(?ContextInterface $context): void;
40+
41+
/**
42+
* Get the resource type.
43+
*
44+
* @return string
45+
*/
46+
public function getResourceType(): string;
47+
48+
/**
49+
* Get the resource id.
50+
*
51+
* @param object $resource
52+
* @return string
53+
*/
54+
public function getId(object $resource): string;
55+
56+
/**
57+
* Get the resource attributes.
58+
*
59+
* @param object $resource
60+
* @return array
61+
*/
62+
public function getAttributes(object $resource): array;
63+
64+
/**
65+
* Get the resource relationships.
66+
*
67+
* @param object $resource
68+
* @param bool $isPrimary
69+
* @param array $includedRelationships
70+
* @return array
71+
*/
72+
public function getRelationships(object $resource, bool $isPrimary, array $includedRelationships): array;
73+
74+
/**
75+
* Get the resource self url.
76+
*
77+
* @param object|null $resource
78+
* @return string
79+
*/
80+
public function getSelfSubUrl(object $resource = null): string;
81+
}

0 commit comments

Comments
 (0)