forked from digitaldonkey/ethereum-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_datatypes.php
243 lines (209 loc) · 6.27 KB
/
make_datatypes.php
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
<?php
/**
* This is used to create consistent data type classes.
*
* Disabled, because only required for development.
*/
header("HTTP/1.1 401 Unauthorized");
die ('Access denied');
define('TAGETPATH', './src');
require_once './includes.inc.php';
use Ethereum\EthDataTypePrimitive;
use Ethereum\EthDataType;
foreach ($schema['objects'] as $obj_name => $params) {
echo "<h3>" . $obj_name . "</h3>";
$required = $params['__required'];
unset($params['__required']);
$ordered_params = reorderParams(array('params' => $params, 'required' => $required));
$constructor = makeConstructor($ordered_params);
$constructor_content = makeConstructorContent($ordered_params);
$setters = makeSetFunctions($ordered_params);
$return_array = makeReturnArray($ordered_params);
$properties = makeProperties($ordered_params);
printMe('Arguments', $ordered_params['params']);
printMe('Required', $required);
printMe('Properties', $properties);
printMe('Constructor', "__construct(" . $constructor . ")");
printMe('ConstructorContent', $constructor_content);
printMe('Set<PROPERTY>', $setters);
printMe('Return Array', $return_array);
printMe('Type Array', makeTypeArray($ordered_params));
$data = array(
"<?php\n",
"namespace Ethereum;",
"",
"/**",
" * Implement data type $obj_name.",
" */",
"class $obj_name extends EthDataType {",
"",
$properties,
" /**",
" * Constructor.",
" */",
" public function __construct($constructor) {",
$constructor_content,
" }",
"",
$setters,
"",
" public function getType() {",
" return '$obj_name';",
" }",
"",
" public function toArray() {",
$return_array,
" }",
makeTypeArray($ordered_params),
"}",
);
file_put_contents(TAGETPATH . '/' . ucfirst($obj_name) . '.php', implode("\n", $data));
echo "<hr />";
}
/**
* Create set_<PROPERTY> functions content.
*
* @param array $input
* ['params' => ['name'=> Type, 'name'=> Type ...],
* 'required' => ['name', 'name' ...] ]
*/
function makeSetFunctions(array $input) {
$functions = '';
// Required params first.
foreach ($input['params'] as $name => $type) {
$functions .= ' public function set' . ucfirst($name) . '(' . EthDataType::getTypeClass($type, TRUE) . ' $value){' . "\n";
$functions .= ' if (is_object($value) && is_a($value, \'' . EthDataType::getTypeClass($type, TRUE) . "')) {\n";
$functions .= ' $this->' . $name . ' = $value;' . "\n";
$functions .= ' }' . "\n";
$functions .= ' else {' . "\n";
$functions .= ' $this->' . $name . ' = new ' . EthDataType::getTypeClass($type, TRUE) . '($value);' . "\n";
$functions .= ' }' . "\n";
$functions .= " }\n\n";
}
return $functions;
}
/**
* Create return array.
*
* @param Array $input -
* ['params' => ['name'=> Type, 'name'=> Type ...],
* 'required' => ['name', 'name' ...] ]
*/
function makeReturnArray(Array $input) {
$array = ' $return = array();' . "\n";
// Required params first.
foreach ($input['params'] as $name => $type) {
if (is_array($type)) {
$array .= ' (!is_null($this->' . $name . ')) ? $return[' . "'$name'" . '] = EthereumStatic::valueArray($this->' . $name . ", '" . $type[0] . "') : array(); \n";
}
else {
$array .= ' (!is_null($this->' . $name .')) ? $return[' . "'$name'" . '] = $this->' . $name . "->hexVal() : NULL; \n";
}
}
$array .= ' return $return;';
return $array;
}
/**
* Create constructor content.
*
* @param Array $input -
* ['params' => ['name'=> Type, 'name'=> Type ...],
* 'required' => ['name', 'name' ...] ]
*/
function makeConstructorContent(Array $input) {
$properties = '';
// Required params first.
foreach ($input['params'] as $name => $type) {
$properties .= ' $this->' . $name . " = $$name; \n";
}
return substr($properties, 0, -2);
}
/**
* Create Constructor from array.
*
* @param Array $input
* ['params' => ['name'=> Type, 'name'=> Type ...],
* 'required' => ['name', 'name' ...] ]
*/
function makeTypeArray(Array $input) {
$data[] = " /**";
$data[] = " * Returns a name => type array.";
$data[] = " */";
$data[] = ' public static function getTypeArray() {';
$data[] = ' return array( ';
foreach ($input['params'] as $name => $type) {
if (is_array($type)) {
$data[] = " '" . $name . "' => '" . EthDataTypePrimitive::typeMap($type[0]) . "',";
}
else {
$data[] = " '" . $name . "' => '" . EthDataTypePrimitive::typeMap($type) . "',";
}
}
$data[] = ' );';
$data[] = ' }';
return implode("\n", $data);
}
/**
* Create properties.
*
* @param Array $input -
* ['params' => ['name'=> Type, 'name'=> Type ...],
* 'required' => ['name', 'name' ...] ]
*/
function makeProperties(Array $input) {
$properties = '';
// Required params first.
foreach ($input['params'] as $name => $type) {
$properties .= ' protected $' . $name . ';' . "\n";
}
return $properties;
}
/**
* Create constructor parameters.
*
* @param Array $input -
* ['params' => ['name'=> Type, 'name'=> Type ...],
* 'required' => ['name', 'name' ...] ]
*/
function makeConstructor(Array $input) {
$constructor = '';
// Required params first.
foreach ($input['params'] as $name => $type) {
if (!is_array($type)) {
$constructor .= EthDataTypePrimitive::typeMap($type) . ' $' . $name;
if (!in_array($name, $input['required'])) {
$constructor .= ' = NULL';
}
}
else {
$constructor .= 'array ' . ' $' . $name;
if (!in_array($name, $input['required'])) {
$constructor .= ' = NULL';
}
}
$constructor .= ', ';
}
return substr($constructor, 0, -2);
}
/**
* Reorder parameters.
*
* Prioritizing required params over unrequired ones.
*/
function reorderParams(Array $input) {
$ordered_params = $input;
$ordered_params['params'] = [];
// Required params first.
foreach ($input['params'] as $name => $type) {
if (in_array($name, $input['required'])) {
$ordered_params['params'][$name] = $type;
}
}
// ... then non required params.
foreach ($input['params'] as $name => $type) {
if (!in_array($name, $input['required'])) {
$ordered_params['params'][$name] = $type;
}
}
return $ordered_params;
}