ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
NumberComparator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use InvalidArgumentException;
24
31{
32 public function __construct(string $test)
33 {
34 if (!preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $test, $matches)) {
35 throw new InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $test));
36 }
37
38 $target = $matches[2];
39 if (!is_numeric($target)) {
40 throw new InvalidArgumentException(sprintf('Invalid number "%s".', $target));
41 }
42
43 if (isset($matches[3])) {
44 switch (strtolower($matches[3])) {
45 case 'k':
46 $target *= 1000;
47 break;
48
49 case 'ki':
50 $target *= 1024;
51 break;
52
53 case 'm':
54 $target *= 1_000_000;
55 break;
56
57 case 'mi':
58 $target *= 1024 * 1024;
59 break;
60
61 case 'g':
62 $target *= 1_000_000_000;
63 break;
64
65 case 'gi':
66 $target *= 1024 * 1024 * 1024;
67 break;
68 }
69 }
70
71 $this->setTarget((string) $target);
72 $this->setOperator($matches[1] ?? '==');
73 }
74}