mirror of
https://github.com/lucaspalomodevelop/core.git
synced 2026-03-16 17:44:41 +00:00
add setters and getters to map json like content to model
This commit is contained in:
parent
a7644ef6db
commit
95d3af241d
@ -232,6 +232,24 @@ abstract class BaseModel
|
||||
return $this->internalData->getFlatNodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* get nodes as array structure
|
||||
* @return array
|
||||
*/
|
||||
public function getNodes()
|
||||
{
|
||||
return $this->internalData->getNodes();
|
||||
}
|
||||
|
||||
/**
|
||||
* structured setter for model
|
||||
* @param array|$data named array
|
||||
*/
|
||||
public function setNodes($data)
|
||||
{
|
||||
return $this->internalData->setNodes($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* validate full model using all fields and data in a single (1 deep) array
|
||||
*/
|
||||
|
||||
@ -281,6 +281,64 @@ abstract class BaseField
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get nodes as array structure
|
||||
* @return array
|
||||
*/
|
||||
public function getNodes()
|
||||
{
|
||||
$result = array ();
|
||||
foreach ($this->__items as $key => $node) {
|
||||
if ($node->isContainer()) {
|
||||
$result[$key] = $node->getNodes();
|
||||
} else {
|
||||
$result[$key] = $node->__toString();
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* update model with data returning missing repeating tag types.
|
||||
* @param $data named array structure containing new model content
|
||||
* @return array missing array keys in data
|
||||
*/
|
||||
public function setNodes($data)
|
||||
{
|
||||
$delItems = array();
|
||||
|
||||
// update structure with new content
|
||||
foreach ($this->__items as $key => $node) {
|
||||
if ($data != null && array_key_exists($key, $data)) {
|
||||
if ($node->isContainer()) {
|
||||
$delItems += $node->setNodes($data[$key]);
|
||||
} else {
|
||||
$node->setValue($data[$key]);
|
||||
}
|
||||
} elseif (get_class($this) == "OPNsense\\Base\\FieldTypes\\ArrayField") {
|
||||
// mark item as missing in input, remove later
|
||||
$delItems[] = array("node" => $this, "key" => $key );
|
||||
} else {
|
||||
$delItems += $node->setNodes(array());
|
||||
}
|
||||
}
|
||||
|
||||
// add new items to array type objects
|
||||
if (get_class($this) == "OPNsense\\Base\\FieldTypes\\ArrayField") {
|
||||
foreach ($data as $dataKey => $dataValue) {
|
||||
if (!array_key_exists($dataKey, $this->__items)) {
|
||||
$node = $this->add();
|
||||
$delItems += $node->setNodes($dataValue);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $delItems;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add this node and it's children to the supplied simplexml node pointer.
|
||||
* @param \SimpleXMLElement $node target node
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user