Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / lib / Doctrine / ORM / Query / FilterCollection.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;
21
22 use Doctrine\ORM\Configuration,
23     Doctrine\ORM\EntityManager;
24
25 /**
26  * Collection class for all the query filters.
27  *
28  * @author Alexander <iam.asm89@gmail.com>
29  */
30 class FilterCollection
31 {
32     /* Filter STATES */
33     /**
34      * A filter object is in CLEAN state when it has no changed parameters.
35      */
36     const FILTERS_STATE_CLEAN  = 1;
37
38     /**
39      * A filter object is in DIRTY state when it has changed parameters.
40      */
41     const FILTERS_STATE_DIRTY = 2;
42
43     /**
44      * The used Configuration.
45      *
46      * @var Doctrine\ORM\Configuration
47      */
48     private $config;
49
50     /**
51      * The EntityManager that "owns" this FilterCollection instance.
52      *
53      * @var Doctrine\ORM\EntityManager
54      */
55     private $em;
56
57     /**
58      * Instances of enabled filters.
59      *
60      * @var array
61      */
62     private $enabledFilters = array();
63
64     /**
65      * @var string The filter hash from the last time the query was parsed.
66      */
67     private $filterHash;
68
69     /**
70      * @var integer $state The current state of this filter
71      */
72     private $filtersState = self::FILTERS_STATE_CLEAN;
73
74     /**
75      * Constructor.
76      *
77      * @param EntityManager $em
78      */
79     public function __construct(EntityManager $em)
80     {
81         $this->em = $em;
82         $this->config = $em->getConfiguration();
83     }
84
85     /**
86      * Get all the enabled filters.
87      *
88      * @return array The enabled filters.
89      */
90     public function getEnabledFilters()
91     {
92         return $this->enabledFilters;
93     }
94
95     /**
96      * Enables a filter from the collection.
97      *
98      * @param string $name Name of the filter.
99      *
100      * @throws \InvalidArgumentException If the filter does not exist.
101      *
102      * @return SQLFilter The enabled filter.
103      */
104     public function enable($name)
105     {
106         if (null === $filterClass = $this->config->getFilterClassName($name)) {
107             throw new \InvalidArgumentException("Filter '" . $name . "' does not exist.");
108         }
109
110         if (!isset($this->enabledFilters[$name])) {
111             $this->enabledFilters[$name] = new $filterClass($this->em);
112
113             // Keep the enabled filters sorted for the hash
114             ksort($this->enabledFilters);
115
116             // Now the filter collection is dirty
117             $this->filtersState = self::FILTERS_STATE_DIRTY;
118         }
119
120         return $this->enabledFilters[$name];
121     }
122
123     /**
124      * Disables a filter.
125      *
126      * @param string $name Name of the filter.
127      *
128      * @return SQLFilter The disabled filter.
129      *
130      * @throws \InvalidArgumentException If the filter does not exist.
131      */
132     public function disable($name)
133     {
134         // Get the filter to return it
135         $filter = $this->getFilter($name);
136
137         unset($this->enabledFilters[$name]);
138
139         // Now the filter collection is dirty
140         $this->filtersState = self::FILTERS_STATE_DIRTY;
141
142         return $filter;
143     }
144
145     /**
146      * Get an enabled filter from the collection.
147      *
148      * @param string $name Name of the filter.
149      *
150      * @return SQLFilter The filter.
151      *
152      * @throws \InvalidArgumentException If the filter is not enabled.
153      */
154     public function getFilter($name)
155     {
156         if (!isset($this->enabledFilters[$name])) {
157             throw new \InvalidArgumentException("Filter '" . $name . "' is not enabled.");
158         }
159
160         return $this->enabledFilters[$name];
161     }
162
163     /**
164      * @return boolean True, if the filter collection is clean.
165      */
166     public function isClean()
167     {
168         return self::FILTERS_STATE_CLEAN === $this->filtersState;
169     }
170
171     /**
172      * Generates a string of currently enabled filters to use for the cache id.
173      *
174      * @return string
175      */
176     public function getHash()
177     {
178         // If there are only clean filters, the previous hash can be returned
179         if (self::FILTERS_STATE_CLEAN === $this->filtersState) {
180             return $this->filterHash;
181         }
182
183         $filterHash = '';
184         foreach ($this->enabledFilters as $name => $filter) {
185             $filterHash .= $name . $filter;
186         }
187
188         return $filterHash;
189     }
190
191     /**
192      * Set the filter state to dirty.
193      */
194     public function setFiltersStateDirty()
195     {
196         $this->filtersState = self::FILTERS_STATE_DIRTY;
197     }
198 }