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