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 LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Selector that filters files based on their size.
22
 *
23
 * @author Hans Lellelid <hans@xmpl.org> (Phing)
24
 * @author Bruce Atherton <bruce@callenish.com> (Ant)
25
 *
26
 * @package phing.types.selectors
27
 */
28
class SizeSelector extends BaseExtendSelector
29
{
30
    /**
31
     * @var int $size
32
     */
33
    private $size = -1;
34

35
    /**
36
     * @var int $multiplier
37
     */
38
    private $multiplier = 1;
39

40
    /**
41
     * @var int $sizelimit
42
     */
43
    private $sizelimit = -1;
44

45
    /**
46
     * @var int $cmp
47
     */
48
    private $cmp = 2;
49

50
    public const SIZE_KEY = "value";
51
    public const UNITS_KEY = "units";
52
    public const WHEN_KEY = "when";
53

54
    /**
55
     * @var array $sizeComparisons
56
     */
57
    private static $sizeComparisons = ["less", "more", "equal"];
58

59
    /**
60
     * @var array $byteUnits
61
     */
62
    private static $byteUnits = [
63
        "K",
64
        "k",
65
        "kilo",
66
        "KILO",
67
        "Ki",
68
        "KI",
69
        "ki",
70
        "kibi",
71
        "KIBI",
72
        "M",
73
        "m",
74
        "mega",
75
        "MEGA",
76
        "Mi",
77
        "MI",
78
        "mi",
79
        "mebi",
80
        "MEBI",
81
        "G",
82
        "g",
83
        "giga",
84
        "GIGA",
85
        "Gi",
86
        "GI",
87
        "gi",
88
        "gibi",
89
        "GIBI",
90
        "T",
91
        "t",
92
        "tera",
93
        "TERA",
94
        /* You wish! */
95
        "Ti",
96
        "TI",
97
        "ti",
98
        "tebi",
99
        "TEBI"
100
    ];
101

102
    /**
103
     * @return string
104
     */
105 0
    public function __toString()
106
    {
107 0
        $buf = "{sizeselector value: ";
108 0
        $buf .= $this->sizelimit;
109 0
        $buf .= "compare: ";
110 0
        if ($this->cmp === 0) {
111 0
            $buf .= "less";
112 0
        } elseif ($this->cmp === 1) {
113 0
            $buf .= "more";
114
        } else {
115 0
            $buf .= "equal";
116
        }
117 0
        $buf .= "}";
118

119 0
        return $buf;
120
    }
121

122
    /**
123
     * A size selector needs to know what size to base its selecting on.
124
     * This will be further modified by the multiplier to get an
125
     * actual size limit.
126
     *
127
     * @param int $size the size to select against expressed in units
128
     *
129
     * @return void
130
     */
131 0
    public function setValue($size)
132
    {
133 0
        $this->size = $size;
134 0
        if (($this->multiplier !== 0) && ($this->size > -1)) {
135 0
            $this->sizelimit = $size * $this->multiplier;
136
        }
137
    }
138

139
    /**
140
     * Sets the units to use for the comparison. This is a little
141
     * complicated because common usage has created standards that
142
     * play havoc with capitalization rules. Thus, some people will
143
     * use "K" for indicating 1000's, when the SI standard calls for
144
     * "k". Others have tried to introduce "K" as a multiple of 1024,
145
     * but that falls down when you reach "M", since "m" is already
146
     * defined as 0.001.
147
     * <p>
148
     * To get around this complexity, a number of standards bodies
149
     * have proposed the 2^10 standard, and at least one has adopted
150
     * it. But we are still left with a populace that isn't clear on
151
     * how capitalization should work.
152
     * <p>
153
     * We therefore ignore capitalization as much as possible.
154
     * Completely mixed case is not possible, but all upper and lower
155
     * forms are accepted for all long and short forms. Since we have
156
     * no need to work with the 0.001 case, this practice works here.
157
     * <p>
158
     * This function translates all the long and short forms that a
159
     * unit prefix can occur in and translates them into a single
160
     * multiplier.
161
     *
162
     * @param array $units The units to compare the size to.
163
     *
164
     * @return void
165
     */
166 0
    public function setUnits($units)
167
    {
168 0
        $i = array_search($units, self::$byteUnits, true);
169 0
        if ($i === false) {
170 0
            $i = -1;
171
        } // make it java-like
172

173 0
        $this->multiplier = 0;
174 0
        if (($i > -1) && ($i < 4)) {
175 0
            $this->multiplier = 1000;
176 0
        } elseif (($i > 3) && ($i < 9)) {
177 0
            $this->multiplier = 1024;
178 0
        } elseif (($i > 8) && ($i < 13)) {
179 0
            $this->multiplier = 1000000;
180 0
        } elseif (($i > 12) && ($i < 18)) {
181 0
            $this->multiplier = 1048576;
182 0
        } elseif (($i > 17) && ($i < 22)) {
183 0
            $this->multiplier = 1000000000;
184 0
        } elseif (($i > 21) && ($i < 27)) {
185 0
            $this->multiplier = 1073741824;
186 0
        } elseif (($i > 26) && ($i < 31)) {
187 0
            $this->multiplier = 1000000000000;
188 0
        } elseif (($i > 30) && ($i < 36)) {
189 0
            $this->multiplier = 1099511627776;
190
        }
191 0
        if (($this->multiplier > 0) && ($this->size > -1)) {
192 0
            $this->sizelimit = $this->size * $this->multiplier;
193
        }
194
    }
195

196
    /**
197
     * This specifies when the file should be selected, whether it be
198
     * when the file matches a particular size, when it is smaller,
199
     * or whether it is larger.
200
     *
201
     * @param array $cmp The comparison to perform, an EnumeratedAttribute
202
     *
203
     * @return void
204
     */
205 0
    public function setWhen($cmp)
206
    {
207 0
        $c = array_search($cmp, self::$sizeComparisons, true);
208 0
        if ($c !== false) {
209 0
            $this->cmp = $c;
210
        }
211
    }
212

213
    /**
214
     * When using this as a custom selector, this method will be called.
215
     * It translates each parameter into the appropriate setXXX() call.
216
     *
217
     * {@inheritdoc}
218
     *
219
     * @param array $parameters the complete set of parameters for this selector
220
     *
221
     * @return void
222
     *
223
     * @throws BuildException
224
     */
225 0
    public function setParameters(array $parameters): void
226
    {
227 0
        parent::setParameters($parameters);
228 0
        if ($parameters !== null) {
229 0
            for ($i = 0, $size = count($parameters); $i < $size; $i++) {
230 0
                $paramname = $parameters[$i]->getName();
231 0
                switch (strtolower($paramname)) {
232
                    case self::SIZE_KEY:
233
                        try {
234 0
                            $this->setValue($parameters[$i]->getValue());
235 0
                        } catch (Exception $nfe) {
236 0
                            $this->setError(
237
                                "Invalid size setting "
238 0
                                . $parameters[$i]->getValue()
239
                            );
240
                        }
241 0
                        break;
242
                    case self::UNITS_KEY:
243 0
                        $this->setUnits($parameters[$i]->getValue());
244 0
                        break;
245
                    case self::WHEN_KEY:
246 0
                        $this->setWhen($parameters[$i]->getValue());
247 0
                        break;
248
                    default:
249 0
                        $this->setError("Invalid parameter " . $paramname);
250
                }
251
            }
252
        }
253
    }
254

255
    /**
256
     * <p>Checks to make sure all settings are kosher. In this case, it
257
     * means that the size attribute has been set (to a positive value),
258
     * that the multiplier has a valid setting, and that the size limit
259
     * is valid. Since the latter is a calculated value, this can only
260
     * fail due to a programming error.
261
     * </p>
262
     * <p>If a problem is detected, the setError() method is called.
263
     * </p>
264
     *
265
     * {@inheritdoc}
266
     *
267
     * @return void
268
     */
269 0
    public function verifySettings()
270
    {
271 0
        if ($this->size < 0) {
272 0
            $this->setError("The value attribute is required, and must be positive");
273 0
        } elseif ($this->multiplier < 1) {
274 0
            $this->setError("Invalid Units supplied, must be K,Ki,M,Mi,G,Gi,T,or Ti");
275 0
        } elseif ($this->sizelimit < 0) {
276 0
            $this->setError("Internal error: Code is not setting sizelimit correctly");
277
        }
278
    }
279

280
    /**
281
     * The heart of the matter. This is where the selector gets to decide
282
     * on the inclusion of a file in a particular fileset.
283
     *
284
     * {@inheritdoc}
285
     *
286
     * @param PhingFile $basedir A PhingFile object for the base directory
287
     * @param string $filename The name of the file to check
288
     * @param PhingFile $file A PhingFile object for this filename
289
     *
290
     * @return bool whether the file should be selected or not
291
     */
292 0
    public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
293
    {
294 0
        $this->validate();
295

296
        // Directory size never selected for
297 0
        if ($file->isDirectory()) {
298 0
            return true;
299
        }
300 0
        if ($this->cmp === 0) {
301 0
            return ($file->length() < $this->sizelimit);
302
        }
303

304 0
        if ($this->cmp === 1) {
305 0
            return ($file->length() > $this->sizelimit);
306
        }
307

308 0
        return ($file->length() === $this->sizelimit);
309
    }
310
}

Read our documentation on viewing source code .

Loading