|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-router/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-router |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace Sunrise\Http\Router\OpenApi\PhpTypeSchemaResolver; |
|
15
|
|
|
|
|
16
|
|
|
use DateTimeImmutable; |
|
17
|
|
|
use ReflectionAttribute; |
|
18
|
|
|
use ReflectionParameter; |
|
19
|
|
|
use ReflectionProperty; |
|
20
|
|
|
use Reflector; |
|
21
|
|
|
use Sunrise\Http\Router\OpenApi\Exception\UnsupportedPhpTypeException; |
|
22
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiConfiguration; |
|
23
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiConfigurationAwareInterface; |
|
24
|
|
|
use Sunrise\Http\Router\OpenApi\OpenApiPhpTypeSchemaResolverInterface; |
|
25
|
|
|
use Sunrise\Http\Router\OpenApi\Type; |
|
26
|
|
|
use Sunrise\Hydrator\Annotation\Format; |
|
27
|
|
|
|
|
28
|
|
|
use function date; |
|
29
|
|
|
use function is_a; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @since 3.0.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
final class TimestampPhpTypeSchemaResolver implements |
|
35
|
|
|
OpenApiPhpTypeSchemaResolverInterface, |
|
36
|
|
|
OpenApiConfigurationAwareInterface |
|
37
|
|
|
{ |
|
38
|
|
|
private readonly OpenApiConfiguration $openApiConfiguration; |
|
39
|
|
|
|
|
40
|
5 |
|
public function setOpenApiConfiguration(OpenApiConfiguration $openApiConfiguration): void |
|
41
|
|
|
{ |
|
42
|
5 |
|
$this->openApiConfiguration = $openApiConfiguration; |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
3 |
|
public function supportsPhpType(Type $phpType, Reflector $phpTypeHolder): bool |
|
46
|
|
|
{ |
|
47
|
3 |
|
return is_a($phpType->name, DateTimeImmutable::class, true); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @inheritDoc |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function resolvePhpTypeSchema(Type $phpType, Reflector $phpTypeHolder): array |
|
54
|
|
|
{ |
|
55
|
3 |
|
$this->supportsPhpType($phpType, $phpTypeHolder) or throw new UnsupportedPhpTypeException(); |
|
56
|
|
|
|
|
57
|
3 |
|
$timestampFormat = $this->openApiConfiguration->defaultTimestampFormat; |
|
58
|
|
|
|
|
59
|
|
|
if ( |
|
60
|
3 |
|
$phpTypeHolder instanceof ReflectionParameter || |
|
61
|
3 |
|
$phpTypeHolder instanceof ReflectionProperty |
|
62
|
|
|
) { |
|
63
|
|
|
/** @var list<ReflectionAttribute<Format>> $annotations */ |
|
64
|
3 |
|
$annotations = $phpTypeHolder->getAttributes(Format::class); |
|
65
|
3 |
|
if (isset($annotations[0])) { |
|
66
|
3 |
|
$annotation = $annotations[0]->newInstance(); |
|
67
|
3 |
|
$timestampFormat = $annotation->value; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
return [ |
|
72
|
3 |
|
'type' => Type::OAS_TYPE_NAME_STRING, |
|
73
|
3 |
|
'format' => 'date-time', |
|
74
|
3 |
|
'example' => date($timestampFormat, 0), |
|
75
|
3 |
|
]; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
3 |
|
public function getWeight(): int |
|
79
|
|
|
{ |
|
80
|
3 |
|
return 0; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|