CompositeExpression.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace Doctrine\Common\Collections\Expr;
  3. use RuntimeException;
  4. /**
  5. * Expression of Expressions combined by AND or OR operation.
  6. */
  7. class CompositeExpression implements Expression
  8. {
  9. public const TYPE_AND = 'AND';
  10. public const TYPE_OR = 'OR';
  11. /** @var string */
  12. private $type;
  13. /** @var Expression[] */
  14. private $expressions = [];
  15. /**
  16. * @param string $type
  17. * @param mixed[] $expressions
  18. *
  19. * @throws RuntimeException
  20. */
  21. public function __construct($type, array $expressions)
  22. {
  23. $this->type = $type;
  24. foreach ($expressions as $expr) {
  25. if ($expr instanceof Value) {
  26. throw new RuntimeException('Values are not supported expressions as children of and/or expressions.');
  27. }
  28. if (! ($expr instanceof Expression)) {
  29. throw new RuntimeException('No expression given to CompositeExpression.');
  30. }
  31. $this->expressions[] = $expr;
  32. }
  33. }
  34. /**
  35. * Returns the list of expressions nested in this composite.
  36. *
  37. * @return Expression[]
  38. */
  39. public function getExpressionList()
  40. {
  41. return $this->expressions;
  42. }
  43. /** @return string */
  44. public function getType()
  45. {
  46. return $this->type;
  47. }
  48. /**
  49. * {@inheritDoc}
  50. */
  51. public function visit(ExpressionVisitor $visitor)
  52. {
  53. return $visitor->walkCompositeExpression($this);
  54. }
  55. }