CompositeExpression.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\Common\Collections\Expr;
  20. /**
  21. * Expression of Expressions combined by AND or OR operation.
  22. *
  23. * @author Benjamin Eberlei <kontakt@beberlei.de>
  24. * @since 2.3
  25. */
  26. class CompositeExpression implements Expression
  27. {
  28. const TYPE_AND = 'AND';
  29. const TYPE_OR = 'OR';
  30. /**
  31. * @var string
  32. */
  33. private $type;
  34. /**
  35. * @var Expression[]
  36. */
  37. private $expressions = array();
  38. /**
  39. * @param string $type
  40. * @param array $expressions
  41. *
  42. * @throws \RuntimeException
  43. */
  44. public function __construct($type, array $expressions)
  45. {
  46. $this->type = $type;
  47. foreach ($expressions as $expr) {
  48. if ($expr instanceof Value) {
  49. throw new \RuntimeException("Values are not supported expressions as children of and/or expressions.");
  50. }
  51. if ( ! ($expr instanceof Expression)) {
  52. throw new \RuntimeException("No expression given to CompositeExpression.");
  53. }
  54. $this->expressions[] = $expr;
  55. }
  56. }
  57. /**
  58. * Returns the list of expressions nested in this composite.
  59. *
  60. * @return Expression[]
  61. */
  62. public function getExpressionList()
  63. {
  64. return $this->expressions;
  65. }
  66. /**
  67. * @return string
  68. */
  69. public function getType()
  70. {
  71. return $this->type;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function visit(ExpressionVisitor $visitor)
  77. {
  78. return $visitor->walkCompositeExpression($this);
  79. }
  80. }