Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Finder\Comparator;
13
 
14
/**
15
 * NumberComparator compiles a simple comparison to an anonymous
16
 * subroutine, which you can call with a value to be tested again.
17
 *
18
 * Now this would be very pointless, if NumberCompare didn't understand
19
 * magnitudes.
20
 *
21
 * The target value may use magnitudes of kilobytes (k, ki),
22
 * megabytes (m, mi), or gigabytes (g, gi).  Those suffixed
23
 * with an i use the appropriate 2**n version in accordance with the
24
 * IEC standard: http://physics.nist.gov/cuu/Units/binary.html
25
 *
26
 * Based on the Perl Number::Compare module.
27
 *
28
 * @author    Fabien Potencier <fabien@symfony.com> PHP port
29
 * @author    Richard Clamp <richardc@unixbeard.net> Perl version
30
 * @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
31
 * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
32
 *
33
 * @see http://physics.nist.gov/cuu/Units/binary.html
34
 */
35
class NumberComparator extends Comparator
36
{
37
    /**
688 lars 38
     * @param string|null $test A comparison string or null
148 lars 39
     *
40
     * @throws \InvalidArgumentException If the test is not understood
41
     */
42
    public function __construct(?string $test)
43
    {
44
        if (null === $test || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
45
            throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test ?? 'null'));
46
        }
47
 
48
        $target = $matches[2];
49
        if (!is_numeric($target)) {
50
            throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target));
51
        }
52
        if (isset($matches[3])) {
53
            // magnitude
54
            switch (strtolower($matches[3])) {
55
                case 'k':
56
                    $target *= 1000;
57
                    break;
58
                case 'ki':
59
                    $target *= 1024;
60
                    break;
61
                case 'm':
62
                    $target *= 1000000;
63
                    break;
64
                case 'mi':
65
                    $target *= 1024 * 1024;
66
                    break;
67
                case 'g':
68
                    $target *= 1000000000;
69
                    break;
70
                case 'gi':
71
                    $target *= 1024 * 1024 * 1024;
72
                    break;
73
            }
74
        }
75
 
76
        parent::__construct($target, $matches[1] ?: '==');
77
    }
78
}