3 namespace Drupal\entity\QueryAccess;
6 * Represents a single query access condition.
8 final class Condition {
11 * The supported operators.
15 protected static $supportedOperators = [
16 '=', '<>', '<', '<=', '>', '>=', 'BETWEEN', 'NOT BETWEEN',
17 'IN', 'NOT IN', 'IS NULL', 'IS NOT NULL',
42 * Constructs a new Condition object.
44 * @param string $field
45 * The field, with an optional column name. E.g: 'uid', 'address.locality'.
48 * @param string $operator
50 * Possible values: =, <>, <, <=, >, >=, BETWEEN, NOT BETWEEN,
51 * IN, NOT IN, IS NULL, IS NOT NULL.
53 public function __construct($field, $value, $operator = NULL) {
54 // Provide a default based on the data type of the value.
55 if (!isset($operator)) {
56 $operator = is_array($value) ? 'IN' : '=';
58 // Validate the selected operator.
59 if (!in_array($operator, self::$supportedOperators)) {
60 throw new \InvalidArgumentException(sprintf('Unrecognized operator "%s".', $operator));
63 $this->field = $field;
64 $this->value = $value;
65 $this->operator = $operator;
71 public function getField() {
78 public function getValue() {
85 public function getOperator() {
86 return $this->operator;
90 * Gets the string representation of the condition.
92 * Used for debugging purposes.
95 * The string representation of the condition.
97 public function __toString() {
98 if (in_array($this->operator, ['IS NULL', 'IS NOT NULL'])) {
99 return "{$this->field} {$this->operator}";
102 if (is_array($this->value)) {
103 $value = "['" . implode("', '", $this->value) . "']";
106 $value = "'" . $this->value . "'";
109 return "{$this->field} {$this->operator} $value";