| [ Index ] |
|
Code source de Symfony 1.0.0 |
1 <?php 2 3 /** 4 * @package pake 5 * @author Fabien Potencier <fabien.potencier@symfony-project.com> php port 6 * @author Richard Clamp <richardc@unixbeard.net> perl version 7 * @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com> 8 * @copyright 2002 Richard Clamp <richardc@unixbeard.net> 9 * @license see the LICENSE file included in the distribution 10 * @version SVN: $Id: pakeNumberCompare.class.php 1791 2006-08-23 21:17:06Z fabien $ 11 */ 12 13 if (class_exists('pakeNumberCompare')) 14 { 15 return; 16 } 17 18 /** 19 * 20 * Numeric comparisons. 21 * 22 * sfNumberCompare compiles a simple comparison to an anonymous 23 * subroutine, which you can call with a value to be tested again. 24 25 * Now this would be very pointless, if sfNumberCompare didn't understand 26 * magnitudes. 27 28 * The target value may use magnitudes of kilobytes (C<k>, C<ki>), 29 * megabytes (C<m>, C<mi>), or gigabytes (C<g>, C<gi>). Those suffixed 30 * with an C<i> use the appropriate 2**n version in accordance with the 31 * IEC standard: http://physics.nist.gov/cuu/Units/binary.html 32 * 33 * based on perl Number::Compare module. 34 * 35 * @package pake 36 * @author Fabien Potencier <fabien.potencier@symfony-project.com> php port 37 * @author Richard Clamp <richardc@unixbeard.net> perl version 38 * @copyright 2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com> 39 * @copyright 2002 Richard Clamp <richardc@unixbeard.net> 40 * @see http://physics.nist.gov/cuu/Units/binary.html 41 * @license see the LICENSE file included in the distribution 42 * @version SVN: $Id: pakeNumberCompare.class.php 1791 2006-08-23 21:17:06Z fabien $ 43 */ 44 class pakeNumberCompare 45 { 46 private $test = ''; 47 48 public function __construct($test) 49 { 50 $this->test = $test; 51 } 52 53 public function test($number) 54 { 55 if (!preg_match('{^([<>]=?)?(.*?)([kmg]i?)?$}i', $this->test, $matches)) 56 { 57 throw new pakeException(sprintf('Don\'t understand "%s" as a test.', $this->test)); 58 } 59 60 $target = array_key_exists(2, $matches) ? $matches[2] : ''; 61 $magnitude = array_key_exists(3, $matches) ? $matches[3] : ''; 62 if (strtolower($magnitude) == 'k') $target *= 1000; 63 if (strtolower($magnitude) == 'ki') $target *= 1024; 64 if (strtolower($magnitude) == 'm') $target *= 1000000; 65 if (strtolower($magnitude) == 'mi') $target *= 1024*1024; 66 if (strtolower($magnitude) == 'g') $target *= 1000000000; 67 if (strtolower($magnitude) == 'gi') $target *= 1024*1024*1024; 68 69 $comparison = array_key_exists(1, $matches) ? $matches[1] : '=='; 70 if ($comparison == '==' || $comparison == '') 71 { 72 return ($number == $target); 73 } 74 else if ($comparison == '>') 75 { 76 return ($number > $target); 77 } 78 else if ($comparison == '>=') 79 { 80 return ($number >= $target); 81 } 82 else if ($comparison == '<') 83 { 84 return ($number < $target); 85 } 86 else if ($comparison == '<=') 87 { 88 return ($number <= $target); 89 } 90 91 return false; 92 } 93 } 94 95 /* 96 =head1 SYNOPSIS 97 98 Number::Compare->new(">1Ki")->test(1025); # is 1025 > 1024 99 100 my $c = Number::Compare->new(">1M"); 101 $c->(1_200_000); # slightly terser invocation 102 103 =head1 DESCRIPTION 104 105 106 =head1 METHODS 107 108 =head2 ->new( $test ) 109 110 Returns a new object that compares the specified test. 111 112 =head2 ->test( $value ) 113 114 A longhanded version of $compare->( $value ). Predates blessed 115 subroutine reference implementation. 116 117 =head2 ->parse_to_perl( $test ) 118 119 Returns a perl code fragment equivalent to the test. 120 */
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
| Généré le : Fri Mar 16 22:42:14 2007 | par Balluche grâce à PHPXref 0.7 |