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