Skip to content

Commit a265f5e

Browse files
committed
[Refactor] More changes for the schema and schema provider
1 parent a1feda6 commit a265f5e

File tree

24 files changed

+647
-132
lines changed

24 files changed

+647
-132
lines changed

src/Api/Api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public function getJobs()
193193
public function getContainer()
194194
{
195195
if (!$this->container) {
196-
$this->container = $this->factory->createExtendedContainer($this->resolver);
196+
$this->container = $this->factory->createContainer($this->resolver);
197197
}
198198

199199
return $this->container;

src/Codec/Codec.php

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

2020
use CloudCreativity\LaravelJsonApi\Contracts\ContainerInterface;
21-
use Neomerx\JsonApi\Contracts\Encoder\EncoderInterface;
22-
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
21+
use CloudCreativity\LaravelJsonApi\Encoder\Encoder;
22+
use CloudCreativity\LaravelJsonApi\Factories\Factory;
2323
use Neomerx\JsonApi\Contracts\Http\Headers\MediaTypeInterface;
2424
use Neomerx\JsonApi\Http\Headers\MediaType;
2525

@@ -30,37 +30,36 @@
3030
*/
3131
class Codec
3232
{
33-
3433
/**
35-
* @var FactoryInterface
34+
* @var Factory
3635
*/
37-
private $factory;
36+
private Factory $factory;
3837

3938
/**
4039
* @var ContainerInterface
4140
*/
42-
private $container;
41+
private ContainerInterface $container;
4342

4443
/**
4544
* @var Encoding
4645
*/
47-
private $encoding;
46+
private Encoding $encoding;
4847

4948
/**
5049
* @var Decoding|null
5150
*/
52-
private $decoding;
51+
private ?Decoding $decoding;
5352

5453
/**
5554
* Codec constructor.
5655
*
57-
* @param FactoryInterface $factory
56+
* @param Factory $factory
5857
* @param ContainerInterface $container
5958
* @param Encoding $encoding
6059
* @param Decoding|null $decoding
6160
*/
6261
public function __construct(
63-
FactoryInterface $factory,
62+
Factory $factory,
6463
ContainerInterface $container,
6564
Encoding $encoding,
6665
?Decoding $decoding
@@ -92,15 +91,18 @@ public function willNotEncode(): bool
9291
}
9392

9493
/**
95-
* @return EncoderInterface
94+
* @return Encoder
9695
*/
97-
public function getEncoder(): EncoderInterface
96+
public function getEncoder(): Encoder
9897
{
9998
if ($this->willNotEncode()) {
10099
throw new \RuntimeException('Codec does not support encoding JSON API content.');
101100
}
102101

103-
$encoder = $this->factory->createEncoder($this->container);
102+
$encoder = $this->factory->createExtendedEncoder(
103+
$this->factory->createLaravelSchemaContainer($this->container)
104+
);
105+
104106
$options = $this->encoding->getOptions();
105107

106108
if ($options) {

src/Container.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
use CloudCreativity\LaravelJsonApi\Contracts\Schema\SchemaProviderInterface;
2626
use CloudCreativity\LaravelJsonApi\Contracts\Validation\ValidatorFactoryInterface;
2727
use CloudCreativity\LaravelJsonApi\Exceptions\RuntimeException;
28-
use CloudCreativity\LaravelJsonApi\Schema\Schema;
2928
use Illuminate\Contracts\Container\Container as IlluminateContainer;
30-
use Neomerx\JsonApi\Contracts\Factories\FactoryInterface;
31-
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface;
3229

3330
/**
3431
* Class Container
@@ -37,36 +34,35 @@
3734
*/
3835
class Container implements ContainerInterface
3936
{
40-
4137
/**
4238
* @var IlluminateContainer
4339
*/
44-
private $container;
40+
private IlluminateContainer $container;
4541

4642
/**
4743
* @var ResolverInterface
4844
*/
49-
private $resolver;
45+
private ResolverInterface $resolver;
5046

5147
/**
5248
* @var array
5349
*/
54-
private $createdSchemas = [];
50+
private array $createdSchemas = [];
5551

5652
/**
5753
* @var array
5854
*/
59-
private $createdAdapters = [];
55+
private array $createdAdapters = [];
6056

6157
/**
6258
* @var array
6359
*/
64-
private $createdValidators = [];
60+
private array $createdValidators = [];
6561

6662
/**
6763
* @var array
6864
*/
69-
private $createdAuthorizers = [];
65+
private array $createdAuthorizers = [];
7066

7167
/**
7268
* Container constructor.
@@ -83,15 +79,15 @@ public function __construct(IlluminateContainer $container, ResolverInterface $r
8379
/**
8480
* @inheritDoc
8581
*/
86-
public function getSchema($resourceObject): SchemaInterface
82+
public function getSchema($resourceObject): SchemaProviderInterface
8783
{
8884
return $this->getSchemaByType(get_class($resourceObject));
8985
}
9086

9187
/**
9288
* @inheritDoc
9389
*/
94-
public function hasSchema($resourceObject): bool
90+
public function hasSchema(object $resourceObject): bool
9591
{
9692
$type = get_class($resourceObject);
9793

@@ -104,9 +100,9 @@ public function hasSchema($resourceObject): bool
104100
* Get resource by object type.
105101
*
106102
* @param string $type
107-
* @return SchemaInterface
103+
* @return SchemaProviderInterface
108104
*/
109-
public function getSchemaByType(string $type): SchemaInterface
105+
public function getSchemaByType(string $type): SchemaProviderInterface
110106
{
111107
$resourceType = $this->getResourceType($type);
112108

@@ -117,9 +113,9 @@ public function getSchemaByType(string $type): SchemaInterface
117113
* Get resource by JSON:API type.
118114
*
119115
* @param string $resourceType
120-
* @return SchemaInterface
116+
* @return SchemaProviderInterface
121117
*/
122-
public function getSchemaByResourceType(string $resourceType): SchemaInterface
118+
public function getSchemaByResourceType(string $resourceType): SchemaProviderInterface
123119
{
124120
if ($this->hasCreatedSchema($resourceType)) {
125121
return $this->getCreatedSchema($resourceType);
@@ -326,39 +322,32 @@ protected function hasCreatedSchema($resourceType)
326322

327323
/**
328324
* @param string $resourceType
329-
* @return SchemaInterface
325+
* @return SchemaProviderInterface
330326
*/
331-
protected function getCreatedSchema($resourceType): SchemaInterface
327+
protected function getCreatedSchema($resourceType): SchemaProviderInterface
332328
{
333329
return $this->createdSchemas[$resourceType];
334330
}
335331

336332
/**
337333
* @param string $resourceType
338-
* @param SchemaInterface $schema
334+
* @param SchemaProviderInterface $schema
339335
* @return void
340336
*/
341-
protected function setCreatedSchema($resourceType, SchemaInterface $schema): void
337+
protected function setCreatedSchema($resourceType, SchemaProviderInterface $schema): void
342338
{
343339
$this->createdSchemas[$resourceType] = $schema;
344340
}
345341

346342
/**
347343
* @param string $className
348-
* @return SchemaInterface
344+
* @return SchemaProviderInterface
349345
*/
350-
protected function createSchemaFromClassName(string $className): SchemaInterface
346+
protected function createSchemaFromClassName(string $className): SchemaProviderInterface
351347
{
352348
$schema = $this->create($className);
353349

354-
if ($schema instanceof SchemaProviderInterface) {
355-
return new Schema(
356-
$this->container->make(FactoryInterface::class),
357-
$schema,
358-
);
359-
}
360-
361-
if (!$schema instanceof SchemaInterface) {
350+
if (!$schema instanceof SchemaProviderInterface) {
362351
throw new RuntimeException("Class [$className] is not a schema provider.");
363352
}
364353

src/Contracts/ContainerInterface.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,47 @@
2020
use CloudCreativity\LaravelJsonApi\Contracts\Adapter\ResourceAdapterInterface;
2121
use CloudCreativity\LaravelJsonApi\Contracts\Auth\AuthorizerInterface;
2222
use CloudCreativity\LaravelJsonApi\Contracts\Http\ContentNegotiatorInterface;
23+
use CloudCreativity\LaravelJsonApi\Contracts\Schema\SchemaProviderInterface;
2324
use CloudCreativity\LaravelJsonApi\Contracts\Validation\ValidatorFactoryInterface;
24-
use Neomerx\JsonApi\Contracts\Schema\SchemaContainerInterface;
25-
use Neomerx\JsonApi\Contracts\Schema\SchemaInterface;
2625

2726
/**
2827
* Interface ContainerInterface
2928
*
3029
* @package CloudCreativity\LaravelJsonApi
3130
*/
32-
interface ContainerInterface extends SchemaContainerInterface
31+
interface ContainerInterface
3332
{
33+
/**
34+
* Get schema provider for resource object.
35+
*
36+
* @param object $resourceObject
37+
* @return SchemaProviderInterface
38+
*/
39+
public function getSchema(object $resourceObject): SchemaProviderInterface;
40+
41+
/**
42+
* If container has a Schema for a given input.
43+
*
44+
* @param object $resourceObject
45+
* @return bool
46+
*/
47+
public function hasSchema(object $resourceObject): bool;
48+
3449
/**
3550
* Get schema provider by resource type.
3651
*
3752
* @param string $type
38-
* @return SchemaInterface
53+
* @return SchemaProviderInterface
3954
*/
40-
public function getSchemaByType(string $type): SchemaInterface;
55+
public function getSchemaByType(string $type): SchemaProviderInterface;
4156

4257
/**
4358
* Get schema provider by JSON:API type.
4459
*
4560
* @param string $resourceType
46-
* @return SchemaInterface
61+
* @return SchemaProviderInterface
4762
*/
48-
public function getSchemaByResourceType(string $resourceType): SchemaInterface;
63+
public function getSchemaByResourceType(string $resourceType): SchemaProviderInterface;
4964

5065
/**
5166
* Get a resource adapter for a domain record.

src/Contracts/Schema/SchemaProviderInterface.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace CloudCreativity\LaravelJsonApi\Contracts\Schema;
2121

2222
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
23+
use Neomerx\JsonApi\Contracts\Schema\LinkInterface;
2324

2425
interface SchemaProviderInterface
2526
{
@@ -78,4 +79,30 @@ public function getRelationships(object $resource, bool $isPrimary, array $inclu
7879
* @return string
7980
*/
8081
public function getSelfSubUrl(object $resource = null): string;
82+
83+
/**
84+
* Get the resource self sub link.
85+
*
86+
* @param object $resource
87+
* @return LinkInterface
88+
*/
89+
public function getSelfSubLink(object $resource): LinkInterface;
90+
91+
/**
92+
* Get the relationship self link.
93+
*
94+
* @param object $resource
95+
* @param string $field
96+
* @return LinkInterface
97+
*/
98+
public function getRelationshipSelfLink(object $resource, string $field): LinkInterface;
99+
100+
/**
101+
* Get the relationship related link.
102+
*
103+
* @param object $resource
104+
* @param string $field
105+
* @return LinkInterface
106+
*/
107+
public function getRelationshipRelatedLink(object $resource, string $field): LinkInterface;
81108
}

0 commit comments

Comments
 (0)