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

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