1
|
|
<?php
|
2
|
|
/**
|
3
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
4
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
5
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
6
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
7
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
8
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
9
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
10
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
11
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
12
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
13
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
14
|
|
*
|
15
|
|
* This software consists of voluntary contributions made by many individuals
|
16
|
|
* and is licensed under the LGPL. For more information please see
|
17
|
|
* <http://phing.info>.
|
18
|
|
*/
|
19
|
|
|
20
|
|
/**
|
21
|
|
* This selector is here just to shake up your thinking a bit. Don't get
|
22
|
|
* too caught up in boolean, there are other ways you can evaluate a
|
23
|
|
* collection of selectors. This one takes a vote of the selectors it
|
24
|
|
* contains, and majority wins. You could also have an "all-but-one"
|
25
|
|
* selector, a "weighted-average" selector, and so on. These are left
|
26
|
|
* as exercises for the reader (as are the usecases where this would
|
27
|
|
* be necessary).
|
28
|
|
*
|
29
|
|
* @author Hans Lellelid <hans@xmpl.org> (Phing)
|
30
|
|
* @author Bruce Atherton <bruce@callenish.com> (Ant)
|
31
|
|
* @package phing.types.selectors
|
32
|
|
*/
|
33
|
|
class MajoritySelector extends BaseSelectorContainer
|
34
|
|
{
|
35
|
|
private $allowtie = true;
|
36
|
|
|
37
|
|
/**
|
38
|
|
* @return string
|
39
|
|
*/
|
40
|
0
|
public function __toString()
|
41
|
|
{
|
42
|
0
|
$buf = "";
|
43
|
0
|
if ($this->hasSelectors()) {
|
44
|
0
|
$buf .= "{majorityselect: ";
|
45
|
0
|
$buf .= parent::__toString();
|
46
|
0
|
$buf .= "}";
|
47
|
|
}
|
48
|
|
|
49
|
0
|
return $buf;
|
50
|
|
}
|
51
|
|
|
52
|
|
/**
|
53
|
|
* @param $tiebreaker
|
54
|
|
*/
|
55
|
0
|
public function setAllowtie($tiebreaker)
|
56
|
|
{
|
57
|
0
|
$this->allowtie = $tiebreaker;
|
58
|
|
}
|
59
|
|
|
60
|
|
/**
|
61
|
|
* Returns true (the file is selected) if most of the other selectors
|
62
|
|
* agree. In case of a tie, go by the allowtie setting. That defaults
|
63
|
|
* to true, meaning in case of a tie, the file is selected.
|
64
|
|
*
|
65
|
|
* @param PhingFile $basedir the base directory the scan is being done from
|
66
|
|
* @param string $filename is the name of the file to check
|
67
|
|
* @param PhingFile $file is a PhingFile object for the filename that the selector
|
68
|
|
* can use
|
69
|
|
* @return bool whether the file should be selected or not
|
70
|
|
*/
|
71
|
0
|
public function isSelected(PhingFile $basedir, $filename, PhingFile $file)
|
72
|
|
{
|
73
|
0
|
$this->validate();
|
74
|
|
|
75
|
0
|
$yesvotes = 0;
|
76
|
0
|
$novotes = 0;
|
77
|
|
|
78
|
0
|
$selectors = $this->selectorElements();
|
79
|
0
|
for ($i = 0, $size = count($selectors); $i < $size; $i++) {
|
80
|
0
|
$result = $selectors[$i]->isSelected($basedir, $filename, $file);
|
81
|
0
|
if ($result) {
|
82
|
0
|
++$yesvotes;
|
83
|
|
} else {
|
84
|
0
|
++$novotes;
|
85
|
|
}
|
86
|
|
}
|
87
|
0
|
if ($yesvotes > $novotes) {
|
88
|
0
|
return true;
|
89
|
|
}
|
90
|
|
|
91
|
0
|
if ($novotes > $yesvotes) {
|
92
|
0
|
return false;
|
93
|
|
}
|
94
|
|
// At this point, we know we have a tie.
|
95
|
0
|
return $this->allowtie;
|
96
|
|
}
|
97
|
|
}
|