Skip to content

Commit a590ff7

Browse files
authored
Featrue/normalizers (#8)
* add Normalizer * add Normalizer * add Normalizer * add Normalizer * fixed docs * fixed docs * fixed toArray Bug * added JsonSerializable * add Normalizer fixed toArray * add cast tests
1 parent 2e35b2e commit a590ff7

26 files changed

+277
-156
lines changed

src/Annotations/Output/OutputDateFormat.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313
use DateTimeZone;
1414
use Exception;
1515

16-
/**
17-
* toArray 输出值为 固定日期格式 默认 YYYY-MM-DD HH:ii:ss的日期格式
18-
*/
1916
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_CLASS)]
2017
class OutputDateFormat implements OutValueCastInterface
2118
{

src/Casts/Normalizer/ArrayNormalizerCast.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ class ArrayNormalizerCast implements NormalizerCastInterface
88
{
99
public function match(mixed $values): bool
1010
{
11-
return is_object($values) && method_exists($values, 'toArray');
11+
return is_object($values) && method_exists($values, 'toArray');
1212
}
1313

1414
public function resolve(mixed $values): mixed
1515
{
16-
if($this->match($values)){
16+
if ($this->match($values)) {
1717
return $values->toArray();
1818
}
1919

2020
return $values;
2121
}
22-
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Astral\Serialize\Casts\Normalizer;
4+
5+
use Astral\Serialize\Contracts\Normalizer\NormalizerCastInterface;
6+
use DateTimeInterface;
7+
use Throwable;
8+
9+
class DateTimeNormalizerCast implements NormalizerCastInterface
10+
{
11+
public function match(mixed $values): bool
12+
{
13+
return is_object($values) && is_subclass_of($values, DateTimeInterface::class);
14+
}
15+
16+
public function resolve(mixed $values): mixed
17+
{
18+
if ($this->match($values)) {
19+
try {
20+
return $values->format('Y-m-d H:i:s');
21+
} catch (Throwable $e) {
22+
}
23+
}
24+
25+
return $values;
26+
}
27+
}

src/Casts/Normalizer/JsonNormalizerCast.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public function match(mixed $values): bool
1212
return is_string($values);
1313
}
1414

15-
public function resolve(mixed $values): array
15+
public function resolve(mixed $values): mixed
1616
{
17-
if($this->match($values)){
17+
if ($this->match($values)) {
1818
try {
1919
$decoded = json_decode($values, true, 512, JSON_THROW_ON_ERROR);
2020
return is_array($decoded) ? $decoded : $values;
@@ -25,4 +25,4 @@ public function resolve(mixed $values): array
2525

2626
return $values;
2727
}
28-
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Astral\Serialize\Casts\Normalizer;
4+
5+
use Astral\Serialize\Contracts\Normalizer\NormalizerCastInterface;
6+
7+
class ObjectNormalizerCast implements NormalizerCastInterface
8+
{
9+
public function match(mixed $values): bool
10+
{
11+
return is_object($values);
12+
}
13+
14+
public function resolve(mixed $values): mixed
15+
{
16+
if ($this->match($values)) {
17+
return (array)$values;
18+
}
19+
20+
return $values;
21+
}
22+
}

src/Contracts/Normalizer/NormalizerCastInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ interface NormalizerCastInterface
77
public function match(mixed $values): bool;
88

99
public function resolve(mixed $values): mixed;
10-
}
10+
}

src/Enums/ConfigCastEnum.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Astral\Serialize\Enums;
4+
5+
use Astral\Serialize\Contracts\Attribute\DataCollectionCastInterface;
6+
use Astral\Serialize\Contracts\Attribute\InputValueCastInterface;
7+
use Astral\Serialize\Contracts\Attribute\OutValueCastInterface;
8+
use Astral\Serialize\Contracts\Normalizer\NormalizerCastInterface;
9+
10+
enum ConfigCastEnum: string
11+
{
12+
case PROPERTY = 'attributePropertyResolver';
13+
case OUTPUT_VALUE = 'outputValueCasts';
14+
case INPUT_VALUE = 'inputValueCasts';
15+
case INPUT_NORMALIZER = 'inputNormalizerCasts';
16+
case OUT_NORMALIZER = 'outNormalizerCasts';
17+
18+
public function getCastInterface(): string
19+
{
20+
return match ($this) {
21+
self::PROPERTY => DataCollectionCastInterface::class,
22+
self::OUTPUT_VALUE => OutValueCastInterface::class,
23+
self::INPUT_VALUE => InputValueCastInterface::class,
24+
self::INPUT_NORMALIZER, self::OUT_NORMALIZER => NormalizerCastInterface::class,
25+
};
26+
}
27+
28+
public static function getValues(): array
29+
{
30+
return array_column(self::cases(), 'value');
31+
}
32+
}

src/OpenApi/Collections/OpenApiCollection.php

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Astral\Serialize\OpenApi\Storage\OpenAPI\SchemaStorage;
2020
use Astral\Serialize\Resolvers\GroupResolver;
2121
use Astral\Serialize\Serialize;
22-
use Astral\Serialize\SerializeContainer;
2322
use Astral\Serialize\Support\Factories\ContextFactory;
2423
use Psr\SimpleCache\InvalidArgumentException;
2524
use ReflectionMethod;
@@ -32,8 +31,8 @@ public function __construct(
3231
public string $methodName,
3332
public reflectionMethod $reflectionMethod,
3433
public Tag $tag,
35-
public Summary $summary,
36-
public Route $route,
34+
public Summary|null $summary,
35+
public Route|null $route,
3736
public Headers|null $headers,
3837
public array $attributes,
3938
public RequestBody|null $requestBody,
@@ -58,7 +57,7 @@ public function build(): Method
5857
$requestBody = $this->buildRequestBody(
5958
className:$this->getRequestBodyClass(),
6059
contentType:$this->requestBody->contentType ?? ContentTypeEnum::JSON,
61-
groups: $this->requestBody->groups ?? []
60+
groups: $this->requestBody->groups ?? []
6261
);
6362

6463
$response = $this->buildResponse(
@@ -71,10 +70,9 @@ className:$this->getResponseClass(),
7170
return $openAPIMethod;
7271
}
7372

74-
7573
public function getRequestBodyClass(): string
7674
{
77-
if($this->requestBody?->className){
75+
if ($this->requestBody?->className) {
7876
return $this->requestBody->className;
7977
}
8078

@@ -88,12 +86,11 @@ public function getRequestBodyClass(): string
8886
return '';
8987
}
9088

91-
92-
public function buildRequestBody(string $className,ContentTypeEnum $contentType,array $groups = []): RequestBodyStorage
89+
public function buildRequestBody(string $className, ContentTypeEnum $contentType, array $groups = []): RequestBodyStorage
9390
{
9491
$openAPIRequestBody = new RequestBodyStorage($contentType);
9592
if (is_subclass_of($className, Serialize::class)) {
96-
$schemaStorage = (new SchemaStorage())->build($this->buildRequestParameterCollections($className,$groups));
93+
$schemaStorage = (new SchemaStorage())->build($this->buildRequestParameterCollections($className, $groups));
9794
$openAPIRequestBody->withParameter($schemaStorage);
9895
}
9996

@@ -102,7 +99,7 @@ public function buildRequestBody(string $className,ContentTypeEnum $contentType,
10299

103100
public function getResponseClass(): string
104101
{
105-
if($this->response?->className){
102+
if ($this->response?->className) {
106103
return $this->response->className;
107104
}
108105

@@ -118,18 +115,18 @@ public function getResponseClass(): string
118115
/**
119116
* @throws InvalidArgumentException
120117
*/
121-
public function buildResponse(string $className,array $groups = []): ResponseStorage
118+
public function buildResponse(string $className, array $groups = []): ResponseStorage
122119
{
123120
$responseStorage = new ResponseStorage();
124121

125-
$baseResponse = Config::get('response',[]);
122+
$baseResponse = Config::get('response', []);
126123

127124
if ($className) {
128-
$schemaStorage = (new SchemaStorage())->build($this->buildResponseParameterCollections($className,$groups));
125+
$schemaStorage = (new SchemaStorage())->build($this->buildResponseParameterCollections($className, $groups));
129126
$responseStorage->withParameter($schemaStorage);
130127
}
131128

132-
if($baseResponse){
129+
if ($baseResponse) {
133130
$responseStorage->addGlobParameters($baseResponse);
134131
}
135132

@@ -148,13 +145,13 @@ public function buildRequestParameterCollections(string $className, array $group
148145
$serializeContext = ContextFactory::build($className);
149146
$serializeContext->setGroups($groups)->from();
150147
$properties = $serializeContext->getGroupCollection()->getProperties();
151-
$groups = $groups ?: [$className];
148+
$groups = $groups ?: [$className];
152149

153150
$vols = [];
154151
foreach ($properties as $property) {
155152

156153

157-
if($property->isInputIgnoreByGroups($groups) || !$this->groupResolver->resolveExistsGroupsByDataCollection($property, $groups, $className)){
154+
if ($property->isInputIgnoreByGroups($groups) || !$this->groupResolver->resolveExistsGroupsByDataCollection($property, $groups, $className)) {
158155
continue;
159156
}
160157

@@ -192,12 +189,12 @@ public function buildResponseParameterCollections(string $className, array $grou
192189
$serializeContext = ContextFactory::build($className);
193190
$serializeContext->from();
194191
$properties = $serializeContext->getGroupCollection()->getProperties();
195-
$groups = $groups ?: [$className];
192+
$groups = $groups ?: [$className];
196193

197194
$vols = [];
198195
foreach ($properties as $property) {
199196

200-
if($property->isOutIgnoreByGroups($groups) || !$this->groupResolver->resolveExistsGroupsByDataCollection($property, $groups, $className)){
197+
if ($property->isOutIgnoreByGroups($groups) || !$this->groupResolver->resolveExistsGroupsByDataCollection($property, $groups, $className)) {
201198
continue;
202199
}
203200

src/OpenApi/Frankenphp/docs/index.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<body>
1616

1717
<elements-api
18-
apiDescriptionUrl="<?php echo \Astral\Serialize\OpenApi\Handler\Config::get('doc_url','http://127.0.0.1:8089'); ?>"
18+
apiDescriptionUrl="<?php echo \Astral\Serialize\OpenApi\Handler\Config::get('doc_url', 'http://127.0.0.1:8089'); ?>"
1919
router="hash"
2020
layout="sidebar"
2121
/>

src/OpenApi/Frankenphp/index.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
use Astral\Serialize\OpenApi;
34

45
ignore_user_abort(true);

0 commit comments

Comments
 (0)