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.
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>.
20 namespace Doctrine\DBAL\Query\Expression;
23 * Composite expression is responsible to build a group of similar expression.
25 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
26 * @link www.doctrine-project.org
28 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
29 * @author Benjamin Eberlei <kontakt@beberlei.de>
31 class CompositeExpression implements \Countable
34 * Constant that represents an AND composite expression
36 const TYPE_AND = 'AND';
39 * Constant that represents an OR composite expression
44 * @var string Holds the instance type of composite expression
49 * @var array Each expression part of the composite expression
51 private $parts = array();
56 * @param string $type Instance type of composite expression
57 * @param array $parts Composition of expressions to be joined on composite expression
59 public function __construct($type, array $parts = array())
63 $this->addMultiple($parts);
67 * Adds multiple parts to composite expression.
71 * @return CompositeExpression
73 public function addMultiple(array $parts = array())
75 foreach ((array) $parts as $part) {
83 * Adds an expression to composite expression.
86 * @return CompositeExpression
88 public function add($part)
90 if ( ! empty($part) || ($part instanceof self && $part->count() > 0)) {
91 $this->parts[] = $part;
98 * Retrieves the amount of expressions on composite expression.
102 public function count()
104 return count($this->parts);
108 * Retrieve the string representation of this composite expression.
112 public function __toString()
114 if (count($this->parts) === 1) {
115 return (string) $this->parts[0];
118 return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
122 * Return type of this composite expression (AND/OR)
126 public function getType()