Completed
Push — master ( 2a239b...205ee7 )
by Marco
22s
created

lib/Doctrine/ORM/Query/FilterCollection.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM\Query;
21
22
use Doctrine\ORM\EntityManagerInterface;
23
24
/**
25
 * Collection class for all the query filters.
26
 *
27
 * @author Alexander <[email protected]>
28
 */
29
class FilterCollection
30
{
31
    /* Filter STATES */
32
33
    /**
34
     * A filter object is in CLEAN state when it has no changed parameters.
35
     */
36
    const FILTERS_STATE_CLEAN  = 1;
37
38
    /**
39
     * A filter object is in DIRTY state when it has changed parameters.
40
     */
41
    const FILTERS_STATE_DIRTY = 2;
42
43
    /**
44
     * The used Configuration.
45
     *
46
     * @var \Doctrine\ORM\Configuration
47
     */
48
    private $config;
49
50
    /**
51
     * The EntityManager that "owns" this FilterCollection instance.
52
     *
53
     * @var \Doctrine\ORM\EntityManager
54
     */
55
    private $em;
56
57
    /**
58
     * Instances of enabled filters.
59
     *
60
     * @var \Doctrine\ORM\Query\Filter\SQLFilter[]
61
     */
62
    private $enabledFilters = [];
63
64
    /**
65
     * @var string The filter hash from the last time the query was parsed.
66
     */
67
    private $filterHash;
68
69
    /**
70
     * @var integer The current state of this filter.
71
     */
72
    private $filtersState = self::FILTERS_STATE_CLEAN;
73
74
    /**
75
     * Constructor.
76
     *
77
     * @param EntityManagerInterface $em
78
     */
79 609
    public function __construct(EntityManagerInterface $em)
80
    {
81 609
        $this->em = $em;
0 ignored issues
show
Documentation Bug introduced by
$em is of type object<Doctrine\ORM\EntityManagerInterface>, but the property $em was declared to be of type object<Doctrine\ORM\EntityManager>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
82 609
        $this->config = $em->getConfiguration();
83 609
    }
84
85
    /**
86
     * Gets all the enabled filters.
87
     *
88
     * @return \Doctrine\ORM\Query\Filter\SQLFilter[] The enabled filters.
89
     */
90 602
    public function getEnabledFilters()
91
    {
92 602
        return $this->enabledFilters;
93
    }
94
95
    /**
96
     * Enables a filter from the collection.
97
     *
98
     * @param string $name Name of the filter.
99
     *
100
     * @return \Doctrine\ORM\Query\Filter\SQLFilter The enabled filter.
101
     *
102
     * @throws \InvalidArgumentException If the filter does not exist.
103
     */
104 42
    public function enable($name)
105
    {
106 42
        if ( ! $this->has($name)) {
107 1
            throw new \InvalidArgumentException("Filter '" . $name . "' does not exist.");
108
        }
109
110 42
        if ( ! $this->isEnabled($name)) {
111 42
            $filterClass = $this->config->getFilterClassName($name);
112
113 42
            $this->enabledFilters[$name] = new $filterClass($this->em);
114
115
            // Keep the enabled filters sorted for the hash
116 42
            ksort($this->enabledFilters);
117
118
            // Now the filter collection is dirty
119 42
            $this->filtersState = self::FILTERS_STATE_DIRTY;
120
        }
121
122 42
        return $this->enabledFilters[$name];
123
    }
124
125
    /**
126
     * Disables a filter.
127
     *
128
     * @param string $name Name of the filter.
129
     *
130
     * @return \Doctrine\ORM\Query\Filter\SQLFilter The disabled filter.
131
     *
132
     * @throws \InvalidArgumentException If the filter does not exist.
133
     */
134 3
    public function disable($name)
135
    {
136
        // Get the filter to return it
137 3
        $filter = $this->getFilter($name);
138
139 3
        unset($this->enabledFilters[$name]);
140
141
        // Now the filter collection is dirty
142 3
        $this->filtersState = self::FILTERS_STATE_DIRTY;
143
144 3
        return $filter;
145
    }
146
147
    /**
148
     * Gets an enabled filter from the collection.
149
     *
150
     * @param string $name Name of the filter.
151
     *
152
     * @return \Doctrine\ORM\Query\Filter\SQLFilter The filter.
153
     *
154
     * @throws \InvalidArgumentException If the filter is not enabled.
155
     */
156 6
    public function getFilter($name)
157
    {
158 6
        if ( ! $this->isEnabled($name)) {
159 3
            throw new \InvalidArgumentException("Filter '" . $name . "' is not enabled.");
160
        }
161
162 5
        return $this->enabledFilters[$name];
163
    }
164
165
    /**
166
     * Checks whether filter with given name is defined.
167
     *
168
     * @param string $name Name of the filter.
169
     *
170
     * @return bool true if the filter exists, false if not.
171
     */
172 43
    public function has($name)
173
    {
174 43
        return null !== $this->config->getFilterClassName($name);
175
    }
176
177
    /**
178
     * Checks if a filter is enabled.
179
     *
180
     * @param string $name Name of the filter.
181
     *
182
     * @return boolean True if the filter is enabled, false otherwise.
183
     */
184 43
    public function isEnabled($name)
185
    {
186 43
        return isset($this->enabledFilters[$name]);
187
    }
188
189
    /**
190
     * @return boolean True, if the filter collection is clean.
191
     */
192 5
    public function isClean()
193
    {
194 5
        return self::FILTERS_STATE_CLEAN === $this->filtersState;
195
    }
196
197
    /**
198
     * Generates a string of currently enabled filters to use for the cache id.
199
     *
200
     * @return string
201
     */
202 63
    public function getHash()
203
    {
204
        // If there are only clean filters, the previous hash can be returned
205 63
        if (self::FILTERS_STATE_CLEAN === $this->filtersState) {
206 57
            return $this->filterHash;
207
        }
208
209 10
        $filterHash = '';
210
211 10
        foreach ($this->enabledFilters as $name => $filter) {
212 10
            $filterHash .= $name . $filter;
213
        }
214
215 10
        return $filterHash;
216
    }
217
218
    /**
219
     * Sets the filter state to dirty.
220
     */
221 33
    public function setFiltersStateDirty()
222
    {
223 33
        $this->filtersState = self::FILTERS_STATE_DIRTY;
224 33
    }
225
}
226