Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Finder\Comparator; Chris@0: Chris@0: /** Chris@0: * NumberComparator compiles a simple comparison to an anonymous Chris@0: * subroutine, which you can call with a value to be tested again. Chris@0: * Chris@0: * Now this would be very pointless, if NumberCompare didn't understand Chris@0: * magnitudes. Chris@0: * Chris@0: * The target value may use magnitudes of kilobytes (k, ki), Chris@0: * megabytes (m, mi), or gigabytes (g, gi). Those suffixed Chris@0: * with an i use the appropriate 2**n version in accordance with the Chris@0: * IEC standard: http://physics.nist.gov/cuu/Units/binary.html Chris@0: * Chris@0: * Based on the Perl Number::Compare module. Chris@0: * Chris@0: * @author Fabien Potencier PHP port Chris@0: * @author Richard Clamp Perl version Chris@0: * @copyright 2004-2005 Fabien Potencier Chris@0: * @copyright 2002 Richard Clamp Chris@0: * Chris@0: * @see http://physics.nist.gov/cuu/Units/binary.html Chris@0: */ Chris@0: class NumberComparator extends Comparator Chris@0: { Chris@0: /** Chris@0: * @param string|int $test A comparison string or an integer Chris@0: * Chris@0: * @throws \InvalidArgumentException If the test is not understood Chris@0: */ Chris@0: public function __construct($test) Chris@0: { Chris@0: if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) { Chris@0: throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test)); Chris@0: } Chris@0: Chris@0: $target = $matches[2]; Chris@0: if (!is_numeric($target)) { Chris@0: throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target)); Chris@0: } Chris@0: if (isset($matches[3])) { Chris@0: // magnitude Chris@0: switch (strtolower($matches[3])) { Chris@0: case 'k': Chris@0: $target *= 1000; Chris@0: break; Chris@0: case 'ki': Chris@0: $target *= 1024; Chris@0: break; Chris@0: case 'm': Chris@0: $target *= 1000000; Chris@0: break; Chris@0: case 'mi': Chris@0: $target *= 1024 * 1024; Chris@0: break; Chris@0: case 'g': Chris@0: $target *= 1000000000; Chris@0: break; Chris@0: case 'gi': Chris@0: $target *= 1024 * 1024 * 1024; Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: $this->setTarget($target); Chris@0: $this->setOperator(isset($matches[1]) ? $matches[1] : '=='); Chris@0: } Chris@0: }