ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
NumberComparator.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
8 
9 /******************************************************************************
10  *
11  * This file is part of ILIAS, a powerful learning management system.
12  *
13  * ILIAS is licensed with the GPL-3.0, you should have received a copy
14  * of said license along with the source code.
15  *
16  * If this is not the case or you just want to try ILIAS, you'll find
17  * us at:
18  * https://www.ilias.de
19  * https://github.com/ILIAS-eLearning
20  *
21  *****************************************************************************/
22 
29 {
30  public function __construct(string $test)
31  {
32  if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
33  throw new InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
34  }
35 
36  $target = $matches[2];
37  if (!is_numeric($target)) {
38  throw new InvalidArgumentException(sprintf('Invalid number "%s".', $target));
39  }
40 
41  if (isset($matches[3])) {
42  switch (strtolower($matches[3])) {
43  case 'k':
44  $target *= 1000;
45  break;
46 
47  case 'ki':
48  $target *= 1024;
49  break;
50 
51  case 'm':
52  $target *= 1_000_000;
53  break;
54 
55  case 'mi':
56  $target *= 1024 * 1024;
57  break;
58 
59  case 'g':
60  $target *= 1_000_000_000;
61  break;
62 
63  case 'gi':
64  $target *= 1024 * 1024 * 1024;
65  break;
66  }
67  }
68 
69  $this->setTarget((string) $target);
70  $this->setOperator($matches[1] ?? '==');
71  }
72 }