Drupal investigation

NumberComparator.php 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder\Comparator;
  11. /**
  12. * NumberComparator compiles a simple comparison to an anonymous
  13. * subroutine, which you can call with a value to be tested again.
  14. *
  15. * Now this would be very pointless, if NumberCompare didn't understand
  16. * magnitudes.
  17. *
  18. * The target value may use magnitudes of kilobytes (k, ki),
  19. * megabytes (m, mi), or gigabytes (g, gi). Those suffixed
  20. * with an i use the appropriate 2**n version in accordance with the
  21. * IEC standard: http://physics.nist.gov/cuu/Units/binary.html
  22. *
  23. * Based on the Perl Number::Compare module.
  24. *
  25. * @author Fabien Potencier <fabien@symfony.com> PHP port
  26. * @author Richard Clamp <richardc@unixbeard.net> Perl version
  27. * @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
  28. * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
  29. *
  30. * @see http://physics.nist.gov/cuu/Units/binary.html
  31. */
  32. class NumberComparator extends Comparator
  33. {
  34. /**
  35. * Constructor.
  36. *
  37. * @param string|int $test A comparison string or an integer
  38. *
  39. * @throws \InvalidArgumentException If the test is not understood
  40. */
  41. public function __construct($test)
  42. {
  43. if (!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));
  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. $this->setTarget($target);
  74. $this->setOperator(isset($matches[1]) ? $matches[1] : '==');
  75. }
  76. }