Skip to content

Commit 06472b3

Browse files
committed
refactor(codegen): simplify request DTO constructors via hydrator
Replace generated per-request constructor validation/coercion boilerplate with a minimal Hydrator-based constructor pattern. Extend Hydrator to hydrate into an existing instance and regenerate request DTO/service-local request classes accordingly. Update constructor behavior tests to cover the lightweight flow.
1 parent f1d668d commit 06472b3

File tree

11 files changed

+216
-2
lines changed

11 files changed

+216
-2
lines changed

codegen/pkg/generator/generator.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,32 @@ func (g *Generator) buildPHPClass(name string, schema *base.SchemaProxy, current
276276
buf.WriteString(propCode)
277277
}
278278

279+
if strings.HasSuffix(name, "Request") && name != "BadRequest" {
280+
buf.WriteString(g.buildRequestArrayConstructor(properties))
281+
}
282+
279283
buf.WriteString("}\n")
280284
return buf.String()
281285
}
282286

287+
func (g *Generator) buildRequestArrayConstructor(_ []phpProperty) string {
288+
var buf strings.Builder
289+
290+
buf.WriteString(" /**\n")
291+
buf.WriteString(" * Create request DTO from an associative array.\n")
292+
buf.WriteString(" *\n")
293+
buf.WriteString(" * @param array<string, mixed> $data\n")
294+
buf.WriteString(" */\n")
295+
buf.WriteString(" public function __construct(array $data = [])\n")
296+
buf.WriteString(" {\n")
297+
buf.WriteString(" if ($data !== []) {\n")
298+
buf.WriteString(" \\SumUp\\Hydrator::hydrate($data, self::class, $this);\n")
299+
buf.WriteString(" }\n")
300+
buf.WriteString(" }\n\n")
301+
302+
return buf.String()
303+
}
304+
283305
func (g *Generator) displayTagName(tagKey string) string {
284306
if tagKey == sharedTagKey {
285307
return sharedTagDisplayName

src/Customers/Customers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ class CustomersUpdateRequest
2121
*/
2222
public ?\SumUp\Types\PersonalDetails $personalDetails = null;
2323

24+
/**
25+
* Create request DTO from an associative array.
26+
*
27+
* @param array<string, mixed> $data
28+
*/
29+
public function __construct(array $data = [])
30+
{
31+
if ($data !== []) {
32+
\SumUp\Hydrator::hydrate($data, self::class, $this);
33+
}
34+
}
35+
2436
}
2537

2638
/**

src/Hydrator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ class Hydrator
2323
*
2424
* @param mixed $payload
2525
* @param string $className
26+
* @param object|null $target Existing instance to hydrate.
2627
*
2728
* @return mixed
2829
*/
29-
public static function hydrate($payload, $className)
30+
public static function hydrate($payload, $className, $target = null)
3031
{
3132
if ($payload === null || $className === '' || !class_exists($className)) {
3233
return $payload;
@@ -40,7 +41,7 @@ public static function hydrate($payload, $className)
4041
return $payload;
4142
}
4243

43-
$object = new $className();
44+
$object = ($target instanceof $className) ? $target : new $className();
4445
$properties = self::getClassProperties($className);
4546

4647
foreach ($payload as $key => $value) {

src/Members/Members.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,18 @@ class MembersCreateRequest
7979
*/
8080
public ?array $attributes = null;
8181

82+
/**
83+
* Create request DTO from an associative array.
84+
*
85+
* @param array<string, mixed> $data
86+
*/
87+
public function __construct(array $data = [])
88+
{
89+
if ($data !== []) {
90+
\SumUp\Hydrator::hydrate($data, self::class, $this);
91+
}
92+
}
93+
8294
}
8395

8496
class MembersUpdateRequest
@@ -110,6 +122,18 @@ class MembersUpdateRequest
110122
*/
111123
public ?array $user = null;
112124

125+
/**
126+
* Create request DTO from an associative array.
127+
*
128+
* @param array<string, mixed> $data
129+
*/
130+
public function __construct(array $data = [])
131+
{
132+
if ($data !== []) {
133+
\SumUp\Hydrator::hydrate($data, self::class, $this);
134+
}
135+
}
136+
113137
}
114138

115139
/**

src/Readers/Readers.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ class ReadersCreateRequest
4545
*/
4646
public ?array $metadata = null;
4747

48+
/**
49+
* Create request DTO from an associative array.
50+
*
51+
* @param array<string, mixed> $data
52+
*/
53+
public function __construct(array $data = [])
54+
{
55+
if ($data !== []) {
56+
\SumUp\Hydrator::hydrate($data, self::class, $this);
57+
}
58+
}
59+
4860
}
4961

5062
/**
@@ -72,6 +84,18 @@ class ReadersUpdateRequest
7284
*/
7385
public ?array $metadata = null;
7486

87+
/**
88+
* Create request DTO from an associative array.
89+
*
90+
* @param array<string, mixed> $data
91+
*/
92+
public function __construct(array $data = [])
93+
{
94+
if ($data !== []) {
95+
\SumUp\Hydrator::hydrate($data, self::class, $this);
96+
}
97+
}
98+
7599
}
76100

77101
/**

src/Roles/Roles.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,18 @@ class RolesCreateRequest
5252
*/
5353
public ?string $description = null;
5454

55+
/**
56+
* Create request DTO from an associative array.
57+
*
58+
* @param array<string, mixed> $data
59+
*/
60+
public function __construct(array $data = [])
61+
{
62+
if ($data !== []) {
63+
\SumUp\Hydrator::hydrate($data, self::class, $this);
64+
}
65+
}
66+
5567
}
5668

5769
class RolesUpdateRequest
@@ -77,6 +89,18 @@ class RolesUpdateRequest
7789
*/
7890
public ?string $description = null;
7991

92+
/**
93+
* Create request DTO from an associative array.
94+
*
95+
* @param array<string, mixed> $data
96+
*/
97+
public function __construct(array $data = [])
98+
{
99+
if ($data !== []) {
100+
\SumUp\Hydrator::hydrate($data, self::class, $this);
101+
}
102+
}
103+
80104
}
81105

82106
/**

src/Subaccounts/Subaccounts.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,18 @@ class SubaccountsCreateSubAccountRequest
3838
*/
3939
public ?array $permissions = null;
4040

41+
/**
42+
* Create request DTO from an associative array.
43+
*
44+
* @param array<string, mixed> $data
45+
*/
46+
public function __construct(array $data = [])
47+
{
48+
if ($data !== []) {
49+
\SumUp\Hydrator::hydrate($data, self::class, $this);
50+
}
51+
}
52+
4153
}
4254

4355
class SubaccountsUpdateSubAccountRequest
@@ -72,6 +84,18 @@ class SubaccountsUpdateSubAccountRequest
7284
*/
7385
public ?array $permissions = null;
7486

87+
/**
88+
* Create request DTO from an associative array.
89+
*
90+
* @param array<string, mixed> $data
91+
*/
92+
public function __construct(array $data = [])
93+
{
94+
if ($data !== []) {
95+
\SumUp\Hydrator::hydrate($data, self::class, $this);
96+
}
97+
}
98+
7599
}
76100

77101
/**

src/Transactions/Transactions.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ class TransactionsRefundRequest
5656
*/
5757
public ?float $amount = null;
5858

59+
/**
60+
* Create request DTO from an associative array.
61+
*
62+
* @param array<string, mixed> $data
63+
*/
64+
public function __construct(array $data = [])
65+
{
66+
if ($data !== []) {
67+
\SumUp\Hydrator::hydrate($data, self::class, $this);
68+
}
69+
}
70+
5971
}
6072

6173
/**

src/Types/CheckoutCreateRequest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,16 @@ class CheckoutCreateRequest
107107
*/
108108
public ?string $redirectUrl = null;
109109

110+
/**
111+
* Create request DTO from an associative array.
112+
*
113+
* @param array<string, mixed> $data
114+
*/
115+
public function __construct(array $data = [])
116+
{
117+
if ($data !== []) {
118+
\SumUp\Hydrator::hydrate($data, self::class, $this);
119+
}
120+
}
121+
110122
}

src/Types/CreateReaderCheckoutRequest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ class CreateReaderCheckoutRequest
7979
*/
8080
public array $totalAmount;
8181

82+
/**
83+
* Create request DTO from an associative array.
84+
*
85+
* @param array<string, mixed> $data
86+
*/
87+
public function __construct(array $data = [])
88+
{
89+
if ($data !== []) {
90+
\SumUp\Hydrator::hydrate($data, self::class, $this);
91+
}
92+
}
93+
8294
}

0 commit comments

Comments
 (0)