-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtry-advanced-transfer-generator.php
More file actions
150 lines (116 loc) · 4.59 KB
/
try-advanced-transfer-generator.php
File metadata and controls
150 lines (116 loc) · 4.59 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
<?php
declare(strict_types=1);
use Picamator\Examples\TransferObject\Advanced\AddressData;
use Picamator\Examples\TransferObject\Advanced\CredentialsData;
use Picamator\Examples\TransferObject\Generated\AdvancedTransferGenerator\AdvancedCustomerTransfer;
use Picamator\Examples\TransferObject\Generated\TransferGenerator\CustomerTransfer;
use Picamator\TransferObject\TransferGenerator\TransferGeneratorFacade;
require_once __DIR__ . '/../vendor/autoload.php';
echo <<<'STORY'
=======================================================
How to use custom Data Transfer Object
and
Apply Transfer Object across modules
=======================================================
STORY;
echo <<<'STORY'
=======================================================
Let's take Generated\TransferGenerator\CustomerTransfer
=======================================================
STORY;
$customerTransfer = new CustomerTransfer();
$customerTransfer->firstName = 'Ignacy';
$customerTransfer->lastName = 'Rzecki';
echo <<<'STORY'
=======================================================
Let's take Advanced\CredentialsData
use
DummyTransferAdapterTrait
=======================================================
STORY;
$credentialsData = new CredentialsData();
$credentialsData->login = 'ignacy.rzecki';
$credentialsData->token = 'Lalka';
$encodedCredentialsData = json_encode($credentialsData);
$iteratedCredentialsData = implode(', ', iterator_to_array($credentialsData));
echo <<<DEBUG
Count: {$credentialsData->count()}
JSON encode: $encodedCredentialsData
Iterated: [$iteratedCredentialsData]
DEBUG;
echo <<<'STORY'
=======================================================
Let's take Advanced\AddressData
use
TransferAdapterTrait
=======================================================
STORY;
$addressData = new AddressData();
$addressData->street = 'Krakowskie Przedmieście';
$addressData->houseNumber = '9';
$addressData->city = 'Warszawa';
$addressData->postCode = '00-068';
$addressData->country = 'Polska';
$encodedAddressData = json_encode($addressData);
$iteratedAddressData = implode(', ', iterator_to_array($addressData));
echo <<<DEBUG
Count: {$addressData->count()}
JSON encode: $encodedAddressData
Iterated: [$iteratedAddressData]
DEBUG;
echo <<<'STORY'
=======================================================
Create a Definition file combining all objects
=======================================================
AdvancedCustomer:
customer:
type: "Picamator\\Examples\\TransferObject\\Generated\\TransferGenerator\\CustomerTransfer"
address:
type: "Picamator\\Examples\\TransferObject\\Advanced\\AddressData"
credentials:
type: "Picamator\\Examples\\TransferObject\\Advanced\\CredentialsData"
STORY;
echo <<<'STORY'
=======================================================
Generate Transfer Object
with notice
for demo the exception handling was skipped
=======================================================
STORY;
$configPath = __DIR__ . '/config/advanced-transfer-generator/generator.config.yml';
$generatedTransferCount = new TransferGeneratorFacade()->generateTransfersOrFail($configPath);
echo "Generated $generatedTransferCount transfer objects.\n";
echo <<<'STORY'
=======================================================
Try newly Generated Transfer Object
=======================================================
STORY;
$advancedCustomerTransfer = new AdvancedCustomerTransfer();
$advancedCustomerTransfer->customer = $customerTransfer;
$advancedCustomerTransfer->address = $addressData;
$advancedCustomerTransfer->credentials = $credentialsData;
var_dump($advancedCustomerTransfer->toArray());
echo <<<'STORY'
=======================================================
Try how fromArray() works
=======================================================
STORY;
$advancedCustomerTransfer = new AdvancedCustomerTransfer()
->fromArray([
AdvancedCustomerTransfer::CUSTOMER_PROP => [
CustomerTransfer::FIRST_NAME_PROP => 'Theodor',
CustomerTransfer::LAST_NAME_PROP => 'Storm',
],
AdvancedCustomerTransfer::ADDRESS_PROP => [
'street' => 'Wasserreihe',
'houseNumber' => '31',
'city' => 'Husum',
'postCode' => '25813',
'country' => 'Deutschland',
],
AdvancedCustomerTransfer::CREDENTIALS_PROP => [
'login' => 'theodor.storm',
'token' => 'Der Schimmelreiter',
],
]);
var_dump($advancedCustomerTransfer->toArray());