annotate vendor/symfony/finder/Comparator/NumberComparator.php @ 19:fa3358dc1485 tip

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