CompositeExpression.php 3.5KB

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