From 9a2de3adcd917b49c9c5fa33095ccbc3a7032e9f Mon Sep 17 00:00:00 2001 From: Roland Haeder Date: Sat, 18 Apr 2015 03:52:56 +0200 Subject: [PATCH] Introduced isFieldSet(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../interfaces/class_FrameworkInterface.php | 9 +++++ .../main/class_BaseFrameworkSystem.php | 37 +++++++++++++++++-- inc/classes/main/helper/class_BaseHelper.php | 21 +++++++---- inc/config/class_FrameworkConfiguration.php | 11 ++++++ 4 files changed, 68 insertions(+), 10 deletions(-) diff --git a/inc/classes/interfaces/class_FrameworkInterface.php b/inc/classes/interfaces/class_FrameworkInterface.php index 123f9c9f..724e57d7 100644 --- a/inc/classes/interfaces/class_FrameworkInterface.php +++ b/inc/classes/interfaces/class_FrameworkInterface.php @@ -33,6 +33,15 @@ interface FrameworkInterface { */ function getField ($fieldName); + /** + * Checks if given field is set + * + * @param $fieldName Field name to check + * @return $isSet Whether the given field name is set + * @throws NullPointerException If the result instance is null + */ + function isFieldSet ($fieldName); + /** * Checks whether an object equals this object. You should overwrite this * method to implement own equality checks diff --git a/inc/classes/main/class_BaseFrameworkSystem.php b/inc/classes/main/class_BaseFrameworkSystem.php index 94c13b75..2e42a794 100644 --- a/inc/classes/main/class_BaseFrameworkSystem.php +++ b/inc/classes/main/class_BaseFrameworkSystem.php @@ -1997,12 +1997,12 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.':
'.print_r($fieldArray, TRUE).'
'); // Convert dashes to underscore - $fieldName = $this->convertDashesToUnderscores($fieldName); + $fieldName2 = $this->convertDashesToUnderscores($fieldName); // Does the field exist? - if (isset($fieldArray[$fieldName])) { + if ($this->isFieldSet($fieldName)) { // Get it - $fieldValue = $fieldArray[$fieldName]; + $fieldValue = $fieldArray[$fieldName2]; } elseif (defined('DEVELOPER')) { // Missing field entry, may require debugging self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']:fieldArray
=' . print_r($fieldArray, TRUE) . '
,fieldName=' . $fieldName . ' not found!'); @@ -2015,6 +2015,37 @@ class BaseFrameworkSystem extends stdClass implements FrameworkInterface { return $fieldValue; } + /** + * Checks if given field is set + * + * @param $fieldName Field name to check + * @return $isSet Whether the given field name is set + * @throws NullPointerException If the result instance is null + */ + public function isFieldSet ($fieldName) { + // Get result instance + $resultInstance = $this->getResultInstance(); + + // Is this instance null? + if (is_null($resultInstance)) { + // Then the user instance is no longer valid (expired cookies?) + throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); + } // END - if + + // Get current array + $fieldArray = $resultInstance->current(); + //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . $this->__toString() . ':' . __LINE__ . '] fieldName=' . $fieldName . ',fieldArray=
'.print_r($fieldArray, TRUE).'
'); + + // Convert dashes to underscore + $fieldName = $this->convertDashesToUnderscores($fieldName); + + // Determine it + $isSet = isset($fieldArray[$fieldName]); + + // Return result + return $isSet; + } + /** * Flushs all pending updates to the database layer * diff --git a/inc/classes/main/helper/class_BaseHelper.php b/inc/classes/main/helper/class_BaseHelper.php index 45b74bb3..f0d436f0 100644 --- a/inc/classes/main/helper/class_BaseHelper.php +++ b/inc/classes/main/helper/class_BaseHelper.php @@ -455,22 +455,29 @@ class BaseHelper extends BaseFrameworkSystem { * @throws NullPointerException Thrown if $valueInstance is null */ public function getValueField ($fieldName) { + // Init value + $fieldValue = NULL; + // The $valueInstance attribute should not be null! if (is_null($this->getValueInstance())) { // Throws an exception here throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER); } // END - if - // Get the field value - $fieldValue = $this->getValueInstance()->getField($fieldName); - //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - BEFORE!'); - - // Is it null? - if ((is_null($fieldValue)) && (!is_null($this->extraInstance))) { + // Is the field set? + if ($this->getValueInstance()->isFieldSet($fieldName)) { + // Get the field value + $fieldValue = $this->getValueInstance()->getField($fieldName); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Value instance!'); + } elseif ((!is_null($this->extraInstance)) && ($this->extraInstance->isFieldSet($fieldName))) { // So try the extra instance $fieldValue = $this->extraInstance->getField($fieldName); + //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - Extra instance!'); + } else { + // Field is not set + if ((!is_null($this->extraInstance)) && ($fieldName == 'city_name')) die('
'.print_r($this->extraInstance, TRUE).'
'); + $this->debugOutput('[' . __METHOD__ . ':' . __LINE__ . '] fieldName=' . $fieldName . ' is not set! - @TODO'); } // END - if - //* DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput($fieldName.'[]='.gettype($fieldValue).'('.strlen($fieldValue).') - AFTER!'); // Return it return $fieldValue; diff --git a/inc/config/class_FrameworkConfiguration.php b/inc/config/class_FrameworkConfiguration.php index a49d4aa9..8d786742 100644 --- a/inc/config/class_FrameworkConfiguration.php +++ b/inc/config/class_FrameworkConfiguration.php @@ -361,6 +361,17 @@ class FrameworkConfiguration implements Registerable { // Our super interface "FrameworkInterface" requires this } + /** + * Checks if given field is set + * + * @param $fieldName Field name to check + * @return $isSet Whether the given field name is set + * @throws NullPointerException If the result instance is null + */ + public function isFieldSet ($fieldName) { + // Our super interface "FrameworkInterface" requires this + } + /** * Generates a code for hashes from this class * -- 2.30.2