You are here

final class Condition in Entity API 8

Represents a single query access condition.

Hierarchy

Expanded class hierarchy of Condition

5 files declare their use of Condition
ConditionGroupTest.php in tests/src/Kernel/QueryAccess/ConditionGroupTest.php
ConditionTest.php in tests/src/Unit/QueryAccess/ConditionTest.php
entity.module in ./entity.module
Provides expanded entity APIs.
QueryAccessHandlerTest.php in tests/src/Kernel/QueryAccess/QueryAccessHandlerTest.php
UncacheableQueryAccessHandlerTest.php in tests/src/Kernel/QueryAccess/UncacheableQueryAccessHandlerTest.php

File

src/QueryAccess/Condition.php, line 8

Namespace

Drupal\entity\QueryAccess
View source
final class Condition {

  /**
   * The supported operators.
   *
   * @var string[]
   */
  protected static $supportedOperators = [
    '=',
    '<>',
    '<',
    '<=',
    '>',
    '>=',
    'BETWEEN',
    'NOT BETWEEN',
    'IN',
    'NOT IN',
    'IS NULL',
    'IS NOT NULL',
  ];

  /**
   * The field.
   *
   * @var string
   */
  protected $field;

  /**
   * The value.
   *
   * @var mixed
   */
  protected $value;

  /**
   * The operator.
   *
   * @var string
   */
  protected $operator;

  /**
   * Constructs a new Condition object.
   *
   * @param string $field
   *   The field, with an optional column name. E.g: 'uid', 'address.locality'.
   * @param mixed $value
   *   The value.
   * @param string $operator
   *   The operator.
   *   Possible values: =, <>, <, <=, >, >=, BETWEEN, NOT BETWEEN,
   *                   IN, NOT IN, IS NULL, IS NOT NULL.
   */
  public function __construct($field, $value, $operator = NULL) {

    // Provide a default based on the data type of the value.
    if (!isset($operator)) {
      $operator = is_array($value) ? 'IN' : '=';
    }

    // Validate the selected operator.
    if (!in_array($operator, self::$supportedOperators)) {
      throw new \InvalidArgumentException(sprintf('Unrecognized operator "%s".', $operator));
    }
    $this->field = $field;
    $this->value = $value;
    $this->operator = $operator;
  }

  /**
   * {@inheritdoc}
   */
  public function getField() {
    return $this->field;
  }

  /**
   * {@inheritdoc}
   */
  public function getValue() {
    return $this->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getOperator() {
    return $this->operator;
  }

  /**
   * Gets the string representation of the condition.
   *
   * Used for debugging purposes.
   *
   * @return string
   *   The string representation of the condition.
   */
  public function __toString() {
    if (in_array($this->operator, [
      'IS NULL',
      'IS NOT NULL',
    ])) {
      return "{$this->field} {$this->operator}";
    }
    else {
      if (is_array($this->value)) {
        $value = "['" . implode("', '", $this->value) . "']";
      }
      else {
        $value = "'" . $this->value . "'";
      }
      return "{$this->field} {$this->operator} {$value}";
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Condition::$field protected property The field.
Condition::$operator protected property The operator.
Condition::$supportedOperators protected static property The supported operators.
Condition::$value protected property The value.
Condition::getField public function
Condition::getOperator public function
Condition::getValue public function
Condition::__construct public function Constructs a new Condition object.
Condition::__toString public function Gets the string representation of the condition.