Skip to content

Commit e89bfee

Browse files
committed
feat: schema return skeleton insteadof object (stdClass)
1 parent 8de2598 commit e89bfee

File tree

6 files changed

+45
-17
lines changed

6 files changed

+45
-17
lines changed

CHANGELOG-1.1.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ Release note
22
============
33

44
# v1.1.8
5+
### Add
6+
- `Skeleton::class` present skeleton of a `JsonApiResource::class`
7+
58
### Changes
69
- **@deprecated** `Support\With` is replaced by `Support\Arr`
10+
- `Resources\Schema::schema` return `Skeleton`
11+
- `Resources\SchemaCollection::schema` return `Skeleton`
712

813
### Fixes
914
- `JsonApiResource::toArray` return true deep array

src/Requests/Rules/Fields.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ark4ne\JsonApi\Requests\Rules;
44

55
use Ark4ne\JsonApi\Requests\Rules\Traits\UseTrans;
6+
use Ark4ne\JsonApi\Resources\Skeleton;
67
use Ark4ne\JsonApi\Support\Fields as SupportFields;
78
use Illuminate\Contracts\Validation\Rule;
89

@@ -66,12 +67,12 @@ public function message(): array
6667
}
6768

6869
/**
69-
* @param object $schema
70+
* @param Skeleton $schema
7071
* @param array<string, string[]> $desired
7172
*
7273
* @return bool
7374
*/
74-
private function assert(object $schema, array $desired): bool
75+
private function assert(Skeleton $schema, array $desired): bool
7576
{
7677
$resources = $this->extractSchemaFields($schema);
7778

@@ -92,12 +93,12 @@ private function assert(object $schema, array $desired): bool
9293
}
9394

9495
/**
95-
* @param object $schema
96+
* @param Skeleton $schema
9697
* @param array<string, string[]> $resources
9798
*
9899
* @return array<string, string[]>
99100
*/
100-
private function extractSchemaFields(object $schema, array $resources = []): array
101+
private function extractSchemaFields(Skeleton $schema, array $resources = []): array
101102
{
102103
if (isset($resources[$schema->type])) {
103104
return $resources;

src/Requests/Rules/Includes.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Ark4ne\JsonApi\Requests\Rules;
44

55
use Ark4ne\JsonApi\Requests\Rules\Traits\UseTrans;
6+
use Ark4ne\JsonApi\Resources\Skeleton;
67
use Ark4ne\JsonApi\Support\Includes as SupportIncludes;
78
use Illuminate\Contracts\Validation\Rule;
89

@@ -60,13 +61,13 @@ public function message(): array
6061
}
6162

6263
/**
63-
* @param object $schema
64+
* @param Skeleton $schema
6465
* @param array<string, mixed> $desired
6566
* @param string $pretend
6667
*
6768
* @return bool
6869
*/
69-
private function assert(object $schema, array $desired, string $pretend = ''): bool
70+
private function assert(Skeleton $schema, array $desired, string $pretend = ''): bool
7071
{
7172
foreach ($desired as $relation => $sub) {
7273
if (!isset($schema->relationships[$relation])) {

src/Resources/Concerns/Schema.php

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

33
namespace Ark4ne\JsonApi\Resources\Concerns;
44

5+
use Ark4ne\JsonApi\Resources\Skeleton;
56
use Ark4ne\JsonApi\Support\FakeModel;
67
use Illuminate\Http\Request;
78
use Illuminate\Support\Collection;
@@ -10,11 +11,11 @@
1011
trait Schema
1112
{
1213
/**
13-
* @var array<class-string, object>
14+
* @var array<class-string, Skeleton>
1415
*/
1516
private static array $schemas = [];
1617

17-
public static function schema(Request $request = null): object
18+
public static function schema(Request $request = null): Skeleton
1819
{
1920
if (isset(self::$schemas[static::class])) {
2021
return self::$schemas[static::class];
@@ -24,15 +25,11 @@ public static function schema(Request $request = null): object
2425

2526
$request ??= new Request;
2627

27-
$schema = (object)[
28-
'type' => null,
29-
'fields' => [],
30-
'relationships' => [],
31-
];
28+
self::$schemas[static::class] = $schema = new Skeleton(
29+
static::class,
30+
$resource->toType($request)
31+
);
3232

33-
self::$schemas[static::class] = $schema;
34-
35-
$schema->type = $resource->toType($request);
3633
$schema->fields = (new Collection($resource->toAttributes($request)))->keys()->all();
3734

3835
foreach ($resource->toRelationships($request) as $name => $relation) {

src/Resources/Concerns/SchemaCollection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace Ark4ne\JsonApi\Resources\Concerns;
44

5+
use Ark4ne\JsonApi\Resources\Skeleton;
56
use Illuminate\Http\Request;
67
use ReflectionClass;
78

89
trait SchemaCollection
910
{
10-
public static function schema(Request $request = null): object
11+
public static function schema(Request $request = null): Skeleton
1112
{
1213
return self::new()->collects::schema($request);
1314
}

src/Resources/Skeleton.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Ark4ne\JsonApi\Resources;
4+
5+
/**
6+
* @template T
7+
*/
8+
class Skeleton
9+
{
10+
/**
11+
* @param class-string<T> $for
12+
* @param string $type
13+
* @param string[] $fields
14+
* @param array<string, self> $relationships
15+
*/
16+
public function __construct(
17+
public string $for,
18+
public string $type,
19+
public array $fields = [],
20+
public array $relationships = [],
21+
) {
22+
}
23+
}

0 commit comments

Comments
 (0)