ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
NumberComparator.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types=1);
3 
5 
12 {
17  public function __construct(string $test)
18  {
19  if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
20  throw new \InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
21  }
22 
23  $target = $matches[2];
24  if (!is_numeric($target)) {
25  throw new \InvalidArgumentException(sprintf('Invalid number "%s".', $target));
26  }
27 
28  if (isset($matches[3])) {
29  // magnitude
30  switch (strtolower($matches[3])) {
31  case 'k':
32  $target *= 1000;
33  break;
34 
35  case 'ki':
36  $target *= 1024;
37  break;
38 
39  case 'm':
40  $target *= 1000000;
41  break;
42 
43  case 'mi':
44  $target *= 1024 * 1024;
45  break;
46 
47  case 'g':
48  $target *= 1000000000;
49  break;
50 
51  case 'gi':
52  $target *= 1024 * 1024 * 1024;
53  break;
54  }
55  }
56 
57  $this->setTarget((string) $target);
58  $this->setOperator($matches[1] ?? '==');
59  }
60 }
__construct(string $test)
NumberComparator constructor.
$test
Definition: Utf8Test.php:84