diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 9ae550b..e425555 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -90,9 +90,9 @@ function _add_twig_support(\Twig\Environment $twig) } )); $twig->addFunction(new \Twig\TwigFunction('value2data', - function($val, \mtgType $type, $data, $tokens = array()) + function($val, $name, \mtgType $type, $data, $tokens = array()) { - return value2data($val, $type, $data, $tokens); + return value2data($val, $name, $type, $data, $tokens); } )); $twig->addFunction(new \Twig\TwigFunction('data_normalize', @@ -261,24 +261,24 @@ function data2value(string $val, \mtgType $type, string $data, array $tokens, bo } //TODO: move it to template -function value2data(string $val, \mtgType $type, string $data, $tokens = array()) +function value2data(string $val, string $name, \mtgType $type, string $data, $tokens = array(), $assoc = '$assoc') { $str = ''; if($type instanceof \mtgBuiltinType) { - $str .= "{$data}[] = $val;\n"; + $str .= "\metagen_php\array_set_value({$data}, {$assoc}, '$name', $val);\n"; } else if($type instanceof \mtgMetaStruct) { if(array_key_exists('virtual', $tokens)) - $str .= "{$data}[] = {$val}->export(true/*virtual*/);\n"; + $str .= "\metagen_php\array_set_value({$data}, {$assoc}, '$name', {$val}->export(\$assoc, true/*virtual*/));\n"; else - $str .= "{$data}[] = {$val}->export();\n"; + $str .= "\metagen_php\array_set_value({$data}, {$assoc}, '$name', {$val}->export(\$assoc));\n"; } else if($type instanceof \mtgMetaEnum) { - $str .= "{$data}[] = $val;\n"; + $str .= "\metagen_php\array_set_value({$data}, {$assoc}, '$name', $val);\n"; } else if($type instanceof \mtgArrType) { @@ -289,9 +289,9 @@ function value2data(string $val, \mtgType $type, string $data, $tokens = array() $str .= "else\n"; $str .= " foreach({$val} as \$arr_tmp_item__)\n"; $str .= " {\n"; - $str .= value2data("\$arr_tmp_item__", $type->getValue(), "\$arr_tmp__", $tokens) . "\n"; + $str .= value2data("\$arr_tmp_item__", '', $type->getValue(), "\$arr_tmp__", $tokens, 'false') . "\n"; $str .= " }\n"; - $str .= "{$data}[] = \$arr_tmp__;\n"; + $str .= "\metagen_php\array_set_value({$data}, \$assoc, '$name', \$arr_tmp__);\n"; } else throw new Exception("Unknown type '{$type->getName()}'"); diff --git a/tpl/macro.twig b/tpl/macro.twig index 2028607..2ed6d21 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -58,9 +58,15 @@ class {{o.name}} {{o.parent ? 'extends ' ~ o.parent.name}} return self::CLASS_ID; } - function import(array &$data) + function import(array &$data, bool $assoc = false) { - $this->_import($data, true); + if($assoc) + { + self::normalize($data, $normalized); + $this->_import($normalized, true); + } + else + $this->_import($data, true); } function _import(array &$data, $root = true) : int @@ -86,14 +92,14 @@ class {{o.name}} {{o.parent ? 'extends ' ~ o.parent.name}} return $IDX; } - function export($virtual = false) : array + function export(bool $assoc = false, bool $virtual = false) : array { $data = array(); - $this->_export($data, $virtual); + $this->_export($data, $assoc, $virtual); return $data; } - function _export(array &$data, $virtual = false) + function _export(array &$data, bool $assoc = false, $virtual = false) { if($virtual) $data[] = $this->getClassId(); @@ -257,10 +263,10 @@ array_merge(parent::CLASS_FIELDS_PROPS(), {% macro export_fields(o) %} {%- if o.parent -%} - parent::_export($data, false); + parent::_export($data, $assoc, false); {%- endif -%} {%- for f in o.fields ~%} - {{value2data('$this->'~f.name, f.type, '$data', f.tokens)}} + {{value2data('$this->'~f.name, f.name, f.type, '$data', f.tokens)}} {%- endfor -%} {% endmacro %}