MVC: support inheritance of the ArrayField type, institutionalise the type by adding a method isArrayType() to the basefield, which returns true if the type itself is a ArrayField or one of its descendants.

Conceptually there are two types containers in our model, the standard one (ContainerField), which only acts as a placeholder without logic and the ArrayField type, which understands repetitive structures and comes with its own uuid reference per item and methods supporting addition and removal of entries.

With the application specific field types it can be practical if you could extend this container type as well, so you can add additional methods on a more logical spot in the code tree.

ref https://github.com/opnsense/plugins/issues/1720
This commit is contained in:
Ad Schellevis 2020-03-04 13:48:18 +01:00
parent 41cf191205
commit a8b2fb5e92
3 changed files with 14 additions and 8 deletions

View File

@ -28,7 +28,6 @@
namespace OPNsense\Base;
use OPNsense\Base\FieldTypes\ArrayField;
use OPNsense\Base\FieldTypes\ContainerField;
use OPNsense\Core\Config;
use Phalcon\Logger\Adapter\Syslog;
@ -221,7 +220,7 @@ abstract class BaseModel
$config_section_data = null;
}
if ($fieldObject instanceof ArrayField) {
if ($fieldObject->isArrayType()) {
// handle Array types, recurring items
if ($config_section_data != null && !empty((string)$config_section_data)) {
foreach ($config_section_data as $conf_section) {

View File

@ -53,10 +53,7 @@ class UniqueConstraint extends BaseConstraint
$parentNode = $node->getParentNode();
$level = 0;
// dive into parent
while (
$containerNode != null &&
get_class($containerNode) != 'OPNsense\Base\FieldTypes\ArrayField'
) {
while ($containerNode != null && !$containerNode->isArrayType()) {
$containerNode = $containerNode->getParentNode();
$level++;
}

View File

@ -122,6 +122,16 @@ abstract class BaseField
*/
private $internalParentModel = null;
/**
* @return bool
*/
public function isArrayType()
{
return is_a($this, "OPNsense\\Base\\FieldTypes\\ArrayField") ||
is_subclass_of($this, "OPNsense\\Base\\FieldTypes\\ArrayField");
}
/**
* generate a new UUID v4 number
* @return string uuid v4 number
@ -583,7 +593,7 @@ abstract class BaseField
}
// add new items to array type objects
if (get_class($this) == "OPNsense\\Base\\FieldTypes\\ArrayField") {
if ($this->isArrayType()) {
foreach ($data as $dataKey => $dataValue) {
if (!isset($this->__items[$dataKey])) {
$node = $this->add();
@ -600,7 +610,7 @@ abstract class BaseField
*/
public function addToXMLNode($node)
{
if ($this->internalReference == "" || get_class($this) == "OPNsense\\Base\\FieldTypes\\ArrayField") {
if ($this->internalReference == "" || $this->isArrayType()) {
// ignore tags without internal reference (root) and ArrayTypes
$subnode = $node;
} else {