View source
<?php
namespace Drupal\Core\Entity\Query;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Utility\TableSort;
abstract class QueryBase implements QueryInterface {
protected $entityTypeId;
protected $entityType;
protected $sort = [];
protected $count = FALSE;
protected $condition;
protected $aggregate = [];
protected $groupBy = [];
protected $conditionAggregate;
protected $sortAggregate = [];
protected $range = [];
protected $alterMetaData;
protected $alterTags;
protected $accessCheck;
protected $allRevisions = FALSE;
protected $latestRevision = FALSE;
protected $pager = [];
protected $namespaces = [];
public function __construct(EntityTypeInterface $entity_type, $conjunction, array $namespaces) {
$this->entityTypeId = $entity_type
->id();
$this->entityType = $entity_type;
$this->conjunction = $conjunction;
$this->namespaces = $namespaces;
$this->condition = $this
->conditionGroupFactory($conjunction);
if ($this instanceof QueryAggregateInterface) {
$this->conditionAggregate = $this
->conditionAggregateGroupFactory($conjunction);
}
}
public function getEntityTypeId() {
return $this->entityTypeId;
}
public function condition($property, $value = NULL, $operator = NULL, $langcode = NULL) {
$this->condition
->condition($property, $value, $operator, $langcode);
return $this;
}
public function exists($property, $langcode = NULL) {
$this->condition
->exists($property, $langcode);
return $this;
}
public function notExists($property, $langcode = NULL) {
$this->condition
->notExists($property, $langcode);
return $this;
}
public function range($start = NULL, $length = NULL) {
$this->range = [
'start' => $start,
'length' => $length,
];
return $this;
}
protected function conditionGroupFactory($conjunction = 'AND') {
$class = static::getClass($this->namespaces, 'Condition');
return new $class($conjunction, $this, $this->namespaces);
}
public function andConditionGroup() {
return $this
->conditionGroupFactory('and');
}
public function orConditionGroup() {
return $this
->conditionGroupFactory('or');
}
public function sort($field, $direction = 'ASC', $langcode = NULL) {
$this->sort[] = [
'field' => $field,
'direction' => strtoupper($direction),
'langcode' => $langcode,
];
return $this;
}
public function count() {
$this->count = TRUE;
return $this;
}
public function accessCheck($access_check = TRUE) {
$this->accessCheck = $access_check;
return $this;
}
public function currentRevision() {
$this->allRevisions = FALSE;
$this->latestRevision = FALSE;
return $this;
}
public function latestRevision() {
$this->allRevisions = TRUE;
$this->latestRevision = TRUE;
return $this;
}
public function allRevisions() {
$this->allRevisions = TRUE;
$this->latestRevision = FALSE;
return $this;
}
public function pager($limit = 10, $element = NULL) {
if (!isset($element)) {
$element = \Drupal::service('pager.manager')
->getMaxPagerElementId() + 1;
}
$this->pager = [
'limit' => $limit,
'element' => $element,
];
return $this;
}
protected function initializePager() {
if ($this->pager && !empty($this->pager['limit']) && !$this->count) {
$page = \Drupal::service('pager.parameters')
->findPage($this->pager['element']);
$count_query = clone $this;
$this->pager['total'] = $count_query
->count()
->execute();
$this->pager['start'] = $page * $this->pager['limit'];
\Drupal::service('pager.manager')
->createPager($this->pager['total'], $this->pager['limit'], $this->pager['element']);
$this
->range($this->pager['start'], $this->pager['limit']);
}
}
public function tableSort(&$headers) {
foreach ($headers as $key => $header) {
if (is_array($header) && isset($header['specifier'])) {
$headers[$key]['field'] = '';
}
}
$order = TableSort::getOrder($headers, \Drupal::request());
$direction = TableSort::getSort($headers, \Drupal::request());
foreach ($headers as $header) {
if (is_array($header) && $header['data'] == $order['name']) {
$this
->sort($header['specifier'], $direction, isset($header['langcode']) ? $header['langcode'] : NULL);
}
}
return $this;
}
public function __clone() {
$this->condition = clone $this->condition;
}
public function addTag($tag) {
$this->alterTags[$tag] = 1;
return $this;
}
public function hasTag($tag) {
return isset($this->alterTags[$tag]);
}
public function hasAllTags() {
return !(bool) array_diff(func_get_args(), array_keys($this->alterTags));
}
public function hasAnyTag() {
return (bool) array_intersect(func_get_args(), array_keys($this->alterTags));
}
public function addMetaData($key, $object) {
$this->alterMetaData[$key] = $object;
return $this;
}
public function getMetaData($key) {
return isset($this->alterMetaData[$key]) ? $this->alterMetaData[$key] : NULL;
}
public function aggregate($field, $function, $langcode = NULL, &$alias = NULL) {
if (!isset($alias)) {
$alias = $this
->getAggregationAlias($field, $function);
}
$this->aggregate[$alias] = [
'field' => $field,
'function' => $function,
'alias' => $alias,
'langcode' => $langcode,
];
return $this;
}
public function conditionAggregate($field, $function = NULL, $value = NULL, $operator = '=', $langcode = NULL) {
$this
->aggregate($field, $function, $langcode);
$this->conditionAggregate
->condition($field, $function, $value, $operator, $langcode);
return $this;
}
public function sortAggregate($field, $function, $direction = 'ASC', $langcode = NULL) {
$alias = $this
->getAggregationAlias($field, $function);
$this->sortAggregate[$alias] = [
'field' => $field,
'function' => $function,
'direction' => $direction,
'langcode' => $langcode,
];
$this
->aggregate($field, $function, $langcode, $alias);
return $this;
}
public function groupBy($field, $langcode = NULL) {
$this->groupBy[] = [
'field' => $field,
'langcode' => $langcode,
];
return $this;
}
protected function getAggregationAlias($field, $function) {
return strtolower($field . '_' . $function);
}
public static function getNamespaces($object) {
$namespaces = [];
for ($class = get_class($object); $class; $class = get_parent_class($class)) {
$namespaces[] = substr($class, 0, strrpos($class, '\\'));
}
return $namespaces;
}
public static function getClass(array $namespaces, $short_class_name) {
foreach ($namespaces as $namespace) {
$class = $namespace . '\\' . $short_class_name;
if (class_exists($class)) {
return $class;
}
}
}
}