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

https://github.com/jaikdean/doctrine2 · PHP · 203 lines · 81 code · 33 blank · 89 comment · 4 complexity · f5c52f9498b7c9f036d90783e9dce932 MD5 · raw file

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