Skip to content

Commit 356a1ef

Browse files
committed
added JsonSerializable
1 parent 4c5c156 commit 356a1ef

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

src/Serialize.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ public function toJsonString(): bool|string
5050

5151
public function withoutResponseToJsonString(): string
5252
{
53-
if ($this->getContext() === null) {
54-
$this->setContext(ContextFactory::build(static::class));
55-
}
56-
57-
return json_encode($this->getContext()->toArrayWithoutResponse($this->toArray()));
53+
return json_encode($this->toArray());
5854
}
5955

6056
public function toArray(): array
@@ -107,15 +103,15 @@ public function jsonSerialize(): array
107103
$resultData = [];
108104
foreach ($responses as $field => $item){
109105
if($item === 'T'){
110-
$resultData[$field] = $this->getContext()->toArrayWithoutResponse($this->toArray());
106+
$resultData[$field] = $this->toArray();
111107
}else{
112108
$resultData[$field] = $item['value'] ?? ($item['example'] ?? '');
113109
}
114110
}
115111
return $resultData;
116112
}
117113

118-
return $this->getContext()->toArrayWithoutResponse($this->toArray());
114+
return $this->toArray();
119115
}
120116

121117
public function __call(string $name, array $arguments)

src/Support/Context/SerializeContext.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,6 @@ className: $type->className,
211211
*/
212212
public function from(mixed ...$payload): object
213213
{
214-
215214
$payloads = [];
216215
foreach ($payload as $field => $itemPayload) {
217216
$itemPayload = $this->normalizerCastResolver->resolve($itemPayload);
@@ -247,17 +246,4 @@ public function toArray(object $object): array
247246
return $this->propertyToArrayResolver->resolve($this->chooseSerializeContext, $this->getGroupCollection(), $object);
248247
}
249248

250-
public function toArrayWithoutResponse($data)
251-
{
252-
if ($data instanceof Serialize) {
253-
return get_object_vars($data);
254-
} elseif (is_array($data)) {
255-
foreach ($data as $key => $value) {
256-
$data[$key] = $this->toArrayWithoutResponse($value);
257-
}
258-
return $data;
259-
}
260-
return $data;
261-
}
262-
263249
}

tests/Serialize/From/NormalizerFromSerializeTest.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Astral\Serialize\Annotations\DataCollection\InputName;
4+
use Astral\Serialize\Annotations\DataCollection\OutputName;
35
use Astral\Serialize\Serialize;
46

57
beforeAll(function () {
@@ -12,6 +14,9 @@ class NormalizerOne extends Serialize
1214
class NormalizerTwo extends Serialize
1315
{
1416
public string $name_two;
17+
18+
#[InputName('id_2')]
19+
#[OutputName('id_2')]
1520
public int $id_two;
1621
}
1722

@@ -47,7 +52,7 @@ class NormalizerClass extends Serialize
4752
'name_one' => 'one name',
4853
'id_one' => 1
4954
]);
50-
55+
//
5156

5257
});
5358

@@ -62,22 +67,23 @@ class NormalizerClass extends Serialize
6267
$normalizerTwo->id_two = 2;
6368

6469
$res = NormalizerClass::from(one: $normalizerOne, two: $normalizerTwo, three: $normalizerOne);
70+
6571
$resJson = json_encode($res);
66-
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}}}');
72+
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}}}');
6773

6874
$res->setMessage('233');
6975
$resJson = json_encode($res);
70-
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}}}');
76+
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}}}');
7177

7278
$res->setCode(-1);
7379
$resJson = json_encode($res);
74-
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}}}');
80+
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}}}');
7581

7682
$resJson = $res->withoutResponseToJsonString();
77-
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}}');
83+
var_dump($resJson);
84+
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}}');
7885

7986
$resJson = $res->toJsonString();
80-
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}}}');
81-
82-
});
87+
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}}}');
8388

89+
});

0 commit comments

Comments
 (0)