CompositeExpression.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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\DBAL\Query\Expression;
  20. /**
  21. * Composite expression is responsible to build a group of similar expression.
  22. *
  23. * @link www.doctrine-project.org
  24. * @since 2.1
  25. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  26. * @author Benjamin Eberlei <kontakt@beberlei.de>
  27. */
  28. class CompositeExpression implements \Countable
  29. {
  30. /**
  31. * Constant that represents an AND composite expression.
  32. */
  33. const TYPE_AND = 'AND';
  34. /**
  35. * Constant that represents an OR composite expression.
  36. */
  37. const TYPE_OR = 'OR';
  38. /**
  39. * The instance type of composite expression.
  40. *
  41. * @var string
  42. */
  43. private $type;
  44. /**
  45. * Each expression part of the composite expression.
  46. *
  47. * @var array
  48. */
  49. private $parts = array();
  50. /**
  51. * Constructor.
  52. *
  53. * @param string $type Instance type of composite expression.
  54. * @param array $parts Composition of expressions to be joined on composite expression.
  55. */
  56. public function __construct($type, array $parts = array())
  57. {
  58. $this->type = $type;
  59. $this->addMultiple($parts);
  60. }
  61. /**
  62. * Adds multiple parts to composite expression.
  63. *
  64. * @param array $parts
  65. *
  66. * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
  67. */
  68. public function addMultiple(array $parts = array())
  69. {
  70. foreach ((array) $parts as $part) {
  71. $this->add($part);
  72. }
  73. return $this;
  74. }
  75. /**
  76. * Adds an expression to composite expression.
  77. *
  78. * @param mixed $part
  79. *
  80. * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
  81. */
  82. public function add($part)
  83. {
  84. if ( ! empty($part) || ($part instanceof self && $part->count() > 0)) {
  85. $this->parts[] = $part;
  86. }
  87. return $this;
  88. }
  89. /**
  90. * Retrieves the amount of expressions on composite expression.
  91. *
  92. * @return integer
  93. */
  94. public function count()
  95. {
  96. return count($this->parts);
  97. }
  98. /**
  99. * Retrieves the string representation of this composite expression.
  100. *
  101. * @return string
  102. */
  103. public function __toString()
  104. {
  105. if (count($this->parts) === 1) {
  106. return (string) $this->parts[0];
  107. }
  108. return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
  109. }
  110. /**
  111. * Returns the type of this composite expression (AND/OR).
  112. *
  113. * @return string
  114. */
  115. public function getType()
  116. {
  117. return $this->type;
  118. }
  119. }