63 lines
1.4 KiB
PHP
Raw Normal View History

2025-10-16 21:07:43 +08:00
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Finder\Comparator;
/**
* @author Fabien Potencier <fabien@symfony.com>
*/
class Comparator
{
2025-10-20 09:23:30 +08:00
private string $operator;
2025-10-16 21:07:43 +08:00
2025-10-20 09:23:30 +08:00
public function __construct(
private string $target,
string $operator = '==',
) {
if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
throw new \InvalidArgumentException(\sprintf('Invalid operator "%s".', $operator));
2025-10-16 21:07:43 +08:00
}
2025-10-20 09:23:30 +08:00
$this->operator = $operator;
2025-10-16 21:07:43 +08:00
}
/**
* Gets the target value.
*/
2025-10-20 09:23:30 +08:00
public function getTarget(): string
2025-10-16 21:07:43 +08:00
{
return $this->target;
}
/**
* Gets the comparison operator.
*/
2025-10-20 09:23:30 +08:00
public function getOperator(): string
2025-10-16 21:07:43 +08:00
{
return $this->operator;
}
/**
* Tests against the target.
*/
2025-10-20 09:23:30 +08:00
public function test(mixed $test): bool
2025-10-16 21:07:43 +08:00
{
2025-10-20 09:23:30 +08:00
return match ($this->operator) {
'>' => $test > $this->target,
'>=' => $test >= $this->target,
'<' => $test < $this->target,
'<=' => $test <= $this->target,
'!=' => $test != $this->target,
default => $test == $this->target,
};
2025-10-16 21:07:43 +08:00
}
}