|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Copyright (c) 2020. |
|
4
|
|
|
* @author Paweł Antosiak <[email protected]> |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace Gorynych\Util; |
|
10
|
|
|
|
|
11
|
|
|
use Cake\Collection\Collection; |
|
12
|
|
|
use Cake\Collection\CollectionInterface; |
|
13
|
|
|
use OpenApi\Annotations\Items; |
|
14
|
|
|
use OpenApi\Annotations\Property; |
|
15
|
|
|
use OpenApi\Annotations\Schema; |
|
16
|
|
|
use const OpenApi\Annotations\UNDEFINED; |
|
17
|
|
|
|
|
18
|
|
|
final class SchemaFactory |
|
19
|
|
|
{ |
|
20
|
|
|
private SchemaStorage $schemaStorage; |
|
21
|
|
|
|
|
22
|
1 |
|
public function __construct(SchemaStorage $schemaStorage) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->schemaStorage = $schemaStorage; |
|
25
|
1 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Creates single JSON schema for provided class name |
|
29
|
|
|
*/ |
|
30
|
1 |
|
public function create(string $className): string |
|
31
|
|
|
{ |
|
32
|
1 |
|
$schemaName = (new \ReflectionClass($className))->getShortName(); |
|
33
|
|
|
|
|
34
|
1 |
|
$schema = $this->schemaStorage->getSchemas()->filter( |
|
35
|
1 |
|
static fn(Schema $schema): bool => $schema->schema === $schemaName |
|
36
|
1 |
|
)->first(); |
|
37
|
|
|
|
|
38
|
1 |
|
$relatedSchemas = $this->getRelatedSchemas($schema)->toArray(); |
|
|
|
|
|
|
39
|
1 |
|
$schema = json_decode(json_encode($schema), true); |
|
40
|
1 |
|
$schema['components']['schemas'] = $relatedSchemas; |
|
41
|
|
|
|
|
42
|
1 |
|
return json_encode($schema); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @return CollectionInterface<string, Schema> |
|
47
|
|
|
*/ |
|
48
|
1 |
|
private function getRelatedSchemas(Schema $schema): CollectionInterface |
|
49
|
|
|
{ |
|
50
|
1 |
|
$schemas = $this->schemaStorage->getSchemas(); |
|
51
|
1 |
|
$relatedSchemas = new Collection([]); |
|
52
|
|
|
|
|
53
|
1 |
|
(new Collection($schema->properties)) |
|
54
|
1 |
|
->reject( |
|
55
|
1 |
|
static function(Property $property): bool { |
|
56
|
|
|
/** @phpstan-ignore-next-line */ |
|
57
|
1 |
|
return UNDEFINED === $property->ref && UNDEFINED === $property->items; |
|
58
|
1 |
|
} |
|
59
|
|
|
) |
|
60
|
1 |
|
->each( |
|
61
|
1 |
|
static function(Property $property) use ($schemas, &$relatedSchemas): void { |
|
62
|
|
|
/** @phpstan-ignore-next-line */ |
|
63
|
1 |
|
$ref = $property->items instanceof Items ? $property->items->ref : $property->ref; |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
1 |
|
$relatedSchemas = $relatedSchemas->append( |
|
66
|
1 |
|
$schemas->filter( |
|
67
|
1 |
|
static function(Schema $schema) use ($ref): bool { |
|
68
|
1 |
|
$ref = explode('/', $ref); |
|
69
|
1 |
|
return $schema->schema === end($ref); |
|
70
|
1 |
|
} |
|
71
|
|
|
) |
|
72
|
|
|
); |
|
73
|
1 |
|
} |
|
74
|
|
|
); |
|
75
|
|
|
|
|
76
|
1 |
|
return $relatedSchemas->indexBy( |
|
77
|
1 |
|
static fn(Schema $schema): string => $schema->schema |
|
78
|
1 |
|
); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|