Commit 4a3ee315 authored by alexpott's avatar alexpott

Issue #2085867 by damiankloip: Add a default parameter to keyvalue get() method.

parent 2977e2ab
......@@ -25,11 +25,13 @@ public function getCollectionName();
*
* @param string $key
* The key of the data to retrieve.
* @param mixed $default
* The default value to use if the key is not found.
*
* @return mixed
* The stored value, or NULL if no value exists.
* The stored value, or the default value if no value exists.
*/
public function get($key);
public function get($key, $default = NULL);
/**
* Returns the stored key/value pairs for a given set of keys.
......
......@@ -22,8 +22,8 @@ class MemoryStorage extends StorageBase {
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get().
*/
public function get($key) {
return array_key_exists($key, $this->data) ? $this->data[$key] : NULL;
public function get($key, $default = NULL) {
return array_key_exists($key, $this->data) ? $this->data[$key] : $default;
}
/**
......
......@@ -36,7 +36,7 @@ public function __construct($collection) {
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get().
*/
public function get($key) {
public function get($key, $default = NULL) {
return NULL;
}
......
......@@ -36,9 +36,9 @@ public function getCollectionName() {
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::get().
*/
public function get($key) {
public function get($key, $default = NULL) {
$values = $this->getMultiple(array($key));
return isset($values[$key]) ? $values[$key] : NULL;
return isset($values[$key]) ? $values[$key] : $default;
}
/**
......
......@@ -157,6 +157,9 @@ public function testNonExistingKeys() {
// Verify that a non-existing key returns NULL as value.
$this->assertNull($stores[0]->get('foo'));
// Verify that a non-existing key with a default returns the default.
$this->assertIdentical($stores[0]->get('foo', 'bar'), 'bar');
// Verify that a FALSE value can be stored.
$stores[0]->set('foo', FALSE);
$this->assertIdentical($stores[0]->get('foo'), FALSE);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment