ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
NumberComparator.php
Go to the documentation of this file.
1<?php
2declare(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}
$test
Definition: Utf8Test.php:84
An exception for terminatinating execution or to throw for unit testing.
__construct(string $test)
NumberComparator constructor.