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