Files
swiftadmin/vendor/symfony/finder/Comparator/Comparator.php

69 lines
1.5 KiB
PHP
Raw Normal View History

2022-08-19 19:48:37 +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
{
2022-11-28 19:11:12 +08:00
private string $target;
private string $operator;
2022-08-19 19:48:37 +08:00
2022-11-28 19:11:12 +08:00
public function __construct(string $target, string $operator = '==')
2022-08-19 19:48:37 +08:00
{
2022-11-28 19:11:12 +08:00
if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) {
throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
2022-08-19 19:48:37 +08:00
}
$this->target = $target;
2022-11-28 19:11:12 +08:00
$this->operator = $operator;
2022-08-19 19:48:37 +08:00
}
/**
* Gets the target value.
*/
2022-11-28 19:11:12 +08:00
public function getTarget(): string
2022-08-19 19:48:37 +08:00
{
return $this->target;
}
/**
* Gets the comparison operator.
*/
2022-11-28 19:11:12 +08:00
public function getOperator(): string
2022-08-19 19:48:37 +08:00
{
return $this->operator;
}
/**
* Tests against the target.
*/
2022-11-28 19:11:12 +08:00
public function test(mixed $test): bool
2022-08-19 19:48:37 +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;
}
}