Comparator.php
4 years ago
DateComparator.php
4 years ago
NumberComparator.php
4 years ago
index.php
3 years ago
NumberComparator.php
42 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Symfony\Component\Finder\Comparator; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | class NumberComparator extends Comparator |
| 5 | { |
| 6 | public function __construct(?string $test) |
| 7 | { |
| 8 | if (null === $test || !\preg_match('#^\\s*(==|!=|[<>]=?)?\\s*([0-9\\.]+)\\s*([kmg]i?)?\\s*$#i', $test, $matches)) { |
| 9 | throw new \InvalidArgumentException(\sprintf('Don\'t understand "%s" as a number test.', $test ?? 'null')); |
| 10 | } |
| 11 | $target = $matches[2]; |
| 12 | if (!\is_numeric($target)) { |
| 13 | throw new \InvalidArgumentException(\sprintf('Invalid number "%s".', $target)); |
| 14 | } |
| 15 | if (isset($matches[3])) { |
| 16 | // magnitude |
| 17 | switch (\strtolower($matches[3])) { |
| 18 | case 'k': |
| 19 | $target *= 1000; |
| 20 | break; |
| 21 | case 'ki': |
| 22 | $target *= 1024; |
| 23 | break; |
| 24 | case 'm': |
| 25 | $target *= 1000000; |
| 26 | break; |
| 27 | case 'mi': |
| 28 | $target *= 1024 * 1024; |
| 29 | break; |
| 30 | case 'g': |
| 31 | $target *= 1000000000; |
| 32 | break; |
| 33 | case 'gi': |
| 34 | $target *= 1024 * 1024 * 1024; |
| 35 | break; |
| 36 | } |
| 37 | } |
| 38 | $this->setTarget($target); |
| 39 | $this->setOperator($matches[1] ?? '=='); |
| 40 | } |
| 41 | } |
| 42 |