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.
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>.
20 namespace Doctrine\ORM\Query;
22 use Doctrine\ORM\Configuration,
23 Doctrine\ORM\EntityManager;
26 * Collection class for all the query filters.
28 * @author Alexander <iam.asm89@gmail.com>
30 class FilterCollection
34 * A filter object is in CLEAN state when it has no changed parameters.
36 const FILTERS_STATE_CLEAN = 1;
39 * A filter object is in DIRTY state when it has changed parameters.
41 const FILTERS_STATE_DIRTY = 2;
44 * The used Configuration.
46 * @var Doctrine\ORM\Configuration
51 * The EntityManager that "owns" this FilterCollection instance.
53 * @var Doctrine\ORM\EntityManager
58 * Instances of enabled filters.
62 private $enabledFilters = array();
65 * @var string The filter hash from the last time the query was parsed.
70 * @var integer $state The current state of this filter
72 private $filtersState = self::FILTERS_STATE_CLEAN;
77 * @param EntityManager $em
79 public function __construct(EntityManager $em)
82 $this->config = $em->getConfiguration();
86 * Get all the enabled filters.
88 * @return array The enabled filters.
90 public function getEnabledFilters()
92 return $this->enabledFilters;
96 * Enables a filter from the collection.
98 * @param string $name Name of the filter.
100 * @throws \InvalidArgumentException If the filter does not exist.
102 * @return SQLFilter The enabled filter.
104 public function enable($name)
106 if (null === $filterClass = $this->config->getFilterClassName($name)) {
107 throw new \InvalidArgumentException("Filter '" . $name . "' does not exist.");
110 if (!isset($this->enabledFilters[$name])) {
111 $this->enabledFilters[$name] = new $filterClass($this->em);
113 // Keep the enabled filters sorted for the hash
114 ksort($this->enabledFilters);
116 // Now the filter collection is dirty
117 $this->filtersState = self::FILTERS_STATE_DIRTY;
120 return $this->enabledFilters[$name];
126 * @param string $name Name of the filter.
128 * @return SQLFilter The disabled filter.
130 * @throws \InvalidArgumentException If the filter does not exist.
132 public function disable($name)
134 // Get the filter to return it
135 $filter = $this->getFilter($name);
137 unset($this->enabledFilters[$name]);
139 // Now the filter collection is dirty
140 $this->filtersState = self::FILTERS_STATE_DIRTY;
146 * Get an enabled filter from the collection.
148 * @param string $name Name of the filter.
150 * @return SQLFilter The filter.
152 * @throws \InvalidArgumentException If the filter is not enabled.
154 public function getFilter($name)
156 if (!isset($this->enabledFilters[$name])) {
157 throw new \InvalidArgumentException("Filter '" . $name . "' is not enabled.");
160 return $this->enabledFilters[$name];
164 * @return boolean True, if the filter collection is clean.
166 public function isClean()
168 return self::FILTERS_STATE_CLEAN === $this->filtersState;
172 * Generates a string of currently enabled filters to use for the cache id.
176 public function getHash()
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;
184 foreach ($this->enabledFilters as $name => $filter) {
185 $filterHash .= $name . $filter;
192 * Set the filter state to dirty.
194 public function setFiltersStateDirty()
196 $this->filtersState = self::FILTERS_STATE_DIRTY;