Passed
Push — main ( 221f6d...f8c128 )
by Siad
05:28
created

src/Phing/Filter/PhpArrayMapLines.php (1 issue)

Severity
1
<?php
2
3
/**
4
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
5
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
6
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
7
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
8
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
9
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
10
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
11
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
12
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
13
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
14
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15
 *
16
 * This software consists of voluntary contributions made by many individuals
17
 * and is licensed under the LGPL. For more information please see
18
 * <http://phing.info>.
19
 */
20
21
namespace Phing\Filter;
22
23
use Phing\Exception\BuildException;
24
use Phing\Io\Reader;
25
26
/**
27
 * Applies a native php function to the original input.
28
 *
29
 * Example:
30
 * <pre><phparraymaplines function="strtoupper"/></pre>
31
 *
32
 * Or:
33
 *
34
 * <pre><filterreader classname="phing.filters.PhpArrayMapLines">
35
 *  <param name="function" value="strtoupper"/>
36
 * </filterreader></pre>
37
 *
38
 * @author  Siad Ardroumli <[email protected]>
39
 */
40
class PhpArrayMapLines extends BaseParamFilterReader implements ChainableReader
41
{
42
    /**
43
     * Parameter name for the function.
44
     *
45
     * @var string
46
     */
47
    public const FUNCTION_KEY = 'function';
48
49
    /**
50
     * The function to be used.
51
     *
52
     * @var string
53
     */
54
    private $function;
55
56
    /**
57
     * Applies a native php function to the original input and returns resulting stream.
58
     *
59
     * @param int $len
60
     *
61
     * @return mixed buffer, -1 on EOF
62
     */
63
    public function read($len = null)
64
    {
65
        if (!$this->getInitialized()) {
66
            $this->initialize();
67
            $this->checkAttributes();
68
            $this->setInitialized(true);
69
        }
70
71
        $buffer = $this->in->read($len);
72
73
        if (-1 === $buffer || !function_exists($this->function)) {
74
            return -1;
75
        }
76
77
        $lines = explode("\n", $buffer);
78
79
        $filtered = array_map($this->function, $lines);
80
81
        return implode("\n", $filtered);
82
    }
83
84
    /**
85
     * Sets the function used by array_map.
86
     *
87
     * @param string $function the function used by array_map
88
     */
89
    public function setFunction($function)
90
    {
91
        $this->function = (string) $function;
92
    }
93
94
    /**
95
     * Returns the prefix which will be added at the start of each input line.
96
     *
97
     * @return string The prefix which will be added at the start of each input line
98
     */
99
    public function getFunction()
100
    {
101
        return $this->function;
102
    }
103
104
    /**
105
     * Creates a new PhpArrayMapLines filter using the passed in
106
     * Reader for instantiation.
107
     *
108
     * @param Reader $reader Reader object providing the underlying stream.
109
     *                       Must not be <code>null</code>.
110
     *
111
     * @return PhpArrayMapLines A new filter based on this configuration, but filtering
112
     *                          the specified reader
113
     */
114
    public function chain(Reader $reader): Reader
115
    {
116
        $newFilter = new PhpArrayMapLines($reader);
117
        $newFilter->setFunction($this->getFunction());
118
        $newFilter->setInitialized(true);
119
        $newFilter->setProject($this->getProject());
120
121
        return $newFilter;
122
    }
123
124
    /**
125
     * Make sure that required attributes are set.
126
     *
127
     * @throws buildException - if any required attribs aren't set
128
     */
129
    protected function checkAttributes()
130
    {
131
        if (!$this->function) {
132
            throw new BuildException("You must specify a value for the 'function' attribute.");
133
        }
134
    }
135
136
    /**
137
     * Initializes the function if it is available from the parameters.
138
     */
139
    private function initialize()
140
    {
141
        $params = $this->getParameters();
142
        if (null !== $params) {
0 ignored issues
show
The condition null !== $params is always true.
Loading history...
143
            for ($i = 0, $_i = count($params); $i < $_i; ++$i) {
144
                if (self::FUNCTION_KEY == $params[$i]->getName()) {
145
                    $this->function = (string) $params[$i]->getValue();
146
147
                    break;
148
                }
149
            }
150
        }
151
    }
152
}
153