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
|
|
|
* Applies a native php function to the original input. |
22
|
|
|
* |
23
|
|
|
* Example: |
24
|
|
|
* <pre><phparraymaplines function="strtoupper"/></pre> |
25
|
|
|
* |
26
|
|
|
* Or: |
27
|
|
|
* |
28
|
|
|
* <pre><filterreader classname="phing.filters.PhpArrayMapLines"> |
29
|
|
|
* <param name="function" value="strtoupper"/> |
30
|
|
|
* </filterreader></pre> |
31
|
|
|
* |
32
|
|
|
* @author Siad Ardroumli <[email protected]> |
33
|
|
|
* @package phing.filters |
34
|
|
|
*/ |
35
|
|
|
class PhpArrayMapLines extends BaseParamFilterReader implements ChainableReader |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* Parameter name for the function. |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
const FUNCTION_KEY = "function"; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The function to be used. |
46
|
|
|
* |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $function = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Applies a native php function to the original input and returns resulting stream. |
53
|
|
|
* |
54
|
|
|
* @param int $len |
55
|
|
|
* @return mixed buffer, -1 on EOF |
56
|
|
|
*/ |
57
|
|
|
public function read($len = null) |
58
|
|
|
{ |
59
|
|
|
if (!$this->getInitialized()) { |
60
|
|
|
$this->initialize(); |
61
|
|
|
$this->checkAttributes(); |
62
|
|
|
$this->setInitialized(true); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$buffer = $this->in->read($len); |
66
|
|
|
|
67
|
|
|
if ($buffer === -1 || !function_exists($this->function)) { |
68
|
|
|
return -1; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$lines = explode("\n", $buffer); |
72
|
|
|
|
73
|
|
|
$filtered = array_map($this->function, $lines); |
74
|
|
|
|
75
|
|
|
$filtered_buffer = implode("\n", $filtered); |
76
|
|
|
|
77
|
|
|
return $filtered_buffer; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Sets the function used by array_map. |
82
|
|
|
* |
83
|
|
|
* @param string $function The function used by array_map. |
84
|
|
|
*/ |
85
|
|
|
public function setFunction($function) |
86
|
|
|
{ |
87
|
|
|
$this->function = (string) $function; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the prefix which will be added at the start of each input line. |
92
|
|
|
* |
93
|
|
|
* @return string The prefix which will be added at the start of each input line |
94
|
|
|
*/ |
95
|
|
|
public function getFunction() |
96
|
|
|
{ |
97
|
|
|
return $this->function; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Make sure that required attributes are set. |
102
|
|
|
* |
103
|
|
|
* @throws BuildException - if any required attribs aren't set. |
104
|
|
|
*/ |
105
|
|
|
protected function checkAttributes() |
106
|
|
|
{ |
107
|
|
|
if (!$this->function) { |
108
|
|
|
throw new BuildException("You must specify a value for the 'function' attribute."); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Creates a new PhpArrayMapLines filter using the passed in |
114
|
|
|
* Reader for instantiation. |
115
|
|
|
* |
116
|
|
|
* @param Reader $reader Reader object providing the underlying stream. |
117
|
|
|
* Must not be <code>null</code>. |
118
|
|
|
* |
119
|
|
|
* @return PhpArrayMapLines A new filter based on this configuration, but filtering |
120
|
|
|
* the specified reader |
121
|
|
|
*/ |
122
|
|
|
public function chain(Reader $reader): Reader |
123
|
|
|
{ |
124
|
|
|
$newFilter = new PhpArrayMapLines($reader); |
125
|
|
|
$newFilter->setFunction($this->getFunction()); |
126
|
|
|
$newFilter->setInitialized(true); |
127
|
|
|
$newFilter->setProject($this->getProject()); |
128
|
|
|
|
129
|
|
|
return $newFilter; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Initializes the function if it is available from the parameters. |
134
|
|
|
*/ |
135
|
|
|
private function initialize() |
136
|
|
|
{ |
137
|
|
|
$params = $this->getParameters(); |
138
|
|
|
if ($params !== null) { |
|
|
|
|
139
|
|
|
for ($i = 0, $_i = count($params); $i < $_i; $i++) { |
140
|
|
|
if (self::FUNCTION_KEY == $params[$i]->getName()) { |
141
|
|
|
$this->function = (string) $params[$i]->getValue(); |
142
|
|
|
break; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|