4f5e1ffe1239fb47e9df2e4352b2de0231459d4d
[yaffs-website] / Comparator.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
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  * Comparator.
16  *
17  * @author Fabien Potencier <fabien@symfony.com>
18  */
19 class Comparator
20 {
21     private $target;
22     private $operator = '==';
23
24     /**
25      * Gets the target value.
26      *
27      * @return string The target value
28      */
29     public function getTarget()
30     {
31         return $this->target;
32     }
33
34     /**
35      * Sets the target value.
36      *
37      * @param string $target The target value
38      */
39     public function setTarget($target)
40     {
41         $this->target = $target;
42     }
43
44     /**
45      * Gets the comparison operator.
46      *
47      * @return string The operator
48      */
49     public function getOperator()
50     {
51         return $this->operator;
52     }
53
54     /**
55      * Sets the comparison operator.
56      *
57      * @param string $operator A valid operator
58      *
59      * @throws \InvalidArgumentException
60      */
61     public function setOperator($operator)
62     {
63         if (!$operator) {
64             $operator = '==';
65         }
66
67         if (!in_array($operator, array('>', '<', '>=', '<=', '==', '!='))) {
68             throw new \InvalidArgumentException(sprintf('Invalid operator "%s".', $operator));
69         }
70
71         $this->operator = $operator;
72     }
73
74     /**
75      * Tests against the target.
76      *
77      * @param mixed $test A test value
78      *
79      * @return bool
80      */
81     public function test($test)
82     {
83         switch ($this->operator) {
84             case '>':
85                 return $test > $this->target;
86             case '>=':
87                 return $test >= $this->target;
88             case '<':
89                 return $test < $this->target;
90             case '<=':
91                 return $test <= $this->target;
92             case '!=':
93                 return $test != $this->target;
94         }
95
96         return $test == $this->target;
97     }
98 }