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

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.      * @psalm-var array<string, SQLFilter>
  41.      */
  42.     private $enabledFilters = [];
  43.     /**
  44.      * The filter hash from the last time the query was parsed.
  45.      *
  46.      * @var string
  47.      */
  48.     private $filterHash '';
  49.     /**
  50.      * The current state of this filter.
  51.      *
  52.      * @var int
  53.      * @psalm-var self::FILTERS_STATE_*
  54.      */
  55.     private $filtersState self::FILTERS_STATE_CLEAN;
  56.     public function __construct(EntityManagerInterface $em)
  57.     {
  58.         $this->em     $em;
  59.         $this->config $em->getConfiguration();
  60.     }
  61.     /**
  62.      * Gets all the enabled filters.
  63.      *
  64.      * @return SQLFilter[] The enabled filters.
  65.      * @psalm-return array<string, SQLFilter>
  66.      */
  67.     public function getEnabledFilters()
  68.     {
  69.         return $this->enabledFilters;
  70.     }
  71.     /**
  72.      * Enables a filter from the collection.
  73.      *
  74.      * @param string $name Name of the filter.
  75.      *
  76.      * @return SQLFilter The enabled filter.
  77.      *
  78.      * @throws InvalidArgumentException If the filter does not exist.
  79.      */
  80.     public function enable($name)
  81.     {
  82.         if (! $this->has($name)) {
  83.             throw new InvalidArgumentException("Filter '" $name "' does not exist.");
  84.         }
  85.         if (! $this->isEnabled($name)) {
  86.             $filterClass $this->config->getFilterClassName($name);
  87.             assert($filterClass !== null);
  88.             $this->enabledFilters[$name] = new $filterClass($this->em);
  89.             // Keep the enabled filters sorted for the hash
  90.             ksort($this->enabledFilters);
  91.             $this->setFiltersStateDirty();
  92.         }
  93.         return $this->enabledFilters[$name];
  94.     }
  95.     /**
  96.      * Disables a filter.
  97.      *
  98.      * @param string $name Name of the filter.
  99.      *
  100.      * @return SQLFilter The disabled filter.
  101.      *
  102.      * @throws InvalidArgumentException If the filter does not exist.
  103.      */
  104.     public function disable($name)
  105.     {
  106.         // Get the filter to return it
  107.         $filter $this->getFilter($name);
  108.         unset($this->enabledFilters[$name]);
  109.         $this->setFiltersStateDirty();
  110.         return $filter;
  111.     }
  112.     /**
  113.      * Gets an enabled filter from the collection.
  114.      *
  115.      * @param string $name Name of the filter.
  116.      *
  117.      * @return SQLFilter The filter.
  118.      *
  119.      * @throws InvalidArgumentException If the filter is not enabled.
  120.      */
  121.     public function getFilter($name)
  122.     {
  123.         if (! $this->isEnabled($name)) {
  124.             throw new InvalidArgumentException("Filter '" $name "' is not enabled.");
  125.         }
  126.         return $this->enabledFilters[$name];
  127.     }
  128.     /**
  129.      * Checks whether filter with given name is defined.
  130.      *
  131.      * @param string $name Name of the filter.
  132.      *
  133.      * @return bool true if the filter exists, false if not.
  134.      */
  135.     public function has($name)
  136.     {
  137.         return $this->config->getFilterClassName($name) !== null;
  138.     }
  139.     /**
  140.      * Checks if a filter is enabled.
  141.      *
  142.      * @param string $name Name of the filter.
  143.      *
  144.      * @return bool True if the filter is enabled, false otherwise.
  145.      */
  146.     public function isEnabled($name)
  147.     {
  148.         return isset($this->enabledFilters[$name]);
  149.     }
  150.     /**
  151.      * Checks if the filter collection is clean.
  152.      *
  153.      * @return bool
  154.      */
  155.     public function isClean()
  156.     {
  157.         return $this->filtersState === self::FILTERS_STATE_CLEAN;
  158.     }
  159.     /**
  160.      * Generates a string of currently enabled filters to use for the cache id.
  161.      *
  162.      * @return string
  163.      */
  164.     public function getHash()
  165.     {
  166.         // If there are only clean filters, the previous hash can be returned
  167.         if ($this->filtersState === self::FILTERS_STATE_CLEAN) {
  168.             return $this->filterHash;
  169.         }
  170.         $filterHash '';
  171.         foreach ($this->enabledFilters as $name => $filter) {
  172.             $filterHash .= $name $filter;
  173.         }
  174.         $this->filterHash   $filterHash;
  175.         $this->filtersState self::FILTERS_STATE_CLEAN;
  176.         return $filterHash;
  177.     }
  178.     /**
  179.      * Sets the filter state to dirty.
  180.      *
  181.      * @return void
  182.      */
  183.     public function setFiltersStateDirty()
  184.     {
  185.         $this->filtersState self::FILTERS_STATE_DIRTY;
  186.     }
  187. }