CompositeExpression.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 array $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. /**
  44. * @return string
  45. */
  46. public function getType()
  47. {
  48. return $this->type;
  49. }
  50. /**
  51. * {@inheritDoc}
  52. */
  53. public function visit(ExpressionVisitor $visitor)
  54. {
  55. return $visitor->walkCompositeExpression($this);
  56. }
  57. }