Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Query / Expr / Base.php
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
20 namespace Doctrine\ORM\Query\Expr;
21
22 /**
23  * Abstract base Expr class for building DQL parts
24  *
25  * 
26  * @link    www.doctrine-project.org
27  * @since   2.0
28  * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
29  * @author  Jonathan Wage <jonwage@gmail.com>
30  * @author  Roman Borschel <roman@code-factory.org>
31  */
32 abstract class Base
33 {
34     /**
35      * @var string
36      */
37     protected $preSeparator = '(';
38
39     /**
40      * @var string
41      */
42     protected $separator = ', ';
43
44     /**
45      * @var string
46      */
47     protected $postSeparator = ')';
48
49     /**
50      * @var array
51      */
52     protected $allowedClasses = array();
53
54     /**
55      * @var array
56      */
57     protected $parts = array();
58
59     /**
60      * @param array $args
61      */
62     public function __construct($args = array())
63     {
64         $this->addMultiple($args);
65     }
66
67     /**
68      * @param   array $args
69      * @return  Base
70      */
71     public function addMultiple($args = array())
72     {
73         foreach ((array) $args as $arg) {
74             $this->add($arg);
75         }
76
77         return $this;
78     }
79
80     /**
81      * @param   mixed $arg
82      * @return  Base
83      */
84     public function add($arg)
85     {
86         if ( $arg !== null || ($arg instanceof self && $arg->count() > 0) ) {
87             // If we decide to keep Expr\Base instances, we can use this check
88             if ( ! is_string($arg)) {
89                 $class = get_class($arg);
90
91                 if ( ! in_array($class, $this->allowedClasses)) {
92                     throw new \InvalidArgumentException("Expression of type '$class' not allowed in this context.");
93                 }
94             }
95
96             $this->parts[] = $arg;
97         }
98
99         return $this;
100     }
101
102     /**
103      * @return integer
104      */
105     public function count()
106     {
107         return count($this->parts);
108     }
109
110     /**
111      * @return string
112      */
113     public function __toString()
114     {
115         if ($this->count() == 1) {
116             return (string) $this->parts[0];
117         }
118
119         return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator;
120     }
121 }