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
 * Filter to flatten the stream to a single line.
22
 *
23
 * Example:
24
 *
25
 * <pre><striplinebreaks/></pre>
26
 *
27
 * Or:
28
 *
29
 * <pre><filterreader classname="phing.filters.StripLineBreaks"/></pre>
30
 *
31
 * @author  <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
32
 * @author  hans lellelid, hans@velum.net
33
 * @see     BaseParamFilterReader
34
 * @package phing.filters
35
 */
36
class StripLineBreaks extends BaseParamFilterReader implements ChainableReader
37
{
38

39
    /**
40
     * Default line-breaking characters.
41
     *
42
     * @var string
43
     */
44
    public const DEFAULT_LINE_BREAKS = "\r\n";
45

46
    /**
47
     * Parameter name for the line-breaking characters parameter.
48
     *
49
     * @var string
50
     */
51
    public const LINES_BREAKS_KEY = "linebreaks";
52

53
    /**
54
     * The characters that are recognized as line breaks.
55
     *
56
     * @var string
57
     */
58
    private $lineBreaks = "\r\n"; // self::DEFAULT_LINE_BREAKS;
59

60
    /**
61
     * Returns the filtered stream, only including
62
     * characters not in the set of line-breaking characters.
63
     *
64
     * @param  int $len
65
     * @return mixed the resulting stream, or -1
66
     *               if the end of the resulting stream has been reached.
67
     *
68
     * @throws IOException if the underlying stream throws an IOException
69
     *            during reading
70
     */
71 1
    public function read($len = null)
72
    {
73 1
        if (!$this->getInitialized()) {
74 0
            $this->initialize();
75 0
            $this->setInitialized(true);
76
        }
77

78 1
        $buffer = $this->in->read($len);
79 1
        if ($buffer === -1) {
80 1
            return -1;
81
        }
82

83 1
        $buffer = preg_replace("/[" . $this->lineBreaks . "]/", '', $buffer);
84

85 1
        return $buffer;
86
    }
87

88
    /**
89
     * Sets the line-breaking characters.
90
     *
91
     * @param string $lineBreaks A String containing all the characters to be
92
     *                           considered as line-breaking.
93
     */
94 1
    public function setLineBreaks($lineBreaks)
95
    {
96 1
        $this->lineBreaks = (string) $lineBreaks;
97
    }
98

99
    /**
100
     * Gets the line-breaking characters.
101
     *
102
     * @return string A String containing all the characters that are considered as line-breaking.
103
     */
104 1
    public function getLineBreaks()
105
    {
106 1
        return $this->lineBreaks;
107
    }
108

109
    /**
110
     * Creates a new StripLineBreaks using the passed in
111
     * Reader for instantiation.
112
     *
113
     * @param Reader $reader
114
     * @return StripLineBreaks A new filter based on this configuration, but filtering
115
     *                the specified reader
116
     * @internal param A $object Reader object providing the underlying stream.
117
     *               Must not be <code>null</code>.
118
     */
119 1
    public function chain(Reader $reader): Reader
120
    {
121 1
        $newFilter = new StripLineBreaks($reader);
122 1
        $newFilter->setLineBreaks($this->getLineBreaks());
123 1
        $newFilter->setInitialized(true);
124 1
        $newFilter->setProject($this->getProject());
125

126 1
        return $newFilter;
127
    }
128

129
    /**
130
     * Parses the parameters to set the line-breaking characters.
131
     */
132 0
    private function initialize()
133
    {
134 0
        $userDefinedLineBreaks = null;
135 0
        $params = $this->getParameters();
136 0
        if ($params !== null) {
137 0
            for ($i = 0, $paramsCount = count($params); $i < $paramsCount; $i++) {
138 0
                if (self::LINES_BREAKS_KEY === $params[$i]->getName()) {
139 0
                    $userDefinedLineBreaks = $params[$i]->getValue();
140 0
                    break;
141
                }
142
            }
143
        }
144

145 0
        if ($userDefinedLineBreaks !== null) {
146 0
            $this->lineBreaks = $userDefinedLineBreaks;
147
        }
148
    }
149
}

Read our documentation on viewing source code .

Loading