2010-04-21 08:31:18 +01:00
|
|
|
<?php
|
|
|
|
|
2010-08-20 22:09:55 +01:00
|
|
|
namespace Symfony\Component\Finder\Comparator;
|
2010-04-21 08:31:18 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony framework.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2010-05-21 12:22:34 +01:00
|
|
|
* NumberComparator compiles a simple comparison to an anonymous
|
2010-04-21 08:31:18 +01:00
|
|
|
* subroutine, which you can call with a value to be tested again.
|
|
|
|
*
|
|
|
|
* Now this would be very pointless, if NumberCompare didn't understand
|
|
|
|
* magnitudes.
|
|
|
|
*
|
|
|
|
* The target value may use magnitudes of kilobytes (k, ki),
|
|
|
|
* megabytes (m, mi), or gigabytes (g, gi). Those suffixed
|
|
|
|
* with an i use the appropriate 2**n version in accordance with the
|
|
|
|
* IEC standard: http://physics.nist.gov/cuu/Units/binary.html
|
|
|
|
*
|
|
|
|
* Based on the Perl Number::Compare module.
|
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com> PHP port
|
|
|
|
* @author Richard Clamp <richardc@unixbeard.net> Perl version
|
|
|
|
* @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
* @copyright 2002 Richard Clamp <richardc@unixbeard.net>
|
|
|
|
* @see http://physics.nist.gov/cuu/Units/binary.html
|
|
|
|
*/
|
2010-05-21 11:50:13 +01:00
|
|
|
class NumberComparator extends Comparator
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param string $test A comparison string
|
2010-05-21 11:50:13 +01:00
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException If the test is not understood
|
2010-05-06 12:25:53 +01:00
|
|
|
*/
|
|
|
|
public function __construct($test)
|
2010-04-21 08:31:18 +01:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!preg_match('#^\s*([<>=]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
|
2010-05-21 11:50:13 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-04-21 08:31:18 +01:00
|
|
|
|
2010-05-21 11:50:13 +01:00
|
|
|
$target = $matches[2];
|
2010-05-06 12:25:53 +01:00
|
|
|
$magnitude = strtolower(isset($matches[3]) ? $matches[3] : '');
|
2010-05-07 15:09:11 +01:00
|
|
|
switch ($magnitude) {
|
2010-05-06 12:25:53 +01:00
|
|
|
case 'k':
|
2010-05-21 11:50:13 +01:00
|
|
|
$target *= 1000;
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
case 'ki':
|
2010-05-21 11:50:13 +01:00
|
|
|
$target *= 1024;
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
case 'm':
|
2010-05-21 11:50:13 +01:00
|
|
|
$target *= 1000000;
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
case 'mi':
|
2010-05-21 11:50:13 +01:00
|
|
|
$target *= 1024*1024;
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
case 'g':
|
2010-05-21 11:50:13 +01:00
|
|
|
$target *= 1000000000;
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
case 'gi':
|
2010-05-21 11:50:13 +01:00
|
|
|
$target *= 1024*1024*1024;
|
2010-05-06 12:25:53 +01:00
|
|
|
break;
|
|
|
|
}
|
2010-04-21 08:31:18 +01:00
|
|
|
|
2010-05-21 11:50:13 +01:00
|
|
|
$this->setTarget($target);
|
|
|
|
$this->setOperator(isset($matches[1]) ? $matches[1] : '==');
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-04-21 08:31:18 +01:00
|
|
|
}
|