Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / QueryAccess / Condition.php
1 <?php
2
3 namespace Drupal\entity\QueryAccess;
4
5 /**
6  * Represents a single query access condition.
7  */
8 final class Condition {
9
10   /**
11    * The supported operators.
12    *
13    * @var string[]
14    */
15   protected static $supportedOperators = [
16     '=', '<>', '<', '<=', '>', '>=', 'BETWEEN', 'NOT BETWEEN',
17     'IN', 'NOT IN', 'IS NULL', 'IS NOT NULL',
18   ];
19
20   /**
21    * The field.
22    *
23    * @var string
24    */
25   protected $field;
26
27   /**
28    * The value.
29    *
30    * @var mixed
31    */
32   protected $value;
33
34   /**
35    * The operator.
36    *
37    * @var string
38    */
39   protected $operator;
40
41   /**
42    * Constructs a new Condition object.
43    *
44    * @param string $field
45    *   The field, with an optional column name. E.g: 'uid', 'address.locality'.
46    * @param mixed $value
47    *   The value.
48    * @param string $operator
49    *   The operator.
50    *   Possible values: =, <>, <, <=, >, >=, BETWEEN, NOT BETWEEN,
51    *                   IN, NOT IN, IS NULL, IS NOT NULL.
52    */
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' : '=';
57     }
58     // Validate the selected operator.
59     if (!in_array($operator, self::$supportedOperators)) {
60       throw new \InvalidArgumentException(sprintf('Unrecognized operator "%s".', $operator));
61     }
62
63     $this->field = $field;
64     $this->value = $value;
65     $this->operator = $operator;
66   }
67
68   /**
69    * {@inheritdoc}
70    */
71   public function getField() {
72     return $this->field;
73   }
74
75   /**
76    * {@inheritdoc}
77    */
78   public function getValue() {
79     return $this->value;
80   }
81
82   /**
83    * {@inheritdoc}
84    */
85   public function getOperator() {
86     return $this->operator;
87   }
88
89   /**
90    * Gets the string representation of the condition.
91    *
92    * Used for debugging purposes.
93    *
94    * @return string
95    *   The string representation of the condition.
96    */
97   public function __toString() {
98     if (in_array($this->operator, ['IS NULL', 'IS NOT NULL'])) {
99       return "{$this->field} {$this->operator}";
100     }
101     else {
102       if (is_array($this->value)) {
103         $value = "['" . implode("', '", $this->value) . "']";
104       }
105       else {
106         $value = "'" . $this->value . "'";
107       }
108
109       return "{$this->field} {$this->operator} $value";
110     }
111   }
112
113 }