12 require_once(__DIR__ .
"/generator-commons.php");
27 define(
'TARGET_PATH', __DIR__ .
'/../src/DataType');
35 $shouldWriteToDisc = (isset($GLOBALS[
'argv'][1]) && $GLOBALS[
'argv'][1] ===
'--no-file-generation') ?
false :
true;
37 echo
"### GENERATING COMPLEX DATA TYPE CLASSES ###\n";
39 echo
"#############################################\n";
59 foreach (
$schema[
'objects'] as $obj_name => $params) {
67 $phpClass =
new PhpClass();
73 $required = $params[
'__required'];
74 unset($params[
'__required']);
80 $params =
reorderParams([
'params' => $params,
'required' => $required]);
84 $phpClass->setQualifiedName(
'\\Ethereum\\DataType\\' . $obj_name)
85 ->setParentClassName(
'EthDataType')
86 ->setDescription(array(
87 'Ethereum data type ' . $obj_name .
'.',
89 "Generated by $scriptName based on resources/ethjs-schema.json.",
92 ->setUseStatements($useStatements)
97 $methods[] = PhpMethod::create(
'__construct')
102 $methods[] = PhpMethod::create(
'getTypeArray')
103 ->setDescription(
'Returns a name => type array.')
109 $methods[] = PhpMethod::create(
'toArray')
110 ->setDescription(
'Returns array with values.')
115 $phpClass->setMethods($methods);
118 $generator =
new CodeGenerator([
119 'generateScalarTypeHints' => TRUE,
120 'generateReturnTypeHints' => TRUE,
121 'enableSorting' => FALSE,
123 $phpClassText = $generator->generate($phpClass);
125 #print $file_header . $phpClassText; 127 $destination_path =
TARGET_PATH .
'/' . ucfirst($obj_name) .
'.php';
128 echo
"generated file: $destination_path \n";
131 file_put_contents($destination_path ,
$file_header . $phpClassText);
134 echo
"File is not written to disc, because file generation is disabled by '--no-file-generation'\n";
154 $params[
'params'] = [];
156 foreach ($input[
'params'] as $name => $type) {
157 if (in_array($name, $input[
'required'])) {
158 $params[
'params'][$name] = $type;
162 foreach ($input[
'params'] as $name => $type) {
163 if (!in_array($name, $input[
'required'])) {
164 $params[
'params'][$name] = $type;
185 foreach ($input[
'params'] as $name => $type) {
187 $optionalValue =
false;
188 if (!is_array($type)) {
189 $type = EthD::typeMap($type);
190 if (!in_array($name, $input[
'required'])) {
191 $optionalValue =
true;
194 if (!in_array($name, $input[
'required'])) {
195 $optionalValue =
true;
197 $subtype = EthD::typeMap($type[0]) ? EthD::typeMap($type[0]) : $type[0];
199 $description =
'Array of ' . $subtype;
203 $tmp =
new PhpParameter($name);
204 $tmp->setType($type);
206 $tmp->setTypeDescription($description);
208 if ($optionalValue) {
209 $tmp->setValue(NULL);
229 foreach ($input[
'params'] as $name => $type) {
230 $p =
new PhpProperty($name);
231 $p->setDescription(
"Value for '$name'.");
251 foreach ($input[
'params'] as $name => $type) {
252 $properties .=
'$this->' . $name .
" = $$name; \n";
255 return substr($properties, 0, -2);
273 foreach ($input[
'params'] as $name => $type) {
276 $prop =
new PhpParameter(
'value');
277 $prop->setType(EthDataType::getTypeClass($type,
true));
280 $body =
'if (is_object($value) && is_a($value, \'' . EthDataType::getTypeClass($type,
true) .
"')) {\n";
281 $body .=
"\t" .
'$this->' . $name .
' = $value;' .
"\n";
283 $body .=
'else {' .
"\n";
284 $body .=
"\t" .
'$this->' . $name .
' = new ' . EthDataType::getTypeClass($type,
true) .
'($value);' .
"\n";
288 $tmp =
new PhpMethod(
'set' . ucfirst($name));
289 $tmp->addParameter($prop)
290 ->setDescription(
"Setter for '$name'.")
311 $data[] =
"return array( ";
312 foreach ($input[
'params'] as $name => $type) {
313 if (is_array($type)) {
314 $data[] =
"\t'" . $name .
"' => '[" . EthD::typeMap($type[0]) .
"]',";
316 $data[] =
"\t'" . $name .
"' => '" . EthD::typeMap($type) .
"',";
320 return implode(
"\n", $data);
334 $return =
'$return = [];' .
"\n";
336 foreach ($input[
'params'] as $name => $type) {
338 if (is_array($type)) {
339 $return .=
'(!is_null($this->' 343 .
'] = Ethereum::valueArray($this->' 346 . EthDataType::getTypeClass($type[0])
347 .
"') : array(); \n";
349 $return .=
'(!is_null($this->' 355 .
"->hexVal() : NULL; \n";
360 $return .=
'return $return;';
printMe($title, $content=null)
makeSetFunctions(array $input)
makeTypeArrayBody(Array $input)
foreach($schema['objects'] as $obj_name=> $params) reorderParams(Array $input)
makeConstructorContent(Array $input)
makeToArrayBody(Array $input)
makeProperties(Array $input)
makeConstructorParams(Array &$input)