vendor/doctrine/orm/lib/Doctrine/ORM/Query/FilterCollection.php line 95

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