69 lines
1.5 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-11-14 15:54:35 +08:00
private string $target;
2025-10-20 09:23:30 +08:00
private string $operator;
2025-10-16 21:07:43 +08:00
2025-11-14 15:54:35 +08:00
public function __construct(string $target, string $operator = '==')
{
2025-10-20 09:23:30 +08:00
if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
2025-11-14 15:54:35 +08:00
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
2025-10-16 21:07:43 +08:00
}
2025-11-14 15:54:35 +08:00
$this->target = $target;
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-11-14 15:54:35 +08:00
switch ($this->operator) {
case '>':
return $test > $this->target;
case '>=':
return $test >= $this->target;
case '<':
return $test < $this->target;
case '<=':
return $test <= $this->target;
case '!=':
return $test != $this->target;
}
return $test == $this->target;
2025-10-16 21:07:43 +08:00
}
}