From a8b2fb5e92eaba22634ea6a56fc859bb90db728e Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 4 Mar 2020 13:48:18 +0100 Subject: [PATCH] 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 --- .../mvc/app/models/OPNsense/Base/BaseModel.php | 3 +-- .../OPNsense/Base/Constraints/UniqueConstraint.php | 5 +---- .../models/OPNsense/Base/FieldTypes/BaseField.php | 14 ++++++++++++-- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php b/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php index a28046202..529b16f02 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/BaseModel.php @@ -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) { diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php b/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php index a7879dc51..c35887f78 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/Constraints/UniqueConstraint.php @@ -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++; } diff --git a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php index 0f9f121e1..1a1bdfeef 100644 --- a/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php +++ b/src/opnsense/mvc/app/models/OPNsense/Base/FieldTypes/BaseField.php @@ -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 {