-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSchemaRegistry.php
More file actions
56 lines (46 loc) · 1.52 KB
/
Copy pathSchemaRegistry.php
File metadata and controls
56 lines (46 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace ApiClients\Tools\OpenApiClientGenerator;
use \cebe\openapi\spec\Schema;
final class SchemaRegistry
{
/**
* @var array<string, class-string>
*/
private array $splHash = [];
/**
* @var array<string, class-string>
*/
private array $json = [];
/**
* @var array<string,array<{name: string, className: string, schema: Schema}>>
*/
private array $unknownSchemas = [];
public function addClassName(string $className, Schema $schema): void
{
$this->splHash[spl_object_hash($schema)] = $className;
$this->json[json_encode($schema->getSerializableData())] = $className;
}
public function get(\cebe\openapi\spec\Schema $schema, string $fallbackName = ''): string
{
$hash = spl_object_hash($schema);
if (array_key_exists($hash, $this->splHash)) {
return $this->splHash[$hash];
}
$json = json_encode($schema->getSerializableData());
if (array_key_exists($json, $this->json)) {
return $this->json[$json];
}
$name = $fallbackName === '' ? 'c_' . md5($json) : $fallbackName;
$className = $fallbackName === '' ? Generator::className('Unknown\C_' . md5($json)) : $fallbackName;
$this->unknownSchemas[$hash] = [
'name' => $name,
'className' => $className,
'schema' => $schema,
];
return $className;
}
public function unknownSchemas(): iterable
{
yield from $this->unknownSchemas;
}
}