| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Finder\Comparator; |
| 13 |
|
| 14 |
/** |
| 15 |
* @author Fabien Potencier <[email protected]> |
| 16 |
*/ |
| 17 |
class Comparator |
| 18 |
{ |
| 19 |
private string $operator; |
| 20 |
|
| 21 |
public function __construct( |
| 22 |
private string $target, |
| 23 |
string $operator = '==', |
| 24 |
) { |
| 25 |
if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='], true)) { |
| 26 |
throw new \InvalidArgumentException(\sprintf('Invalid operator "%s".', $operator)); |
| 27 |
} |
| 28 |
|
| 29 |
$this->operator = $operator; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Gets the target value. |
| 34 |
*/ |
| 35 |
public function getTarget(): string |
| 36 |
{ |
| 37 |
return $this->target; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Gets the comparison operator. |
| 42 |
*/ |
| 43 |
public function getOperator(): string |
| 44 |
{ |
| 45 |
return $this->operator; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Tests against the target. |
| 50 |
*/ |
| 51 |
public function test(mixed $test): bool |
| 52 |
{ |
| 53 |
return match ($this->operator) { |
| 54 |
'>' => $test > $this->target, |
| 55 |
'>=' => $test >= $this->target, |
| 56 |
'<' => $test < $this->target, |
| 57 |
'<=' => $test <= $this->target, |
| 58 |
'!=' => $test != $this->target, |
| 59 |
default => $test == $this->target, |
| 60 |
}; |
| 61 |
} |
| 62 |
} |