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
 * Task definition for the phing task to switch on a particular value.
22
 *
23
 * Task calling syntax:
24
 * ```
25
 * <switch value="value" [caseinsensitive="true|false"] >
26
 *   <case value="val">
27
 *     <property name="propname" value="propvalue" /> |
28
 *     <phingcall target="targetname" /> |
29
 *     any other tasks
30
 *   </case>
31
 *   [
32
 *   <default>
33
 *     <property name="propname" value="propvalue" /> |
34
 *     <phingcall target="targetname" /> |
35
 *     any other tasks
36
 *   </default>
37
 *   ]
38
 * </switch>
39
 * ```
40
 *
41
 *
42
 * Attributes:
43
 * value           -> The value to switch on
44
 * caseinsensitive -> Should we do case insensitive comparisons?
45
 *                    (default is false)
46
 *
47
 * Subitems:
48
 * case     --> An individual case to consider, if the value that
49
 *              is being switched on matches to value attribute of
50
 *              the case, then the nested tasks will be executed.
51
 * default  --> The default case for when no match is found.
52
 *
53
 *
54
 * Crude Example:
55
 *
56
 * ```
57
 * <switch value="${foo}">
58
 *     <case value="bar">
59
 *       <echo message="The value of property foo is bar" />
60
 *     </case>
61
 *     <case value="baz">
62
 *       <echo message="The value of property foo is baz" />
63
 *     </case>
64
 *     <default>
65
 *       <echo message="The value of property foo is not sensible" />
66
 *     </default>
67
 * </switch>
68
 * ```
69
 *
70
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
71
 * @package phing.tasks.system
72
 */
73
class SwitchTask extends Task
74
{
75
    /**
76
     * @var mixed $value
77
     */
78
    private $value = null;
79

80
    /**
81
     * @var array $cases
82
     */
83
    private $cases = [];
84

85
    /**
86
     * @var SequentialTask $defaultCase
87
     */
88
    private $defaultCase = null;
89

90
    /**
91
     * @var bool $caseInsensitive
92
     */
93
    private $caseInsensitive = false;
94

95
    /***
96
     * Sets the value being switched on.
97
     *
98
     * @param mixed $value
99
     *
100
     * @return void
101
     */
102 0
    public function setValue($value)
103
    {
104 0
        $this->value = $value;
105
    }
106

107
    /**
108
     * Adds a CaseTask.
109
     *
110
     * @param CaseTask $case
111
     *
112
     * @return void
113
     */
114 0
    public function addCase(CaseTask $case)
115
    {
116 0
        $this->cases[] = $case;
117
    }
118

119
    /**
120
     * @param $bool
121
     *
122
     * @return void
123
     */
124 0
    public function setCaseInsensitive($bool)
125
    {
126 0
        $this->caseInsensitive = $bool;
127
    }
128

129
    /**
130
     * Creates the `<default>` tag
131
     */
132 0
    public function addDefault(SequentialTask $res)
133
    {
134 0
        if ($this->defaultCase !== null) {
135 0
            throw new BuildException("Cannot specify multiple default cases");
136
        }
137

138 0
        $this->defaultCase = $res;
139
    }
140

141 0
    public function main()
142
    {
143 0
        if ($this->value === null) {
144 0
            throw new BuildException("Value is missing <switch>");
145
        }
146

147 0
        if (empty($this->cases) && $this->defaultCase === null) {
148 0
            throw new BuildException("No cases supplied <switch>");
149
        }
150

151 0
        $selectedCase = $this->defaultCase;
152

153
        /**
154
         * @var CaseTask $case
155
         */
156 0
        foreach ($this->cases as $case) {
157 0
            $cValue = $case->getValue();
158

159 0
            if (empty($case)) {
160 0
                throw new BuildException("Value is required for case.");
161
            }
162

163 0
            $mValue = $this->value;
164 0
            if ($this->caseInsensitive) {
165 0
                $cValue = strtoupper($case->getValue());
166 0
                $mValue = strtoupper($this->value);
167
            }
168

169 0
            if ($cValue === $mValue && $case != $this->defaultCase) {
170 0
                $selectedCase = $case;
171
            }
172
        }
173

174 0
        if ($selectedCase === null) {
175 0
            throw new BuildException("No case matched the value " . $this->value . " and no default has been specified.");
176
        }
177

178 0
        $selectedCase->perform();
179
    }
180
}

Read our documentation on viewing source code .

Loading