44
55namespace Jerowork \GraphqlAttributeSchema \Parser \Node \Child ;
66
7+ use Jerowork \GraphqlAttributeSchema \Parser \Node \ArraySerializable ;
78use Jerowork \GraphqlAttributeSchema \Parser \Node \Type ;
89
9- final readonly class FieldNode
10+ /**
11+ * @phpstan-import-type ArgNodePayload from ArgNode
12+ * @phpstan-import-type TypePayload from Type
13+ *
14+ * @phpstan-type FieldNodePayload array{
15+ * type: TypePayload,
16+ * name: string,
17+ * description: null|string,
18+ * argNodes: list<ArgNodePayload>,
19+ * fieldType: string,
20+ * methodName: null|string,
21+ * propertyName: null|string,
22+ * deprecationReason: null|string
23+ * }
24+ *
25+ * @implements ArraySerializable<FieldNodePayload>
26+ */
27+ final readonly class FieldNode implements ArraySerializable
1028{
1129 /**
1230 * @param list<ArgNode> $argNodes
@@ -21,4 +39,32 @@ public function __construct(
2139 public ?string $ propertyName ,
2240 public ?string $ deprecationReason ,
2341 ) {}
42+
43+ public function toArray (): array
44+ {
45+ return [
46+ 'type ' => $ this ->type ->toArray (),
47+ 'name ' => $ this ->name ,
48+ 'description ' => $ this ->description ,
49+ 'argNodes ' => array_map (fn ($ argNode ) => $ argNode ->toArray (), $ this ->argNodes ),
50+ 'fieldType ' => $ this ->fieldType ->value ,
51+ 'methodName ' => $ this ->methodName ,
52+ 'propertyName ' => $ this ->propertyName ,
53+ 'deprecationReason ' => $ this ->deprecationReason ,
54+ ];
55+ }
56+
57+ public static function fromArray (array $ payload ): FieldNode
58+ {
59+ return new self (
60+ Type::fromArray ($ payload ['type ' ]),
61+ $ payload ['name ' ],
62+ $ payload ['description ' ],
63+ array_map (fn ($ argNodePayload ) => ArgNode::fromArray ($ argNodePayload ), $ payload ['argNodes ' ]),
64+ FieldNodeType::from ($ payload ['fieldType ' ]),
65+ $ payload ['methodName ' ],
66+ $ payload ['propertyName ' ],
67+ $ payload ['deprecationReason ' ],
68+ );
69+ }
2470}
0 commit comments