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