Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Finder\Comparator; Chris@0: Chris@0: /** Chris@0: * Comparator. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class Comparator Chris@0: { Chris@0: private $target; Chris@0: private $operator = '=='; Chris@0: Chris@0: /** Chris@0: * Gets the target value. Chris@0: * Chris@0: * @return string The target value Chris@0: */ Chris@0: public function getTarget() Chris@0: { Chris@0: return $this->target; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the target value. Chris@0: * Chris@0: * @param string $target The target value Chris@0: */ Chris@0: public function setTarget($target) Chris@0: { Chris@0: $this->target = $target; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the comparison operator. Chris@0: * Chris@0: * @return string The operator Chris@0: */ Chris@0: public function getOperator() Chris@0: { Chris@0: return $this->operator; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the comparison operator. Chris@0: * Chris@0: * @param string $operator A valid operator Chris@0: * Chris@0: * @throws \InvalidArgumentException Chris@0: */ Chris@0: public function setOperator($operator) Chris@0: { Chris@0: if (!$operator) { Chris@0: $operator = '=='; Chris@0: } Chris@0: Chris@17: if (!\in_array($operator, ['>', '<', '>=', '<=', '==', '!='])) { Chris@0: throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator)); Chris@0: } Chris@0: Chris@0: $this->operator = $operator; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests against the target. Chris@0: * Chris@0: * @param mixed $test A test value Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function test($test) Chris@0: { Chris@0: switch ($this->operator) { Chris@0: case '>': Chris@0: return $test > $this->target; Chris@0: case '>=': Chris@0: return $test >= $this->target; Chris@0: case '<': Chris@0: return $test < $this->target; Chris@0: case '<=': Chris@0: return $test <= $this->target; Chris@0: case '!=': Chris@0: return $test != $this->target; Chris@0: } Chris@0: Chris@0: return $test == $this->target; Chris@0: } Chris@0: }