CompositeExpression.php 3.55 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
/*
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
Benjamin Eberlei's avatar
Benjamin Eberlei committed
16
 * and is licensed under the MIT license. For more information, see
17 18 19 20 21
 * <http://www.doctrine-project.org>.
 */

namespace Doctrine\DBAL\Query\Expression;

22 23 24
use function count;
use function implode;

25 26 27
/**
 * Composite expression is responsible to build a group of similar expression.
 *
Benjamin Morel's avatar
Benjamin Morel committed
28 29 30 31
 * @link   www.doctrine-project.org
 * @since  2.1
 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author Benjamin Eberlei <kontakt@beberlei.de>
32 33 34 35
 */
class CompositeExpression implements \Countable
{
    /**
Benjamin Morel's avatar
Benjamin Morel committed
36
     * Constant that represents an AND composite expression.
37 38
     */
    const TYPE_AND = 'AND';
39

40
    /**
Benjamin Morel's avatar
Benjamin Morel committed
41
     * Constant that represents an OR composite expression.
42 43
     */
    const TYPE_OR  = 'OR';
44

45
    /**
Benjamin Morel's avatar
Benjamin Morel committed
46 47 48
     * The instance type of composite expression.
     *
     * @var string
49 50
     */
    private $type;
51

52
    /**
Benjamin Morel's avatar
Benjamin Morel committed
53 54 55
     * Each expression part of the composite expression.
     *
     * @var array
56
     */
57
    private $parts = [];
58

59 60
    /**
     * Constructor.
61
     *
Benjamin Morel's avatar
Benjamin Morel committed
62 63
     * @param string $type  Instance type of composite expression.
     * @param array  $parts Composition of expressions to be joined on composite expression.
64
     */
65
    public function __construct($type, array $parts = [])
66 67
    {
        $this->type = $type;
68

69 70
        $this->addMultiple($parts);
    }
71

72 73
    /**
     * Adds multiple parts to composite expression.
74
     *
75
     * @param array $parts
76
     *
Benjamin Morel's avatar
Benjamin Morel committed
77
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
78
     */
79
    public function addMultiple(array $parts = [])
80
    {
81
        foreach ($parts as $part) {
82 83
            $this->add($part);
        }
84

85 86
        return $this;
    }
87

88 89
    /**
     * Adds an expression to composite expression.
90
     *
91
     * @param mixed $part
Benjamin Morel's avatar
Benjamin Morel committed
92 93
     *
     * @return \Doctrine\DBAL\Query\Expression\CompositeExpression
94 95 96
     */
    public function add($part)
    {
97 98
        if (empty($part)) {
            return $this;
99
        }
100

101 102 103 104 105 106
        if ($part instanceof self && 0 === count($part)) {
            return $this;
        }

        $this->parts[] = $part;

107 108
        return $this;
    }
109

110 111
    /**
     * Retrieves the amount of expressions on composite expression.
112
     *
113
     * @return int
114 115 116 117 118
     */
    public function count()
    {
        return count($this->parts);
    }
119

120
    /**
Benjamin Morel's avatar
Benjamin Morel committed
121
     * Retrieves the string representation of this composite expression.
122
     *
123 124 125 126
     * @return string
     */
    public function __toString()
    {
127
        if ($this->count() === 1) {
128 129
            return (string) $this->parts[0];
        }
130

131 132
        return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
    }
133

134
    /**
Benjamin Morel's avatar
Benjamin Morel committed
135
     * Returns the type of this composite expression (AND/OR).
136
     *
137 138 139 140 141 142
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }
Benjamin Eberlei's avatar
Benjamin Eberlei committed
143
}