diff --git a/metagen.inc.php b/metagen.inc.php index 0188846..6e05614 100644 --- a/metagen.inc.php +++ b/metagen.inc.php @@ -192,8 +192,11 @@ class mtgMetaStruct extends mtgUserType return $this->fields; } - function setFields(array $fields) + function setFields(array $fields, $replace = false) { + if($replace) + $this->fields = array(); + foreach($fields as $field) $this->addField($field); } @@ -206,7 +209,6 @@ class mtgMetaStruct extends mtgUserType $this->fields[$field->getName()] = $field; } - //TODO: doesn't really belong here function delField(mtgMetaField $field) { if(isset($this->fields[$field->getName()])) @@ -225,20 +227,6 @@ class mtgMetaStruct extends mtgUserType return $this->fields[$name]; } - //TODO: doesn't really belong here - function findFieldOwner($name, mtgMetaInfo $meta) - { - $tmp = $this; - while($tmp) - { - $fields = $tmp->getFields(); - if(isset($fields[$name])) - return $tmp; - $tmp = $tmp->getParent(); - } - return null; - } - function getFuncs() { return $this->funcs; @@ -264,17 +252,10 @@ class mtgMetaStruct extends mtgUserType return $this->funcs[$name]; } + //TODO: doesn't belong here function hasTokenInParent($name) { - $parent = $this; - while($parent) - { - if($parent->hasToken($name)) - return true; - $parent = $parent->getParent(); - } - - return false; + return mtg_try_get_token_in_hierarchy($this, $name, $value); } } @@ -737,3 +718,32 @@ function mtg_get_all_fields(mtgMetaStruct $struct) $fields = array_merge(mtg_get_all_fields($parent), $fields); return $fields; } + +function mtg_find_field_owner(mtgMetaStruct $struct, $name) +{ + $tmp = $struct; + while($tmp) + { + $fields = $tmp->getFields(); + if(isset($fields[$name])) + return $tmp; + $tmp = $tmp->getParent(); + } + return null; +} + +function mtg_try_get_token_in_hierarchy(mtgMetaStruct $struct, $name, &$value) +{ + $tmp = $struct; + while($tmp) + { + if($tmp->hasToken($name)) + { + $value = $tmp->getToken($name); + return true; + } + $tmp = $tmp->getParent(); + } + + return false; +}