-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplain-object-example.php
More file actions
294 lines (233 loc) · 8.21 KB
/
plain-object-example.php
File metadata and controls
294 lines (233 loc) · 8.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use event4u\DataHelpers\SimpleDto;
use event4u\DataHelpers\SimpleDto\Attributes\HasDto;
use event4u\DataHelpers\SimpleDto\Attributes\HasObject;
use event4u\DataHelpers\SimpleDto\SimpleDtoObjectTrait;
use event4u\DataHelpers\Traits\ObjectMappingTrait;
// ============================================================================
// Example 1: Plain PHP Object with Public Properties
// ============================================================================
echo "Example 1: Plain PHP Object with Public Properties\n";
echo str_repeat('=', 80) . "\n\n";
class Product
{
public int $id = 0;
public string $name = '';
public float $price = 0.0;
public ?string $description = null;
}
class ProductDto extends SimpleDto
{
use SimpleDtoObjectTrait;
public function __construct(
public readonly int $id,
public readonly string $name,
public readonly float $price,
public readonly ?string $description = null,
) {}
}
// Create plain object
$product = new Product();
$product->id = 1;
$product->name = 'Laptop';
$product->price = 999.99;
$product->description = 'High-performance laptop';
// Convert to DTO
$productDto = ProductDto::fromObject($product);
echo "Original Product:\n";
echo sprintf(' ID: %d%s', $product->id, PHP_EOL);
echo sprintf(' Name: %s%s', $product->name, PHP_EOL);
echo sprintf(' Price: %s%s', $product->price, PHP_EOL);
echo " Description: {$product->description}\n\n";
echo "Product DTO:\n";
echo sprintf(' ID: %s%s', $productDto->id, PHP_EOL);
echo sprintf(' Name: %s%s', $productDto->name, PHP_EOL);
echo sprintf(' Price: %s%s', $productDto->price, PHP_EOL);
echo " Description: {$productDto->description}\n\n";
// Convert back to object
$newProduct = $productDto->toObject(Product::class);
echo "Converted back to Product:\n";
echo sprintf(' ID: %s%s', $newProduct->id, PHP_EOL);
echo sprintf(' Name: %s%s', $newProduct->name, PHP_EOL);
echo sprintf(' Price: %s%s', $newProduct->price, PHP_EOL);
echo " Description: {$newProduct->description}\n\n";
// ============================================================================
// Example 2: Object with Getters and Setters
// ============================================================================
echo "\nExample 2: Object with Getters and Setters\n";
echo str_repeat('=', 80) . "\n\n";
class Customer
{
private int $id = 0;
private string $name = '';
private string $email = '';
public function getId(): int
{
return $this->id;
}
public function setId(int $id): void
{
$this->id = $id;
}
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getEmail(): string
{
return $this->email;
}
public function setEmail(string $email): void
{
$this->email = $email;
}
}
class CustomerDto extends SimpleDto
{
use SimpleDtoObjectTrait;
public function __construct(
public readonly int $id,
public readonly string $name,
public readonly string $email,
) {}
}
// Create customer with setters
$customer = new Customer();
$customer->setId(42);
$customer->setName('John Doe');
$customer->setEmail('john@example.com');
// Convert to DTO (uses getters)
$customerDto = CustomerDto::fromObject($customer);
echo "Original Customer:\n";
echo sprintf(' ID: %d%s', $customer->getId(), PHP_EOL);
echo sprintf(' Name: %s%s', $customer->getName(), PHP_EOL);
echo " Email: {$customer->getEmail()}\n\n";
echo "Customer DTO:\n";
echo sprintf(' ID: %d%s', $customerDto->id, PHP_EOL);
echo sprintf(' Name: %s%s', $customerDto->name, PHP_EOL);
echo " Email: {$customerDto->email}\n\n";
// Convert back to object (uses setters)
$newCustomer = $customerDto->toObject(Customer::class);
echo "Converted back to Customer:\n";
echo sprintf(' ID: %s%s', $newCustomer->getId(), PHP_EOL);
echo sprintf(' Name: %s%s', $newCustomer->getName(), PHP_EOL);
echo " Email: {$newCustomer->getEmail()}\n\n";
// ============================================================================
// Example 3: Using HasObject Attribute
// ============================================================================
echo "\nExample 3: Using HasObject Attribute\n";
echo str_repeat('=', 80) . "\n\n";
class Order
{
public int $id = 0;
public string $orderNumber = '';
public float $total = 0.0;
}
#[HasObject(Order::class)]
class OrderDto extends SimpleDto
{
use SimpleDtoObjectTrait;
public function __construct(
public readonly int $id,
public readonly string $orderNumber,
public readonly float $total,
) {}
}
$orderDto = new OrderDto(1, 'ORD-2024-001', 1499.99);
echo "Order DTO:\n";
echo sprintf(' ID: %d%s', $orderDto->id, PHP_EOL);
echo sprintf(' Order Number: %s%s', $orderDto->orderNumber, PHP_EOL);
echo " Total: {$orderDto->total}\n\n";
// No need to specify class - uses HasObject attribute
$order = $orderDto->toObject();
echo "Converted to Order (using HasObject attribute):\n";
echo sprintf(' ID: %s%s', $order->id, PHP_EOL);
echo sprintf(' Order Number: %s%s', $order->orderNumber, PHP_EOL);
echo " Total: {$order->total}\n\n";
// ============================================================================
// Example 4: Using ObjectMappingTrait with HasDto Attribute
// ============================================================================
echo "\nExample 4: Using ObjectMappingTrait with HasDto Attribute\n";
echo str_repeat('=', 80) . "\n\n";
class InvoiceDto extends SimpleDto
{
use SimpleDtoObjectTrait;
public function __construct(
public readonly int $id,
public readonly string $invoiceNumber,
public readonly float $amount,
) {}
}
#[HasDto(InvoiceDto::class)]
class Invoice
{
use ObjectMappingTrait;
public int $id = 0;
public string $invoiceNumber = '';
public float $amount = 0.0;
}
$invoice = new Invoice();
$invoice->id = 1;
$invoice->invoiceNumber = 'INV-2024-001';
$invoice->amount = 2499.99;
echo "Original Invoice:\n";
echo sprintf(' ID: %d%s', $invoice->id, PHP_EOL);
echo sprintf(' Invoice Number: %s%s', $invoice->invoiceNumber, PHP_EOL);
echo " Amount: {$invoice->amount}\n\n";
// Convert to DTO using HasDto attribute
$invoiceDto = $invoice->toDto();
echo "Invoice DTO (using HasDto attribute):\n";
echo sprintf(' ID: %s%s', $invoiceDto->id, PHP_EOL);
echo sprintf(' Invoice Number: %s%s', $invoiceDto->invoiceNumber, PHP_EOL);
echo " Amount: {$invoiceDto->amount}\n\n";
// ============================================================================
// Example 5: Round-Trip Conversion
// ============================================================================
echo "\nExample 5: Round-Trip Conversion\n";
echo str_repeat('=', 80) . "\n\n";
class Article
{
public int $id = 0;
public string $title = '';
public string $content = '';
}
class ArticleDto extends SimpleDto
{
use SimpleDtoObjectTrait;
public function __construct(
public readonly int $id,
public readonly string $title,
public readonly string $content,
) {}
}
// Original object
$originalArticle = new Article();
$originalArticle->id = 1;
$originalArticle->title = 'Plain Object Integration';
$originalArticle->content = 'This is a great feature!';
echo "Original Article:\n";
echo sprintf(' ID: %d%s', $originalArticle->id, PHP_EOL);
echo sprintf(' Title: %s%s', $originalArticle->title, PHP_EOL);
echo " Content: {$originalArticle->content}\n\n";
// Object → DTO → Object
$articleDto = ArticleDto::fromObject($originalArticle);
$newArticle = $articleDto->toObject(Article::class);
echo "After Round-Trip:\n";
echo sprintf(' ID: %s%s', $newArticle->id, PHP_EOL);
echo sprintf(' Title: %s%s', $newArticle->title, PHP_EOL);
echo " Content: {$newArticle->content}\n\n";
echo "Data preserved: " . (
$newArticle->id === $originalArticle->id &&
$newArticle->title === $originalArticle->title &&
$newArticle->content === $originalArticle->content
? '✅ YES' : '❌ NO'
) . "\n\n";
echo "\n" . str_repeat('=', 80) . "\n";
echo "All examples completed successfully!\n";
echo str_repeat('=', 80) . "\n";