StorageBase.php 976 Bytes
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\KeyValueStore;

5 6 7 8
/**
 * Provides a base class for key/value storage implementations.
 */
abstract class StorageBase implements KeyValueStoreInterface {
9 10 11 12 13 14 15 16 17

  /**
   * The name of the collection holding key and value pairs.
   *
   * @var string
   */
  protected $collection;

  /**
18
   * {@inheritdoc}
19
   */
20
  public function __construct($collection) {
21 22 23 24
    $this->collection = $collection;
  }

  /**
25
   * {@inheritdoc}
26 27 28 29 30 31
   */
  public function getCollectionName() {
    return $this->collection;
  }

  /**
32
   * {@inheritdoc}
33
   */
34
  public function get($key, $default = NULL) {
35
    $values = $this->getMultiple([$key]);
36
    return isset($values[$key]) ? $values[$key] : $default;
37 38 39
  }

  /**
40
   * {@inheritdoc}
41 42 43 44 45 46 47 48
   */
  public function setMultiple(array $data) {
    foreach ($data as $key => $value) {
      $this->set($key, $value);
    }
  }

  /**
49
   * {@inheritdoc}
50 51
   */
  public function delete($key) {
52
    $this->deleteMultiple([$key]);
53 54 55
  }

}