Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Query / Filter / SQLFilter.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\Filter;
21
22 use Doctrine\ORM\EntityManager,
23     Doctrine\ORM\Mapping\ClassMetaData,
24     Doctrine\ORM\Query\ParameterTypeInferer;
25
26 /**
27  * The base class that user defined filters should extend.
28  *
29  * Handles the setting and escaping of parameters.
30  *
31  * @author Alexander <iam.asm89@gmail.com>
32  * @author Benjamin Eberlei <kontakt@beberlei.de>
33  * @abstract
34  */
35 abstract class SQLFilter
36 {
37     /**
38      * The entity manager.
39      * @var EntityManager
40      */
41     private $em;
42
43     /**
44      * Parameters for the filter.
45      * @var array
46      */
47     private $parameters;
48
49     /**
50      * Constructs the SQLFilter object.
51      *
52      * @param EntityManager $em The EM
53      */
54     final public function __construct(EntityManager $em)
55     {
56         $this->em = $em;
57     }
58
59     /**
60      * Sets a parameter that can be used by the filter.
61      *
62      * @param string $name Name of the parameter.
63      * @param string $value Value of the parameter.
64      * @param string $type The parameter type. If specified, the given value will be run through
65      *                     the type conversion of this type. This is usually not needed for
66      *                     strings and numeric types.
67      *
68      * @return SQLFilter The current SQL filter.
69      */
70     final public function setParameter($name, $value, $type = null)
71     {
72         if (null === $type) {
73             $type = ParameterTypeInferer::inferType($value);
74         }
75
76         $this->parameters[$name] = array('value' => $value, 'type' => $type);
77
78         // Keep the parameters sorted for the hash
79         ksort($this->parameters);
80
81         // The filter collection of the EM is now dirty
82         $this->em->getFilters()->setFiltersStateDirty();
83
84         return $this;
85     }
86
87     /**
88      * Gets a parameter to use in a query.
89      *
90      * The function is responsible for the right output escaping to use the
91      * value in a query.
92      *
93      * @param string $name Name of the parameter.
94      *
95      * @return string The SQL escaped parameter to use in a query.
96      */
97     final public function getParameter($name)
98     {
99         if (!isset($this->parameters[$name])) {
100             throw new \InvalidArgumentException("Parameter '" . $name . "' does not exist.");
101         }
102
103         return $this->em->getConnection()->quote($this->parameters[$name]['value'], $this->parameters[$name]['type']);
104     }
105
106     /**
107      * Returns as string representation of the SQLFilter parameters (the state).
108      *
109      * @return string String representation of the SQLFilter.
110      */
111     final public function __toString()
112     {
113         return serialize($this->parameters);
114     }
115
116     /**
117      * Gets the SQL query part to add to a query.
118      *
119      * @return string The constraint SQL if there is available, empty string otherwise
120      */
121     abstract public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias);
122 }