ConditionFundamentals.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Drupal\Core\Entity\Query;
  3. /**
  4. * Common code for all implementations of the entity query condition interfaces.
  5. */
  6. abstract class ConditionFundamentals {
  7. /**
  8. * Array of conditions.
  9. *
  10. * @var array
  11. */
  12. protected $conditions = [];
  13. /**
  14. * The conjunction of this condition group. The value is one of the following:
  15. *
  16. * - AND (default)
  17. * - OR
  18. *
  19. * @var string
  20. */
  21. protected $conjunction;
  22. /**
  23. * The query this condition belongs to.
  24. *
  25. * @var \Drupal\Core\Entity\Query\QueryInterface
  26. */
  27. protected $query;
  28. /**
  29. * List of potential namespaces of the classes belonging to this condition.
  30. *
  31. * @var array
  32. */
  33. protected $namespaces = [];
  34. /**
  35. * Constructs a Condition object.
  36. *
  37. * @param string $conjunction
  38. * The operator to use to combine conditions: 'AND' or 'OR'.
  39. * @param QueryInterface $query
  40. * The entity query this condition belongs to.
  41. * @param array $namespaces
  42. * List of potential namespaces of the classes belonging to this condition.
  43. */
  44. public function __construct($conjunction, QueryInterface $query, $namespaces = []) {
  45. $this->conjunction = $conjunction;
  46. $this->query = $query;
  47. $this->namespaces = $namespaces;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getConjunction() {
  53. return $this->conjunction;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function count() {
  59. return count($this->conditions) - 1;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function &conditions() {
  65. return $this->conditions;
  66. }
  67. /**
  68. * Implements the magic __clone function.
  69. *
  70. * Makes sure condition groups are cloned as well.
  71. */
  72. public function __clone() {
  73. foreach ($this->conditions as $key => $condition) {
  74. if ($condition['field'] instanceof ConditionInterface) {
  75. $this->conditions[$key]['field'] = clone($condition['field']);
  76. }
  77. }
  78. }
  79. }