Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions docs/en/annotation/alisa-annotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions docs/en/faker/collection-faker.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
16 changes: 8 additions & 8 deletions docs/en/mapper/array-mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ $data1 = MultiArraySerialize::from(
]
);

$data1Array = $data1->toArray();
// Content of $data1Array:
var_dump($data1)
// Content of $data1:
// [
// 'mixedTypeArray' => [
// [0] => ArrayOne Object
Expand Down Expand Up @@ -72,8 +72,8 @@ $data2 = MultiArraySerialize::from(
]
);

$data2Array = $data2->toArray();
// Content of $data2Array:
var_dump($data2)
// Content of $data2:
// [
// 'multiTypeArray' => [
// ArrayOne Object (
Expand All @@ -97,8 +97,8 @@ $data3 = MultiArraySerialize::from(
]
);

$data3Array = $data3->toArray();
// $data3Array toArray:
var_dump($data3)
// Content of $data3:
// [
// 'keyValueMixedArray' => [
// 'user1' => ArrayOne Object (
Expand All @@ -121,8 +121,8 @@ $data4 = MultiArraySerialize::from(
]
);

$data4Array = $data4->toArray();
// $data4Array toArray:
var_dump($data4)
// Content of $data4:
// [
// 'mixedTypeArray' => [
// ['unknown' => 'data1'],
Expand Down
12 changes: 6 additions & 6 deletions docs/en/mapper/union-mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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 (
Expand Down
3 changes: 1 addition & 2 deletions docs/zh/annotation/alisa-annotation.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ $complexUser = ComplexUser::from([
'user_tags' => ['developer', 'programmer']
]);

// 转换为标准数组
$complexUserArray = $complexUser->toArray();
var_dump($complexUser);
// $complexUserArray 的内容:
// [
// 'profile' => UserProfile Object ([
Expand Down
4 changes: 2 additions & 2 deletions docs/zh/faker/collection-faker.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ $userList = UserListFaker::faker();

$complexUserListFaker = UserListFaker::faker();

$complexUserListFakerArray = $complexUserListFaker->toArray();
// $complexUserListFakerArray 的内容:
var_dump($complexUserListFaker)
// $complexUserListFaker 的内容:
// [
// 'profile' => [
// [0] => UserProfile Object (
Expand Down
14 changes: 7 additions & 7 deletions docs/zh/mapper/array-mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ $data1 = MultiArraySerialize::from(
]
);

$data1Array = $data1->toArray();
// $data1Array 的内容:
var_dump($data1)
// $data1 的内容:
// [
// 'mixedTypeArray' => [
// [0] => ArrayOne Object
Expand Down Expand Up @@ -72,8 +72,8 @@ $data2 = MultiArraySerialize::from(
]
);

$data2Array = $data2->toArray();
// $data2Array 的内容:
var_dump($data2)
// $data2 的内容:
// [
// 'multiTypeArray' => [
// ArrayOne Object (
Expand All @@ -97,8 +97,8 @@ $data3 = MultiArraySerialize::from(
]
);

$data3Array = $data3->toArray();
// $data3Array 的内容:
var_dump($data3)
// $data3 的内容:
// [
// 'keyValueMixedArray' => [
// 'user1' => ArrayOne Object (
Expand All @@ -121,7 +121,7 @@ $data4 = MultiArraySerialize::from(
]
);

$data4Array = $data4->toArray();
var_dump($data4)
// $data4Array 的内容:
// [
// 'mixedTypeArray' => [
Expand Down
8 changes: 4 additions & 4 deletions docs/zh/mapper/union-mapper.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ $data2 = FlexibleData::from([
echo $data2->userIdentifier; // 输出 User 对象
echo $data2->complexIdentifier; // 输出 User 对象

$data2Array = $data2->toArray();
var_dump($data2)
// $data2Array 的内容:
// [
// 'flexibleId' => 'ABC123',
Expand All @@ -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',
Expand Down
22 changes: 22 additions & 0 deletions src/Casts/Normalizer/ArrayNormalizerCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Astral\Serialize\Casts\Normalizer;

use Astral\Serialize\Contracts\Normalizer\NormalizerCastInterface;

class ArrayNormalizerCast implements NormalizerCastInterface
{
public function match(mixed $values): bool
{
return is_object($values) && method_exists($values, 'toArray');
}

public function resolve(mixed $values): mixed
{
if($this->match($values)){
return $values->toArray();
}

return $values;
}
}
28 changes: 28 additions & 0 deletions src/Casts/Normalizer/JsonNormalizerCast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Astral\Serialize\Casts\Normalizer;

use Astral\Serialize\Contracts\Normalizer\NormalizerCastInterface;
use JsonException;

class JsonNormalizerCast implements NormalizerCastInterface
{
public function match(mixed $values): bool
{
return is_string($values);
}

public function resolve(mixed $values): array
{
if($this->match($values)){
try {
$decoded = json_decode($values, true, 512, JSON_THROW_ON_ERROR);
return is_array($decoded) ? $decoded : $values;
} catch (JsonException $e) {

}
}

return $values;
}
}
10 changes: 10 additions & 0 deletions src/Contracts/Normalizer/NormalizerCastInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Astral\Serialize\Contracts\Normalizer;

interface NormalizerCastInterface
{
public function match(mixed $values): bool;

public function resolve(mixed $values): mixed;
}
25 changes: 25 additions & 0 deletions src/Resolvers/Casts/NormalizerCastResolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Astral\Serialize\Resolvers\Casts;

use Astral\Serialize\Support\Collections\DataCollection;
use Astral\Serialize\Support\Config\ConfigManager;
use Astral\Serialize\Support\Context\InputValueContext;

class NormalizerCastResolver
{
public function __construct(
private readonly ConfigManager $configManager
) {

}

public function resolve(mixed $values): mixed
{
foreach ($this->configManager->getNormalizerCasts() as $cast) {
$values = $cast->resolve($values);
}

return $values;
}
}
6 changes: 3 additions & 3 deletions src/Resolvers/OutputResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -32,7 +32,6 @@ classInstance: $object,

$properties = $groupCollection->getProperties();


$toArray = [];
foreach ($properties as $collection) {

Expand All @@ -53,8 +52,9 @@ classInstance: $object,
);

foreach ($matchData['names'] as $name) {
$toArray[$name] = $resolvedValue;
$toArray[$name] = $resolvedValue instanceof Serialize ? $resolvedValue->toArray() : $resolvedValue;
}

}

return $toArray;
Expand Down
41 changes: 37 additions & 4 deletions src/Serialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,6 +43,16 @@ public function withGroups(array|string $groups): static
return $this;
}

public function toJsonString(): bool|string
{
return json_encode($this);
}

public function withoutResponseToJsonString(): string
{
return json_encode($this->toArray());
}

public function toArray(): array
{
if ($this->getContext() === null) {
Expand Down Expand Up @@ -66,6 +84,7 @@ public static function faker(): static
return $instance;
}


public function __debugInfo()
{
$res = get_object_vars($this);
Expand All @@ -76,19 +95,33 @@ 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;
}

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;
}
}
Loading