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 <siad.ardroumli@gmail.com>
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
    public 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 0
    public function read($len = null)
58
    {
59 0
        if (!$this->getInitialized()) {
60 0
            $this->initialize();
61 0
            $this->checkAttributes();
62 0
            $this->setInitialized(true);
63
        }
64

65 0
        $buffer = $this->in->read($len);
66

67 0
        if ($buffer === -1 || !function_exists($this->function)) {
68 0
            return -1;
69
        }
70

71 0
        $lines = explode("\n", $buffer);
72

73 0
        $filtered = array_map($this->function, $lines);
74

75 0
        $filtered_buffer = implode("\n", $filtered);
76

77 0
        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 0
    public function setFunction($function)
86
    {
87 0
        $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 0
    public function getFunction()
96
    {
97 0
        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 0
    protected function checkAttributes()
106
    {
107 0
        if (!$this->function) {
108 0
            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 0
    public function chain(Reader $reader): Reader
123
    {
124 0
        $newFilter = new PhpArrayMapLines($reader);
125 0
        $newFilter->setFunction($this->getFunction());
126 0
        $newFilter->setInitialized(true);
127 0
        $newFilter->setProject($this->getProject());
128

129 0
        return $newFilter;
130
    }
131

132
    /**
133
     * Initializes the function if it is available from the parameters.
134
     */
135 0
    private function initialize()
136
    {
137 0
        $params = $this->getParameters();
138 0
        if ($params !== null) {
139 0
            for ($i = 0, $_i = count($params); $i < $_i; $i++) {
140 0
                if (self::FUNCTION_KEY == $params[$i]->getName()) {
141 0
                    $this->function = (string) $params[$i]->getValue();
142 0
                    break;
143
                }
144
            }
145
        }
146
    }
147
}

Read our documentation on viewing source code .

Loading