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
 * PropertySelector Task
22
 *
23
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
24
 * @package phing.tasks.ext.property
25
 */
26
class PropertySelector extends AbstractPropertySetterTask
27
{
28
    /**
29
     * @var RegularExpression $match
30
     */
31
    private $match;
32
    private $select = "\\0";
33
    private $delim = ',';
34
    private $caseSensitive = true;
35
    private $distinct = false;
36

37 1
    public function setMatch($match)
38
    {
39 1
        $this->match = new RegularExpression();
40 1
        $this->match->setPattern($match);
41
    }
42

43 1
    public function setSelect($select)
44
    {
45 1
        $this->select = $select;
46
    }
47

48 1
    public function setCaseSensitive($caseSensitive)
49
    {
50 1
        $this->caseSensitive = $caseSensitive;
51
    }
52

53 1
    public function setDelimiter($delim)
54
    {
55 1
        $this->delim = $delim;
56
    }
57

58 0
    public function setDistinct($distinct)
59
    {
60 0
        $this->distinct = $distinct;
61
    }
62

63 1
    protected function validate()
64
    {
65 1
        parent::validate();
66 1
        if ($this->match == null) {
67 0
            throw new BuildException("No match expression specified.");
68
        }
69
    }
70

71 1
    public function main()
72
    {
73 1
        $this->validate();
74

75 1
        $regex = $this->match->getRegexp($this->project);
76 1
        $regex->setIgnoreCase(!$this->caseSensitive);
77 1
        $props = $this->project->getProperties();
78 1
        $e = array_keys($props);
79 1
        $buf = '';
80 1
        $cnt = 0;
81

82 1
        $used = [];
83

84 1
        foreach ($e as $key) {
85 1
            if ($regex->matches($key)) {
86 1
                $output = $this->select;
87 1
                $groups = $regex->getGroups();
88 1
                $sz = count($groups);
89 1
                for ($i = 0; $i < $sz; $i++) {
90 1
                    $s = $groups[$i];
91

92 1
                    $result = new RegularExpression();
93 1
                    $result->setPattern("\\\\" . $i);
94 1
                    $sregex = $result->getRegexp($this->project);
95 1
                    $sregex->setReplace($output);
96 1
                    $output = $sregex->replace($s);
97
                }
98

99 1
                if (!($this->distinct && in_array($output, $used))) {
100 1
                    $used[] = $output;
101 1
                    if ($cnt !== 0) {
102 1
                        $buf .= $this->delim;
103
                    }
104 1
                    $buf .= $output;
105 1
                    $cnt++;
106
                }
107
            }
108
        }
109

110 1
        if ($buf !== '') {
111 1
            $this->setPropertyValue($buf);
112
        }
113
    }
114
}

Read our documentation on viewing source code .

Loading