From 4d99b5cb91fcdaf16c4b2dcf5aa44d3cbd047f52 Mon Sep 17 00:00:00 2001
From: Alex Barth <alex_b@53995.no-reply.drupal.org>
Date: Fri, 19 Feb 2010 23:11:06 +0000
Subject: [PATCH] Add existing() method that allows for making sure that a
 configurable actually exists when executing a method on it. Usage: try {  
 ->existing()->action(); } catch(FeedsNotExistingException ) {}

Fix notices from setConfig() by making sure that all default values are present.
---
 includes/FeedsConfigurable.inc | 26 ++++++++++++++++++++++----
 1 file changed, 22 insertions(+), 4 deletions(-)

diff --git a/includes/FeedsConfigurable.inc b/includes/FeedsConfigurable.inc
index 8f399f10..35bf1720 100644
--- a/includes/FeedsConfigurable.inc
+++ b/includes/FeedsConfigurable.inc
@@ -6,6 +6,12 @@
  * FeedsConfigurable and helper functions.
  */
 
+/**
+ * Used when an object does not exist in the DB or code but should.
+ */
+class FeedsNotExistingException extends Exception {
+}
+
 /**
  * A configurable class.
  */
@@ -68,6 +74,17 @@ abstract class FeedsConfigurable {
     $this->disabled = FALSE;
   }
 
+  /**
+   * Determine whether this object is persistent. I. e. it is defined either
+   * in code or in the database.
+   */
+  public function existing() {
+    if ($this->export_type == FEEDS_EXPORT_NONE) {
+      throw new FeedsNotExistingException(t('Object is not persistent.'));
+    }
+    return $this;
+  }
+
   /**
    * Save a configuration. Concrete extending classes must implement a save
    * operation.
@@ -85,12 +102,13 @@ abstract class FeedsConfigurable {
    * Set configuration.
    *
    * @param $config
-   *   Array containing configuration information. Will be filtered by the keys
-   *   returned by configDefaults().
+   *   Array containing configuration information. Config array will be filtered
+   *   by the keys returned by configDefaults() and populated with default
+   *   values that are not included in $config.
    */
   public function setConfig($config) {
-    $default_keys = $this->configDefaults();
-    $this->config = array_intersect_key($config, $default_keys);
+    $defaults = $this->configDefaults();
+    $this->config = array_intersect_key($config, $defaults) + $defaults;
   }
 
   /**
-- 
GitLab