Skip to content
Snippets Groups Projects
Commit 4d99b5cb authored by Alex Barth's avatar Alex Barth
Browse files

Add existing() method that allows for making sure that a configurable actually...

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.
parent 37b606f2
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,12 @@ ...@@ -6,6 +6,12 @@
* FeedsConfigurable and helper functions. * 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. * A configurable class.
*/ */
...@@ -68,6 +74,17 @@ abstract class FeedsConfigurable { ...@@ -68,6 +74,17 @@ abstract class FeedsConfigurable {
$this->disabled = FALSE; $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 * Save a configuration. Concrete extending classes must implement a save
* operation. * operation.
...@@ -85,12 +102,13 @@ abstract class FeedsConfigurable { ...@@ -85,12 +102,13 @@ abstract class FeedsConfigurable {
* Set configuration. * Set configuration.
* *
* @param $config * @param $config
* Array containing configuration information. Will be filtered by the keys * Array containing configuration information. Config array will be filtered
* returned by configDefaults(). * by the keys returned by configDefaults() and populated with default
* values that are not included in $config.
*/ */
public function setConfig($config) { public function setConfig($config) {
$default_keys = $this->configDefaults(); $defaults = $this->configDefaults();
$this->config = array_intersect_key($config, $default_keys); $this->config = array_intersect_key($config, $defaults) + $defaults;
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment