5da68dcf58a462286019510ba3d42614d3a577f3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Database / Query / Restriction / AbstractRestrictionContainer.php
1 <?php
2 declare (strict_types = 1);
3 namespace TYPO3\CMS\Core\Database\Query\Restriction;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Database\Query\Expression\CompositeExpression;
19 use TYPO3\CMS\Core\Database\Query\Expression\ExpressionBuilder;
20 use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22 /**
23 * Base class for query restriction collections
24 */
25 abstract class AbstractRestrictionContainer implements QueryRestrictionContainerInterface
26 {
27 /**
28 * @var QueryRestrictionInterface[]
29 */
30 protected $restrictions = [];
31
32 /**
33 * Main method to build expressions for given tables.
34 * Iterating over all registered expressions and combine them with AND
35 *
36 * @param array $queriedTables Array of tables, where array key is table name and value potentially an alias
37 * @param ExpressionBuilder $expressionBuilder Expression builder instance to add restrictions with
38 * @return CompositeExpression The result of query builder expression(s)
39 */
40 public function buildExpression(array $queriedTables, ExpressionBuilder $expressionBuilder): CompositeExpression
41 {
42 $constraints = [];
43 foreach ($this->restrictions as $restriction) {
44 $constraints[] = $restriction->buildExpression($queriedTables, $expressionBuilder);
45 }
46 return $expressionBuilder->andX(...$constraints);
47 }
48
49 /**
50 * Removes all restrictions stored within this container
51 *
52 * @return QueryRestrictionContainerInterface
53 */
54 public function removeAll()
55 {
56 $this->restrictions = [];
57 return $this;
58 }
59
60 /**
61 * Remove restriction of a given type
62 *
63 * @param string $restrictionType Class name of the restriction to be removed
64 * @return QueryRestrictionContainerInterface
65 */
66 public function removeByType(string $restrictionType)
67 {
68 unset($this->restrictions[$restrictionType]);
69 return $this;
70 }
71
72 /**
73 * Add a new restriction instance to this collection
74 *
75 * @param QueryRestrictionInterface $restriction
76 * @return QueryRestrictionContainerInterface
77 */
78 public function add(QueryRestrictionInterface $restriction)
79 {
80 $this->restrictions[get_class($restriction)] = $restriction;
81 return $this;
82 }
83
84 /**
85 * Factory method for restrictions.
86 * Currently only instantiates the class.
87 *
88 * @param string $restrictionClass
89 * @return QueryRestrictionInterface
90 */
91 protected function createRestriction($restrictionClass)
92 {
93 return GeneralUtility::makeInstance($restrictionClass);
94 }
95 }