4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Finder\Comparator;
15 * NumberComparator compiles a simple comparison to an anonymous
16 * subroutine, which you can call with a value to be tested again.
18 * Now this would be very pointless, if NumberCompare didn't understand
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
26 * Based on the Perl Number::Compare module.
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>
33 * @see http://physics.nist.gov/cuu/Units/binary.html
35 class NumberComparator extends Comparator
40 * @param string|int $test A comparison string or an integer
42 * @throws \InvalidArgumentException If the test is not understood
44 public function __construct($test)
46 if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
47 throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
50 $target = $matches[2];
51 if (!is_numeric($target)) {
52 throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target));
54 if (isset($matches[3])) {
56 switch (strtolower($matches[3])) {
67 $target *= 1024 * 1024;
70 $target *= 1000000000;
73 $target *= 1024 * 1024 * 1024;
78 $this->setTarget($target);
79 $this->setOperator(isset($matches[1]) ? $matches[1] : '==');