|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Lenevor Framework |
|
5
|
|
|
* |
|
6
|
|
|
* LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the new BSD license that is bundled |
|
9
|
|
|
* with this package in the file license.md. |
|
10
|
|
|
* It is also available through the world-wide-web at this URL: |
|
11
|
|
|
* https://lenevor.com/license |
|
12
|
|
|
* If you did not receive a copy of the license and are unable to |
|
13
|
|
|
* obtain it through the world-wide-web, please send an email |
|
14
|
|
|
* to [email protected] so we can send you a copy immediately. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Lenevor |
|
17
|
|
|
* @subpackage Base |
|
18
|
|
|
* @link https://lenevor.com |
|
19
|
|
|
* @copyright Copyright (c) 2019 - 2023 Alexander Campo <[email protected]> |
|
20
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause New BSD license or see https://lenevor.com/license or see /license.md |
|
21
|
|
|
*/ |
|
22
|
|
|
|
|
23
|
|
|
namespace Syscodes\Components\Finder\Comparators; |
|
24
|
|
|
|
|
25
|
|
|
use InvalidArgumentException; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Allows realize compiles a simple comparison to an anonymous |
|
29
|
|
|
* subroutine with number. |
|
30
|
|
|
*/ |
|
31
|
|
|
class NumberComparator extends Comparator |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Constructor. Create a new NumberComparator instance, |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|null $value A comparison string or null |
|
37
|
|
|
* |
|
38
|
|
|
* @throws \InvalidArgumentException |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(?string $value) |
|
41
|
|
|
{ |
|
42
|
|
|
if (null === $value || !preg_match('#^\s*(==|!=|[<>]=?)?\s*([0-9\.]+)\s*([kmg]i?)?\s*$#i', $value, $matches)) { |
|
43
|
|
|
throw new InvalidArgumentException(sprintf('Don\'t understand "%s" as a number test.', $value ?? 'null')); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$target = $matches[2]; |
|
47
|
|
|
|
|
48
|
|
|
if ( ! is_numeric($target)) { |
|
49
|
|
|
throw new InvalidArgumentException(sprintf('Invalid number "%s"', $target)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
if (isset($matches[3])) { |
|
53
|
|
|
// magnitude |
|
54
|
|
|
switch (strtolower($matches[3])) { |
|
55
|
|
|
case 'k': |
|
56
|
|
|
$target *= 1000; |
|
57
|
|
|
break; |
|
58
|
|
|
case 'ki': |
|
59
|
|
|
$target *= 1024; |
|
60
|
|
|
break; |
|
61
|
|
|
case 'm': |
|
62
|
|
|
$target *= 1000000; |
|
63
|
|
|
break; |
|
64
|
|
|
case 'mi': |
|
65
|
|
|
$target *= 1024 * 1024; |
|
66
|
|
|
break; |
|
67
|
|
|
case 'g': |
|
68
|
|
|
$target *= 1000000000; |
|
69
|
|
|
break; |
|
70
|
|
|
case 'gi': |
|
71
|
|
|
$target *= 1024 * 1024 * 1024; |
|
72
|
|
|
break; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
parent::__construct($target, $matches[1] ?: '=='); |
|
77
|
|
|
} |
|
78
|
|
|
} |