From f4efcf30d5595f0175bb3a1aba130502ab15a3ff Mon Sep 17 00:00:00 2001 From: "350375092@qq.com" <350375092@qq.com> Date: Fri, 8 Aug 2025 17:35:35 +0800 Subject: [PATCH 1/8] add Normalizer --- src/Casts/Normalizer/ArrayNormalizerCast.php | 22 ++++++++ src/Casts/Normalizer/JsonNormalizerCast.php | 28 ++++++++++ .../Normalizer/NormalizerCastInterface.php | 10 ++++ .../Casts/NormalizerCastResolver.php | 25 +++++++++ src/SerializeContainer.php | 7 +++ src/Support/Config/ConfigManager.php | 31 +++++++++++ src/Support/Context/SerializeContext.php | 4 ++ src/Support/Factories/ContextFactory.php | 1 + .../From/NormalizerFromSerializeTest.php | 55 +++++++++++++++++++ 9 files changed, 183 insertions(+) create mode 100644 src/Casts/Normalizer/ArrayNormalizerCast.php create mode 100644 src/Casts/Normalizer/JsonNormalizerCast.php create mode 100644 src/Contracts/Normalizer/NormalizerCastInterface.php create mode 100644 src/Resolvers/Casts/NormalizerCastResolver.php create mode 100644 tests/Serialize/From/NormalizerFromSerializeTest.php diff --git a/src/Casts/Normalizer/ArrayNormalizerCast.php b/src/Casts/Normalizer/ArrayNormalizerCast.php new file mode 100644 index 0000000..c6527a5 --- /dev/null +++ b/src/Casts/Normalizer/ArrayNormalizerCast.php @@ -0,0 +1,22 @@ +match($values)){ + return $values->toArray(); + } + + return $values; + } +} \ No newline at end of file diff --git a/src/Casts/Normalizer/JsonNormalizerCast.php b/src/Casts/Normalizer/JsonNormalizerCast.php new file mode 100644 index 0000000..1bf6fe0 --- /dev/null +++ b/src/Casts/Normalizer/JsonNormalizerCast.php @@ -0,0 +1,28 @@ +match($values)){ + try { + $decoded = json_decode($values, true, 512, JSON_THROW_ON_ERROR); + return is_array($decoded) ? $decoded : $values; + } catch (JsonException $e) { + + } + } + + return $values; + } +} \ No newline at end of file diff --git a/src/Contracts/Normalizer/NormalizerCastInterface.php b/src/Contracts/Normalizer/NormalizerCastInterface.php new file mode 100644 index 0000000..09d49fa --- /dev/null +++ b/src/Contracts/Normalizer/NormalizerCastInterface.php @@ -0,0 +1,10 @@ +configManager->getNormalizerCasts() as $cast) { + $values = $cast->resolve($values); + } + + return $values; + } +} \ No newline at end of file diff --git a/src/SerializeContainer.php b/src/SerializeContainer.php index a17ddc7..8715a71 100644 --- a/src/SerializeContainer.php +++ b/src/SerializeContainer.php @@ -8,6 +8,7 @@ use Astral\Serialize\Faker\Rule\FakerDefaultRules; use Astral\Serialize\Resolvers\Casts\DataCollectionCastResolver; use Astral\Serialize\Resolvers\Casts\InputValueCastResolver; +use Astral\Serialize\Resolvers\Casts\NormalizerCastResolver; use Astral\Serialize\Resolvers\Casts\OutputCastResolver; use Astral\Serialize\Resolvers\GroupResolver; use Astral\Serialize\Resolvers\InputResolver; @@ -41,6 +42,7 @@ class SerializeContainer protected ?GroupResolver $groupResolver = null; protected ?ReflectionClassInstanceManager $reflectionClassInstanceManager = null; protected ?SerializeInstanceManager $serializeInstanceManager = null; + protected ?NormalizerCastResolver $normalizerCastResolver = null; protected ?DataCollectionCastResolver $attributePropertyResolver = null; protected ?InputResolver $propertyInputValueResolver = null; protected ?OutputResolver $propertyToArrayResolver = null; @@ -119,6 +121,11 @@ public function propertyInputValueResolver(): InputResolver ); } + public function normalizerCastResolver(): NormalizerCastResolver + { + return $this->normalizerCastResolver ??= new NormalizerCastResolver(ConfigManager::getInstance()); + } + public function inputValueCastResolver(): InputValueCastResolver { return $this->inputValueCastResolver ??= new InputValueCastResolver(ConfigManager::getInstance()); diff --git a/src/Support/Config/ConfigManager.php b/src/Support/Config/ConfigManager.php index 90b72b7..4e8f8ca 100644 --- a/src/Support/Config/ConfigManager.php +++ b/src/Support/Config/ConfigManager.php @@ -7,12 +7,15 @@ use Astral\Serialize\Casts\InputValue\InputObjectBestMatchChildCast; use Astral\Serialize\Casts\InputValue\InputValueEnumCast; use Astral\Serialize\Casts\InputValue\InputValueNullCast; +use Astral\Serialize\Casts\Normalizer\ArrayNormalizerCast; +use Astral\Serialize\Casts\Normalizer\JsonNormalizerCast; use Astral\Serialize\Casts\OutValue\OutArrayChildCast; use Astral\Serialize\Casts\OutValue\OutValueEnumCast; use Astral\Serialize\Casts\OutValue\OutValueGetterCast; use Astral\Serialize\Contracts\Attribute\DataCollectionCastInterface; use Astral\Serialize\Contracts\Attribute\InputValueCastInterface; use Astral\Serialize\Contracts\Attribute\OutValueCastInterface; +use Astral\Serialize\Contracts\Normalizer\NormalizerCastInterface; use Astral\Serialize\Enums\CacheDriverEnum; use Astral\Serialize\Exceptions\NotFoundAttributePropertyResolver; use Astral\Serialize\Support\Caching\MemoryCache; @@ -40,6 +43,12 @@ class ConfigManager OutValueGetterCast::class, ]; + /** @var (NormalizerCastInterface|string)[] $normalizerCasts */ + private array $normalizerCasts = [ +// JsonNormalizerCast::class, + ArrayNormalizerCast::class, + ]; + /** @var CacheDriverEnum|class-string $cacheDriver */ private string|CacheDriverEnum $cacheDriver = MemoryCache::class; @@ -52,6 +61,10 @@ public function __construct() foreach ($this->outputValueCasts as $key => $cast) { $this->outputValueCasts[$key] = new $cast(); } + + foreach ($this->normalizerCasts as $key => $cast) { + $this->normalizerCasts[$key] = new $cast(); + } } public static function getInstance(): ConfigManager @@ -59,6 +72,19 @@ public static function getInstance(): ConfigManager return self::$instance ??= new self(); } + /** + * @throws NotFoundAttributePropertyResolver + */ + public function addNormalizerCasts(NormalizerCastInterface|string $resolverClass): static + { + if (is_string($resolverClass) && !is_subclass_of($resolverClass, NormalizerCastInterface::class)) { + throw new NotFoundAttributePropertyResolver('Resolver class must be an instance of NormalizerCastInterface'); + } + $this->attributePropertyResolver[] = (is_string($resolverClass) ? new $resolverClass() : $resolverClass); + + return $this; + } + /** * @throws NotFoundAttributePropertyResolver */ @@ -113,6 +139,11 @@ public function getOutValueCasts(): array return $this->outputValueCasts; } + public function getNormalizerCasts(): array + { + return $this->normalizerCasts; + } + public function getCacheDriver(): string { if ($this->cacheDriver instanceof CacheDriverEnum) { diff --git a/src/Support/Context/SerializeContext.php b/src/Support/Context/SerializeContext.php index 679a861..542ec30 100644 --- a/src/Support/Context/SerializeContext.php +++ b/src/Support/Context/SerializeContext.php @@ -5,6 +5,7 @@ use Astral\Serialize\Exceptions\NotFoundGroupException; use Astral\Serialize\Faker\FakerResolver; use Astral\Serialize\Resolvers\Casts\DataCollectionCastResolver; +use Astral\Serialize\Resolvers\Casts\NormalizerCastResolver; use Astral\Serialize\Resolvers\GroupResolver; use Astral\Serialize\Resolvers\InputResolver; use Astral\Serialize\Resolvers\OutputResolver; @@ -38,6 +39,7 @@ public function __construct( private readonly InputResolver $propertyInputValueResolver, private readonly OutputResolver $propertyToArrayResolver, private readonly FakerResolver $fakerResolver, + private readonly NormalizerCastResolver $normalizerCastResolver, ) { } @@ -187,8 +189,10 @@ className: $type->className, */ public function from(mixed ...$payload): object { + $payloads = []; foreach ($payload as $field => $itemPayload) { + $itemPayload = $this->normalizerCastResolver->resolve($itemPayload); $values = is_numeric($field) && is_array($itemPayload) ? $itemPayload : [$field => $itemPayload]; $payloads = [...$payloads, ...$values]; } diff --git a/src/Support/Factories/ContextFactory.php b/src/Support/Factories/ContextFactory.php index 9b20d6a..4661f00 100644 --- a/src/Support/Factories/ContextFactory.php +++ b/src/Support/Factories/ContextFactory.php @@ -28,6 +28,7 @@ public static function build(string $className): SerializeContext propertyInputValueResolver: SerializeContainer::get()->propertyInputValueResolver(), propertyToArrayResolver: SerializeContainer::get()->propertyToArrayResolver(), fakerResolver: SerializeContainer::get()->fakerResolver(), + normalizerCastResolver: SerializeContainer::get()->normalizerCastResolver(), )); } } diff --git a/tests/Serialize/From/NormalizerFromSerializeTest.php b/tests/Serialize/From/NormalizerFromSerializeTest.php new file mode 100644 index 0000000..af69d28 --- /dev/null +++ b/tests/Serialize/From/NormalizerFromSerializeTest.php @@ -0,0 +1,55 @@ +name_one = 'one name'; + $normalizerOne->id_one = 1; + + $normalizerTwo = new NormalizerTwo(); + $normalizerTwo->name_two = 'two name'; + $normalizerTwo->id_two = 2; + + $res = NormalizerClass::from(one: $normalizerOne, two: $normalizerTwo, three: $normalizerOne); + + expect($res->one)->toBeInstanceOf(NormalizerOne::class) + ->and($res->one->name_one)->toBe('one name') + ->and($res->one->id_one)->toBe(1) + ->and($res->two)->toBeInstanceOf(NormalizerTwo::class) + ->and($res->two->name_two)->toBe('two name') + ->and($res->two->id_two)->toBe(2) + ->and($res->three)->toBeArray() + ->and($res->three)->toMatchArray([ + 'name_one' => 'one name', + 'id_one' => 1 + ]); + + $resJson = json_encode($res); + expect($resJson)->toBe('{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"one":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_one":"one name","id_one":1}},"two":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_two":"two name","id_two":2}},"three":{"name_one":"one name","id_one":1}}}'); +}); + From e9c0ea892eb910f788e7841cffe6ffbf1931a023 Mon Sep 17 00:00:00 2001 From: "350375092@qq.com" <350375092@qq.com> Date: Thu, 21 Aug 2025 16:36:28 +0800 Subject: [PATCH 2/8] add Normalizer --- src/Serialize.php | 30 ++++++++++++++++--- src/Support/Context/SerializeContext.php | 22 ++++++++++++++ .../From/NormalizerFromSerializeTest.php | 25 +++++++++++++++- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/Serialize.php b/src/Serialize.php index 04faf3b..15d9fd2 100644 --- a/src/Serialize.php +++ b/src/Serialize.php @@ -7,6 +7,14 @@ use Astral\Serialize\Support\Factories\ContextFactory; use JsonSerializable; +/** + * @method void withResponses(array $responses) static + * @see SerializeContext::withResponses() + * @method void setCode(string|int $code) static + * @see SerializeContext::setCode() + * @method void setMessage(string $message) static + * @see SerializeContext::setMessage() + */ abstract class Serialize implements JsonSerializable { private ?SerializeContext $_context = null; @@ -76,14 +84,17 @@ public function __debugInfo() public function jsonSerialize(): array { - $baseResponse = Config::get('response',[]); - if($baseResponse){ + $baseResponses = Config::get('response',[]); + $customerResponses = $this->getContext()?->getResponses() ?? []; + $responses = array_merge($baseResponses, $customerResponses); + + if($responses){ $resultData = []; - foreach ($baseResponse as $field => $item){ + foreach ($responses as $field => $item){ if($item === 'T'){ $resultData[$field] = $this->toArray(); }else{ - $resultData[$field] = $item['example'] ?? ''; + $resultData[$field] = $item['value'] ?? ($item['example'] ?? ''); } } return $resultData; @@ -91,4 +102,15 @@ public function jsonSerialize(): array return $this->toArray(); } + + public function __call(string $name, array $arguments) + { + if ($this->getContext() === null) { + $this->setContext(ContextFactory::build(static::class)); + } + + $this->getContext()->{$name}(...$arguments); + + return $this; + } } diff --git a/src/Support/Context/SerializeContext.php b/src/Support/Context/SerializeContext.php index 542ec30..ec69d62 100644 --- a/src/Support/Context/SerializeContext.php +++ b/src/Support/Context/SerializeContext.php @@ -26,6 +26,7 @@ class SerializeContext { private array $groups = []; + private array $responses = []; public function __construct( /** @var class-string */ @@ -44,6 +45,27 @@ public function __construct( } + public function setCode(string|int $code, $description ='' , $field = 'code'): void + { + $this->responses[$field] = ['description' => $description,'value' => $code]; + } + + public function setMessage(string $message, $description ='' , $field = 'message'): void + { + $this->responses[$field] = ['description' => $description,'value' => $message]; + } + + public function withResponses(array $responses): self + { + $this->responses = $responses; + return $this; + } + + public function getResponses(): array + { + return $this->responses ?? []; + } + public function getChooseSerializeContext(): ChooseSerializeContext { return $this->chooseSerializeContext; diff --git a/tests/Serialize/From/NormalizerFromSerializeTest.php b/tests/Serialize/From/NormalizerFromSerializeTest.php index af69d28..c416211 100644 --- a/tests/Serialize/From/NormalizerFromSerializeTest.php +++ b/tests/Serialize/From/NormalizerFromSerializeTest.php @@ -19,7 +19,6 @@ class NormalizerClass extends Serialize { public NormalizerOne $one; public NormalizerTwo $two; - public mixed $three; } @@ -49,7 +48,31 @@ class NormalizerClass extends Serialize 'id_one' => 1 ]); + +}); + +it('test json_encode Serialize class', function () { + + $normalizerOne = new NormalizerOne(); + $normalizerOne->name_one = 'one name'; + $normalizerOne->id_one = 1; + + $normalizerTwo = new NormalizerTwo(); + $normalizerTwo->name_two = 'two name'; + $normalizerTwo->id_two = 2; + + $res = NormalizerClass::from(one: $normalizerOne, two: $normalizerTwo, three: $normalizerOne); $resJson = json_encode($res); expect($resJson)->toBe('{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"one":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_one":"one name","id_one":1}},"two":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_two":"two name","id_two":2}},"three":{"name_one":"one name","id_one":1}}}'); + + $res->setMessage('233'); + $resJson = json_encode($res); + expect($resJson)->toBe('{"code":200,"message":"233","data":{"one":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_one":"one name","id_one":1}},"two":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_two":"two name","id_two":2}},"three":{"name_one":"one name","id_one":1}}}'); + + $res->setCode(-1); + $resJson = json_encode($res); + expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_one":"one name","id_one":1}},"two":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_two":"two name","id_two":2}},"three":{"name_one":"one name","id_one":1}}}'); + + }); From 844457b99b6b8f10bcf6ac52f70153c3b913260c Mon Sep 17 00:00:00 2001 From: "350375092@qq.com" <350375092@qq.com> Date: Fri, 29 Aug 2025 15:13:11 +0800 Subject: [PATCH 3/8] add Normalizer --- src/Serialize.php | 19 +++++++++++++++++-- src/Support/Context/SerializeContext.php | 14 ++++++++++++++ .../From/NormalizerFromSerializeTest.php | 11 ++++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Serialize.php b/src/Serialize.php index 15d9fd2..09d792c 100644 --- a/src/Serialize.php +++ b/src/Serialize.php @@ -43,6 +43,20 @@ public function withGroups(array|string $groups): static return $this; } + public function toJsonString(): bool|string + { + return json_encode($this); + } + + public function withoutResponseToJsonString(): string + { + if ($this->getContext() === null) { + $this->setContext(ContextFactory::build(static::class)); + } + + return json_encode($this->getContext()->toArrayWithoutResponse($this->toArray())); + } + public function toArray(): array { if ($this->getContext() === null) { @@ -74,6 +88,7 @@ public static function faker(): static return $instance; } + public function __debugInfo() { $res = get_object_vars($this); @@ -92,7 +107,7 @@ public function jsonSerialize(): array $resultData = []; foreach ($responses as $field => $item){ if($item === 'T'){ - $resultData[$field] = $this->toArray(); + $resultData[$field] = $this->getContext()->toArrayWithoutResponse($this->toArray()); }else{ $resultData[$field] = $item['value'] ?? ($item['example'] ?? ''); } @@ -100,7 +115,7 @@ public function jsonSerialize(): array return $resultData; } - return $this->toArray(); + return $this->getContext()->toArrayWithoutResponse($this->toArray()); } public function __call(string $name, array $arguments) diff --git a/src/Support/Context/SerializeContext.php b/src/Support/Context/SerializeContext.php index ec69d62..a8f2821 100644 --- a/src/Support/Context/SerializeContext.php +++ b/src/Support/Context/SerializeContext.php @@ -246,4 +246,18 @@ public function toArray(object $object): array $this->chooseSerializeContext->setGroups($this->getGroups()); return $this->propertyToArrayResolver->resolve($this->chooseSerializeContext, $this->getGroupCollection(), $object); } + + public function toArrayWithoutResponse($data) + { + if ($data instanceof Serialize) { + return $data->toArray(); + } elseif (is_array($data)) { + foreach ($data as $key => $value) { + $data[$key] = $this->toArrayWithoutResponse($value); + } + return $data; + } + return $data; + } + } diff --git a/tests/Serialize/From/NormalizerFromSerializeTest.php b/tests/Serialize/From/NormalizerFromSerializeTest.php index c416211..488be3f 100644 --- a/tests/Serialize/From/NormalizerFromSerializeTest.php +++ b/tests/Serialize/From/NormalizerFromSerializeTest.php @@ -63,16 +63,21 @@ class NormalizerClass extends Serialize $res = NormalizerClass::from(one: $normalizerOne, two: $normalizerTwo, three: $normalizerOne); $resJson = json_encode($res); - expect($resJson)->toBe('{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"one":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_one":"one name","id_one":1}},"two":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_two":"two name","id_two":2}},"three":{"name_one":"one name","id_one":1}}}'); + expect($resJson)->toBe('{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}}'); $res->setMessage('233'); $resJson = json_encode($res); - expect($resJson)->toBe('{"code":200,"message":"233","data":{"one":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_one":"one name","id_one":1}},"two":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_two":"two name","id_two":2}},"three":{"name_one":"one name","id_one":1}}}'); + expect($resJson)->toBe('{"code":200,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}}'); $res->setCode(-1); $resJson = json_encode($res); - expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_one":"one name","id_one":1}},"two":{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"name_two":"two name","id_two":2}},"three":{"name_one":"one name","id_one":1}}}'); + expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}}'); + $resJson = $res->withoutResponseToJsonString(); + expect($resJson)->toBe('{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}'); + + $resJson = $res->toJsonString(); + expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}}'); }); From c707aae28b17bd45f43fccbde73d87f4dbe12da8 Mon Sep 17 00:00:00 2001 From: "350375092@qq.com" <350375092@qq.com> Date: Fri, 29 Aug 2025 16:27:37 +0800 Subject: [PATCH 4/8] add Normalizer --- src/Support/Context/SerializeContext.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Support/Context/SerializeContext.php b/src/Support/Context/SerializeContext.php index a8f2821..b55a33b 100644 --- a/src/Support/Context/SerializeContext.php +++ b/src/Support/Context/SerializeContext.php @@ -250,7 +250,7 @@ public function toArray(object $object): array public function toArrayWithoutResponse($data) { if ($data instanceof Serialize) { - return $data->toArray(); + return get_object_vars($data); } elseif (is_array($data)) { foreach ($data as $key => $value) { $data[$key] = $this->toArrayWithoutResponse($value); From b3eab7ec5e6ddbfabe101fa352cdaf47ddd4e11c Mon Sep 17 00:00:00 2001 From: "350375092@qq.com" <350375092@qq.com> Date: Fri, 29 Aug 2025 17:35:23 +0800 Subject: [PATCH 5/8] fixed docs --- docs/en/annotation/alisa-annotation.md | 5 ++--- docs/en/faker/collection-faker.md | 4 ++-- docs/en/mapper/array-mapper.md | 16 ++++++++-------- docs/zh/annotation/alisa-annotation.md | 3 +-- docs/zh/faker/collection-faker.md | 4 ++-- docs/zh/mapper/array-mapper.md | 14 +++++++------- 6 files changed, 22 insertions(+), 24 deletions(-) diff --git a/docs/en/annotation/alisa-annotation.md b/docs/en/annotation/alisa-annotation.md index 7be00ae..cba8605 100644 --- a/docs/en/annotation/alisa-annotation.md +++ b/docs/en/annotation/alisa-annotation.md @@ -128,9 +128,8 @@ $complexUser = ComplexUser::from([ 'user_tags' => ['developer', 'programmer'] ]); -// Convert to standard array -$complexUserArray = $complexUser->toArray(); -// $complexUserArray toArray: +var_dump($complexUserListFaker) +// Content of $complexUserListFaker: // [ // 'profile' => UserProfile Object ([ // 'nickname' => 'job', diff --git a/docs/en/faker/collection-faker.md b/docs/en/faker/collection-faker.md index be9a1f7..4d9269b 100644 --- a/docs/en/faker/collection-faker.md +++ b/docs/en/faker/collection-faker.md @@ -21,8 +21,8 @@ $userList = UserListFaker::faker(); $complexUserListFaker = UserListFaker::faker(); -$complexUserListFakerArray = $complexUserListFaker->toArray(); -// Content of $complexUserListFakerArray: +var_dump($complexUserListFaker) +// Content of $complexUserListFaker: // [ // 'profile' => [ // [0] => UserProfile Object ( diff --git a/docs/en/mapper/array-mapper.md b/docs/en/mapper/array-mapper.md index f836f7f..ff63f73 100644 --- a/docs/en/mapper/array-mapper.md +++ b/docs/en/mapper/array-mapper.md @@ -40,8 +40,8 @@ $data1 = MultiArraySerialize::from( ] ); -$data1Array = $data1->toArray(); -// Content of $data1Array: +var_dump($data1) +// Content of $data1: // [ // 'mixedTypeArray' => [ // [0] => ArrayOne Object @@ -72,8 +72,8 @@ $data2 = MultiArraySerialize::from( ] ); -$data2Array = $data2->toArray(); -// Content of $data2Array: +var_dump($data2) +// Content of $data2: // [ // 'multiTypeArray' => [ // ArrayOne Object ( @@ -97,8 +97,8 @@ $data3 = MultiArraySerialize::from( ] ); -$data3Array = $data3->toArray(); -// $data3Array toArray: +var_dump($data3) +// Content of $data3: // [ // 'keyValueMixedArray' => [ // 'user1' => ArrayOne Object ( @@ -121,8 +121,8 @@ $data4 = MultiArraySerialize::from( ] ); -$data4Array = $data4->toArray(); -// $data4Array toArray: +var_dump($data4) +// Content of $data4: // [ // 'mixedTypeArray' => [ // ['unknown' => 'data1'], diff --git a/docs/zh/annotation/alisa-annotation.md b/docs/zh/annotation/alisa-annotation.md index ff61fb7..77ad700 100644 --- a/docs/zh/annotation/alisa-annotation.md +++ b/docs/zh/annotation/alisa-annotation.md @@ -128,8 +128,7 @@ $complexUser = ComplexUser::from([ 'user_tags' => ['developer', 'programmer'] ]); -// 转换为标准数组 -$complexUserArray = $complexUser->toArray(); +var_dump($complexUser); // $complexUserArray 的内容: // [ // 'profile' => UserProfile Object ([ diff --git a/docs/zh/faker/collection-faker.md b/docs/zh/faker/collection-faker.md index a7b2126..7208ccd 100644 --- a/docs/zh/faker/collection-faker.md +++ b/docs/zh/faker/collection-faker.md @@ -21,8 +21,8 @@ $userList = UserListFaker::faker(); $complexUserListFaker = UserListFaker::faker(); -$complexUserListFakerArray = $complexUserListFaker->toArray(); -// $complexUserListFakerArray 的内容: +var_dump($complexUserListFaker) +// $complexUserListFaker 的内容: // [ // 'profile' => [ // [0] => UserProfile Object ( diff --git a/docs/zh/mapper/array-mapper.md b/docs/zh/mapper/array-mapper.md index 17434cc..5ecc9e3 100644 --- a/docs/zh/mapper/array-mapper.md +++ b/docs/zh/mapper/array-mapper.md @@ -40,8 +40,8 @@ $data1 = MultiArraySerialize::from( ] ); -$data1Array = $data1->toArray(); -// $data1Array 的内容: +var_dump($data1) +// $data1 的内容: // [ // 'mixedTypeArray' => [ // [0] => ArrayOne Object @@ -72,8 +72,8 @@ $data2 = MultiArraySerialize::from( ] ); -$data2Array = $data2->toArray(); -// $data2Array 的内容: +var_dump($data2) +// $data2 的内容: // [ // 'multiTypeArray' => [ // ArrayOne Object ( @@ -97,8 +97,8 @@ $data3 = MultiArraySerialize::from( ] ); -$data3Array = $data3->toArray(); -// $data3Array 的内容: +var_dump($data3) +// $data3 的内容: // [ // 'keyValueMixedArray' => [ // 'user1' => ArrayOne Object ( @@ -121,7 +121,7 @@ $data4 = MultiArraySerialize::from( ] ); -$data4Array = $data4->toArray(); +var_dump($data4) // $data4Array 的内容: // [ // 'mixedTypeArray' => [ From 32a3fa26351aa34d218379c407aec4b07d6970d8 Mon Sep 17 00:00:00 2001 From: "350375092@qq.com" <350375092@qq.com> Date: Fri, 29 Aug 2025 17:35:35 +0800 Subject: [PATCH 6/8] fixed docs --- docs/en/mapper/union-mapper.md | 12 ++++++------ docs/zh/mapper/union-mapper.md | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/docs/en/mapper/union-mapper.md b/docs/en/mapper/union-mapper.md index b9f538c..e4bbfde 100644 --- a/docs/en/mapper/union-mapper.md +++ b/docs/en/mapper/union-mapper.md @@ -60,8 +60,8 @@ $data2 = FlexibleData::from([ echo $data2->userIdentifier; // Output User object echo $data2->complexIdentifier; // Output AdminUser object -$data2Array = $data2->toArray(); -// Content of $data2Array: +var_dump($data2) +// Content of $data2: // [ // 'flexibleId' => 'ABC123', // 'userIdentifier' => User Object ( @@ -87,11 +87,11 @@ $data3 = FlexibleData::from([ ] ]); -echo $data2->userIdentifier; // Output User object -echo $data2->complexIdentifier; // Output AdminUser object +echo $data3->userIdentifier; // Output User object +echo $data3->complexIdentifier; // Output AdminUser object -$data3Array = $data3->toArray(); -// Content of $data3Array: +var_dump($data3) +// Content of $data3: // [ // 'flexibleId' => 'USER001', // 'userIdentifier' => User Object ( diff --git a/docs/zh/mapper/union-mapper.md b/docs/zh/mapper/union-mapper.md index 08d838e..0e38e9b 100644 --- a/docs/zh/mapper/union-mapper.md +++ b/docs/zh/mapper/union-mapper.md @@ -60,7 +60,7 @@ $data2 = FlexibleData::from([ echo $data2->userIdentifier; // 输出 User 对象 echo $data2->complexIdentifier; // 输出 User 对象 -$data2Array = $data2->toArray(); +var_dump($data2) // $data2Array 的内容: // [ // 'flexibleId' => 'ABC123', @@ -87,10 +87,10 @@ $data3 = FlexibleData::from([ ] ]); -echo $data2->userIdentifier; // 输出 User 对象 -echo $data2->complexIdentifier; // 输出 AdminUser 对象 +echo $data3->userIdentifier; // 输出 User 对象 +echo $data3->complexIdentifier; // 输出 AdminUser 对象 -$data3Array = $data3->toArray(); +var_dump($data3) // $data3Array 的内容: // [ // 'flexibleId' => 'USER001', From 4c5c156adab2b5a513e726a983769c223a2cd008 Mon Sep 17 00:00:00 2001 From: "350375092@qq.com" <350375092@qq.com> Date: Fri, 29 Aug 2025 17:36:08 +0800 Subject: [PATCH 7/8] fixed toArray Bug --- src/Resolvers/OutputResolver.php | 6 +++--- src/Support/Config/ConfigManager.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Resolvers/OutputResolver.php b/src/Resolvers/OutputResolver.php index 01a9014..d8df062 100644 --- a/src/Resolvers/OutputResolver.php +++ b/src/Resolvers/OutputResolver.php @@ -3,6 +3,7 @@ namespace Astral\Serialize\Resolvers; use Astral\Serialize\Resolvers\Casts\OutputCastResolver; +use Astral\Serialize\Serialize; use Astral\Serialize\Support\Collections\DataCollection; use Astral\Serialize\Support\Collections\GroupDataCollection; use Astral\Serialize\Support\Context\ChoosePropertyContext; @@ -22,7 +23,6 @@ public function __construct( public function resolve(ChooseSerializeContext $chooseContext, GroupDataCollection $groupCollection, object $object): array { - $context = new OutContext( className: $groupCollection->getClassName(), classInstance: $object, @@ -32,7 +32,6 @@ classInstance: $object, $properties = $groupCollection->getProperties(); - $toArray = []; foreach ($properties as $collection) { @@ -53,8 +52,9 @@ classInstance: $object, ); foreach ($matchData['names'] as $name) { - $toArray[$name] = $resolvedValue; + $toArray[$name] = $resolvedValue instanceof Serialize ? $resolvedValue->toArray() : $resolvedValue; } + } return $toArray; diff --git a/src/Support/Config/ConfigManager.php b/src/Support/Config/ConfigManager.php index 4e8f8ca..20db6a3 100644 --- a/src/Support/Config/ConfigManager.php +++ b/src/Support/Config/ConfigManager.php @@ -38,7 +38,7 @@ class ConfigManager /** @var (OutValueCastInterface|string)[] $outputValueCasts */ private array $outputValueCasts = [ - OutArrayChildCast::class, +// OutArrayChildCast::class, OutValueEnumCast::class, OutValueGetterCast::class, ]; From 356a1ef965d96c5f330f6942a2ff976274f0dcd9 Mon Sep 17 00:00:00 2001 From: "350375092@qq.com" <350375092@qq.com> Date: Fri, 29 Aug 2025 17:36:35 +0800 Subject: [PATCH 8/8] added JsonSerializable --- src/Serialize.php | 10 +++------ src/Support/Context/SerializeContext.php | 14 ------------ .../From/NormalizerFromSerializeTest.php | 22 ++++++++++++------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/src/Serialize.php b/src/Serialize.php index 09d792c..11e4f9e 100644 --- a/src/Serialize.php +++ b/src/Serialize.php @@ -50,11 +50,7 @@ public function toJsonString(): bool|string public function withoutResponseToJsonString(): string { - if ($this->getContext() === null) { - $this->setContext(ContextFactory::build(static::class)); - } - - return json_encode($this->getContext()->toArrayWithoutResponse($this->toArray())); + return json_encode($this->toArray()); } public function toArray(): array @@ -107,7 +103,7 @@ public function jsonSerialize(): array $resultData = []; foreach ($responses as $field => $item){ if($item === 'T'){ - $resultData[$field] = $this->getContext()->toArrayWithoutResponse($this->toArray()); + $resultData[$field] = $this->toArray(); }else{ $resultData[$field] = $item['value'] ?? ($item['example'] ?? ''); } @@ -115,7 +111,7 @@ public function jsonSerialize(): array return $resultData; } - return $this->getContext()->toArrayWithoutResponse($this->toArray()); + return $this->toArray(); } public function __call(string $name, array $arguments) diff --git a/src/Support/Context/SerializeContext.php b/src/Support/Context/SerializeContext.php index b55a33b..b601405 100644 --- a/src/Support/Context/SerializeContext.php +++ b/src/Support/Context/SerializeContext.php @@ -211,7 +211,6 @@ className: $type->className, */ public function from(mixed ...$payload): object { - $payloads = []; foreach ($payload as $field => $itemPayload) { $itemPayload = $this->normalizerCastResolver->resolve($itemPayload); @@ -247,17 +246,4 @@ public function toArray(object $object): array return $this->propertyToArrayResolver->resolve($this->chooseSerializeContext, $this->getGroupCollection(), $object); } - public function toArrayWithoutResponse($data) - { - if ($data instanceof Serialize) { - return get_object_vars($data); - } elseif (is_array($data)) { - foreach ($data as $key => $value) { - $data[$key] = $this->toArrayWithoutResponse($value); - } - return $data; - } - return $data; - } - } diff --git a/tests/Serialize/From/NormalizerFromSerializeTest.php b/tests/Serialize/From/NormalizerFromSerializeTest.php index 488be3f..9feac3a 100644 --- a/tests/Serialize/From/NormalizerFromSerializeTest.php +++ b/tests/Serialize/From/NormalizerFromSerializeTest.php @@ -1,5 +1,7 @@ 'one name', 'id_one' => 1 ]); - +// }); @@ -62,22 +67,23 @@ class NormalizerClass extends Serialize $normalizerTwo->id_two = 2; $res = NormalizerClass::from(one: $normalizerOne, two: $normalizerTwo, three: $normalizerOne); + $resJson = json_encode($res); - expect($resJson)->toBe('{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}}'); + expect($resJson)->toBe('{"code":200,"message":"\u64cd\u4f5c\u6210\u529f","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}}'); $res->setMessage('233'); $resJson = json_encode($res); - expect($resJson)->toBe('{"code":200,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}}'); + expect($resJson)->toBe('{"code":200,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}}'); $res->setCode(-1); $resJson = json_encode($res); - expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}}'); + expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}}'); $resJson = $res->withoutResponseToJsonString(); - expect($resJson)->toBe('{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}'); + var_dump($resJson); + expect($resJson)->toBe('{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}'); $resJson = $res->toJsonString(); - expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_two":2},"three":{"name_one":"one name","id_one":1}}}'); - -}); + expect($resJson)->toBe('{"code":-1,"message":"233","data":{"one":{"name_one":"one name","id_one":1},"two":{"name_two":"two name","id_2":2},"three":{"name_one":"one name","id_one":1}}}'); +}); \ No newline at end of file