diff --git a/src/codegen.inc.php b/src/codegen.inc.php index 22fc6fa..9ae550b 100644 --- a/src/codegen.inc.php +++ b/src/codegen.inc.php @@ -353,13 +353,13 @@ function data_normalize($name, \mtgType $type, $buf, $dst, $tokens = array(), $t $str .= "\$vclass__ = AutogenBundle::getClassName(\$vclass_id__);\n"; $str .= "if(!is_a(\$vclass__, '{$type->getName()}', true)) throw new Exception(\"'\$vclass__' is not subclass of '{$type->getName()}'\");\n"; $str .= "\$tmp_sub_mapped__ = array(\$vclass_id__);\n"; - $str .= "call_user_func_array([\$vclass__, 'normalize'], array(&\$tmp_sub_arr__, &\$tmp_sub_mapped__));\n"; + $str .= "call_user_func_array([\$vclass__, 'normalize'], array(&\$tmp_sub_arr__, &\$tmp_sub_mapped__, true, \$check_junk));\n"; $str .= "{$dst}[] = \$tmp_sub_mapped__;\n"; } else { $str .= "\$tmp_sub_mapped__ = array();\n"; - $str .= "{$type}::normalize(\$tmp_sub_arr__, \$tmp_sub_mapped__);\n"; + $str .= "{$type}::normalize(\$tmp_sub_arr__, \$tmp_sub_mapped__, true, \$check_junk);\n"; $str .= "{$dst}[] = \$tmp_sub_mapped__;\n"; } $str .= "}"; diff --git a/src/data_utils.inc.php b/src/data_utils.inc.php index 4c12c66..403b454 100644 --- a/src/data_utils.inc.php +++ b/src/data_utils.inc.php @@ -222,3 +222,11 @@ function val_blob($val) return $val; } + +function check_junk_fields(array $data, array $FIELDS) +{ + unset($data['$id']); + $diff = array_diff(array_keys($data), $FIELDS); + if($diff) + throw new Exception("Junk fields: " . json_encode(array_values($diff))); +} diff --git a/tpl/macro.twig b/tpl/macro.twig index f8bd38c..903bacb 100644 --- a/tpl/macro.twig +++ b/tpl/macro.twig @@ -82,6 +82,7 @@ class {{o.name}} {{o.parent ? 'extends ' ~ o.parent.name}} $FIELDS = self::CLASS_FIELDS(); throw new Exception($e->getMessage() . " < {$FIELDS[$IDX]} < ({{o.name}})"); } + return $IDX; } @@ -100,7 +101,7 @@ class {{o.name}} {{o.parent ? 'extends ' ~ o.parent.name}} {{_self.export_fields(o)}} } - static function normalize(array &$data, array &$mapped, bool $root = true) : int + static function normalize(array &$data, array &$mapped, bool $root = true, bool $check_junk = false) : int { $IDX = -1; @@ -108,7 +109,7 @@ class {{o.name}} {{o.parent ? 'extends ' ~ o.parent.name}} { $IDX = 0; {%- if o.parent ~%} - $IDX = parent::normalize($data, $mapped, false); + $IDX = parent::normalize($data, $mapped, false, false); {%- endif ~%} {%- for f in o.fields ~%} @@ -122,6 +123,9 @@ class {{o.name}} {{o.parent ? 'extends ' ~ o.parent.name}} throw new Exception($e->getMessage() . " < {$FIELDS[$IDX]} < ({{o.name}})"); } + if($root && $check_junk) + \metagen_php\check_junk_fields($data, self::CLASS_FIELDS()); + return $IDX; } }