/lib/Doctrine/ORM/Query/FilterCollection.php

https://github.com/stefanklug/doctrine2 · PHP · 198 lines · 68 code · 27 blank · 103 comment · 4 complexity · 051f6a3258561e0641448c88bd9cd780 MD5 · raw file

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