DatabaseStorageSortedBase.php in Key-value Extensions 8
File
src/KeyValueStore/DatabaseStorageSortedBase.php
View source
<?php
namespace Drupal\key_value\KeyValueStore;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Database\Connection;
abstract class DatabaseStorageSortedBase implements KeyValueStoreSortedInterface {
protected $collection;
protected $serializer;
protected $connection;
protected $table;
public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_sorted') {
$this->collection = $collection;
$this->serializer = $serializer;
$this->connection = $connection;
$this->table = $table;
}
public function getCount() {
return $this->connection
->select($this->table, 't')
->condition('collection', $this->collection)
->countQuery()
->execute()
->fetchField();
}
public function getRange($start, $stop = NULL, $inclusive = TRUE) {
$query = $this->connection
->select($this->table, 't')
->fields('t', [
'value',
])
->condition('collection', $this->collection)
->condition('name', $start, $inclusive ? '>=' : '>');
if ($stop !== NULL) {
$query
->condition('name', $stop, $inclusive ? '<=' : '<');
}
$result = $query
->orderBy('name', 'ASC')
->execute();
$values = [];
foreach ($result as $item) {
$values[] = $this->serializer
->decode($item->value);
}
return $values;
}
}