Skip to content

Commit

Permalink
MVC/Model/ArrayField - add generic static record definition.
Browse files Browse the repository at this point in the history
While converting legacy code we do seem to have multiple static entries in different models, in which case we tend to overwrite ArrayField and extend the output with the static entries that are required for the model. These can either be defined in the legacy code (SPDField) or defined as static options (like the bogons in AliasField).

This commit adds a method getStaticChildren() which the derived class may overwrite in order to spawn new items to the list which can not be persisted. This should help avoid duplication of similar code constructs in various migrated areas. These items are constructed during the "post loading" event.
  • Loading branch information
AdSchellevis committed Jul 3, 2023
1 parent 1c05a19 commit 3408cbf
Showing 1 changed file with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ class ArrayField extends BaseField
*/
private $internalTemplateNode = null;

/**
* @var list statically defined children, key value store for static defined model entries
*/
private static $internalStaticChildren = [];

/**
* @return key value store of static model items, overwrite when needed
*/
protected static function getStaticChildren()
{
return [];
}

/**
* Copy first node pointer as template node to make sure we always have a template to create new nodes from.
* If the first node is virtual (no source data), remove that from the list.
Expand All @@ -56,6 +69,22 @@ protected function actionPostLoadingEvent()
unset($this->internalChildnodes[$firstKey]);
}
}
// init static entries when returned by getStaticChildren()
foreach (static::getStaticChildren() as $skey => $payload) {
$container_node = $this->newContainerField($this->__reference . "." . $skey, $this->internalXMLTagName);
$container_node->setAttributeValue("uuid", $skey);
$container_node->setInternalIsVirtual();
foreach ($this->getTemplateNode()->iterateItems() as $key => $value) {
$node = clone $value;
$node->setInternalReference($container_node->__reference . "." . $key);
if (isset($payload[$key])) {
$node->setValue($payload[$key]);
}
$node->markUnchanged();
$container_node->addChildNode($key, $node);
}
self::$internalStaticChildren[$skey] = $container_node;
}
}

/**
Expand Down Expand Up @@ -181,4 +210,42 @@ public function sortedBy($fieldNames, $descending = false, $sort_flags = SORT_NA

return array_values($sortedData);
}

/**
* @inheritdoc
*/
public function hasChild($name)
{
if (isset(self::$internalStaticChildren[$name])) {
return true;
} else {
return parent::hasChild($name);
}
}

/**
* @inheritdoc
*/
public function getChild($name)
{
if (isset(self::$internalStaticChildren[$name])) {
return self::$internalStaticChildren[$name];
} else {
return parent::getChild($name);
}
}

/**
* @inheritdoc
*/
public function iterateItems()
{
foreach (parent::iterateItems() as $key => $value) {
yield $key => $value;
}
foreach (self::$internalStaticChildren as $key => $node) {
yield $key => $node;
}
}

}

0 comments on commit 3408cbf

Please sign in to comment.