Skip to content
Newer
Older
100644 83 lines (77 sloc) 2.53 KB
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
1 <?php
2
3 /*
782e089 @blue-eyes normalized license messages in PHP files
blue-eyes authored Jan 15, 2011
4 * This file is part of the Symfony package.
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
5 *
db96c6a @fabpot replaced symfony-project.org by symfony.com
fabpot authored Mar 6, 2011
6 * (c) Fabien Potencier <fabien@symfony.com>
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
7 *
782e089 @blue-eyes normalized license messages in PHP files
blue-eyes authored Jan 15, 2011
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
10 */
11
782e089 @blue-eyes normalized license messages in PHP files
blue-eyes authored Jan 15, 2011
12 namespace Symfony\Component\Finder\Comparator;
13
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
14 /**
15 * NumberComparator compiles a simple comparison to an anonymous
16 * subroutine, which you can call with a value to be tested again.
17 *
18 * Now this would be very pointless, if NumberCompare didn't understand
19 * magnitudes.
20 *
21 * The target value may use magnitudes of kilobytes (k, ki),
22 * megabytes (m, mi), or gigabytes (g, gi). Those suffixed
23 * with an i use the appropriate 2**n version in accordance with the
24 * IEC standard: http://physics.nist.gov/cuu/Units/binary.html
25 *
26 * Based on the Perl Number::Compare module.
27 *
234be8a @fabpot [DoctrineBridge] fixed some CS
fabpot authored Dec 13, 2011
28 * @author Fabien Potencier <fabien@symfony.com> PHP port
29 * @author Richard Clamp <richardc@unixbeard.net> Perl version
30 *
31 * @copyright 2004-2005 Fabien Potencier <fabien@symfony.com>
32 * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
33 *
34 * @see http://physics.nist.gov/cuu/Units/binary.html
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
35 */
36 class NumberComparator extends Comparator
37 {
38 /**
39 * Constructor.
40 *
41 * @param string $test A comparison string
42 *
43 * @throws \InvalidArgumentException If the test is not understood
44 */
45 public function __construct($test)
46 {
3ea903f @gajdaw [Finder][Comparator] not equal operator
gajdaw authored Apr 18, 2012
47 if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
48 throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
49 }
50
51 $target = $matches[2];
3ea903f @gajdaw [Finder][Comparator] not equal operator
gajdaw authored Apr 18, 2012
52 if (!is_numeric($target)) {
53 throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target));
54 }
bdef5dc @fabpot [Finder] bypassed some code when possible
fabpot authored Nov 5, 2011
55 if (isset($matches[3])) {
56 // magnitude
57 switch (strtolower($matches[3])) {
58 case 'k':
59 $target *= 1000;
60 break;
61 case 'ki':
62 $target *= 1024;
63 break;
64 case 'm':
65 $target *= 1000000;
66 break;
67 case 'mi':
68 $target *= 1024*1024;
69 break;
70 case 'g':
71 $target *= 1000000000;
72 break;
73 case 'gi':
74 $target *= 1024*1024*1024;
75 break;
76 }
8e50d17 @fabpot renamed Symfony\Components to Symfony\Component
fabpot authored Aug 20, 2010
77 }
78
79 $this->setTarget($target);
80 $this->setOperator(isset($matches[1]) ? $matches[1] : '==');
81 }
82 }
Something went wrong with that request. Please try again.