Completed
Push — master ( 7082f0...55a2b2 )
by Siad
15:25
created

SuffixLines::initialize()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 4
rs 10
c 0
b 0
f 0
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
 * Attaches a suffix to every line.
22
 *
23
 * Example:
24
 * <pre><suffixlines suffix="Foo"/></pre>
25
 *
26
 * Or:
27
 *
28
 * <pre><filterreader classname="phing.filters.SuffixLines">
29
 *  <param name="suffix" value="Foo"/>
30
 * </filterreader></pre>
31
 *
32
 * @author  Siad Ardroumli <[email protected]>
33
 * @see     FilterReader
34
 * @package phing.filters
35
 */
36
class SuffixLines extends BaseParamFilterReader implements ChainableReader
37
{
38
    /**
39
     * Parameter name for the suffix.
40
     *
41
     * @var string
42
     */
43
    const SUFFIX_KEY = "suffix";
44
45
    /**
46
     * The suffix to be used.
47
     *
48
     * @var string
49
     */
50
    private $suffix = null;
51
52
    /** @var string */
53
    private $queuedData;
54
55
    /**
56
     * Adds a suffix to each line of input stream and returns resulting stream.
57
     *
58
     * @param  int $len
59
     * @return mixed buffer, -1 on EOF
60
     */
61 1
    public function read($len = null)
62
    {
63 1
        if (!$this->getInitialized()) {
64 1
            $this->initialize();
65 1
            $this->setInitialized(true);
66
        }
67
68 1
        $ch = -1;
69
70 1
        if ($this->queuedData !== null && $this->queuedData === '') {
71
            $this->queuedData = null;
72
        }
73
74 1
        if ($this->queuedData !== null) {
75 1
            $ch = $this->queuedData[0];
76 1
            $this->queuedData = substr($this->queuedData, 1);
77 1
            if ($this->queuedData === '') {
78 1
                $this->queuedData = null;
79
            }
80
        } else {
81 1
            $this->queuedData = $this->readLine();
82 1
            if ($this->queuedData === null) {
83 1
                $ch = -1;
84
            } else {
85 1
                if ($this->suffix !== null) {
86 1
                    $lf = '';
87 1
                    if (StringHelper::endsWith($this->queuedData, "\r\n")) {
88 1
                        $lf = "\r\n";
89 1
                    } elseif (StringHelper::endsWith($this->queuedData, "\n")) {
90
                        $lf = "\n";
91
                    }
92 1
                    $this->queuedData = substr($this->queuedData, 0, strlen($this->queuedData) - strlen($lf)) . $this->suffix . $lf;
93
                }
94 1
                return $this->read();
95
            }
96
        }
97 1
        return $ch;
98
    }
99
100
    /**
101
     * Sets the suffix to add at the end of each input line.
102
     *
103
     * @param string $suffix The suffix to add at the start of each input line.
104
     *                       May be <code>null</code>, in which case no suffix
105
     *                       is added.
106
     */
107
    public function setSuffix($suffix)
108
    {
109
        $this->suffix = (string) $suffix;
110
    }
111
112
    /**
113
     * Returns the suffix which will be added at the end of each input line.
114
     *
115
     * @return string The suffix which will be added at the end of each input line
116
     */
117
    public function getSuffix()
118
    {
119
        return $this->suffix;
120
    }
121
122
    /**
123
     * Creates a new PrefixLines filter using the passed in
124
     * Reader for instantiation.
125
     *
126
     * @param Reader $reader
127
     * @return SuffixLines A new filter based on this configuration, but filtering
128
     *                the specified reader
129
     * @internal param A $object Reader object providing the underlying stream.
130
     *               Must not be <code>null</code>.
131
     */
132
    public function chain(Reader $reader): Reader
133
    {
134
        $newFilter = new SuffixLines($reader);
135
        $newFilter->setSuffix($this->getSuffix());
136
        $newFilter->setInitialized(true);
137
        $newFilter->setProject($this->getProject());
138
139
        return $newFilter;
140
    }
141
142
    /**
143
     * Initializes the suffix if it is available from the parameters.
144
     */
145 1
    private function initialize()
146
    {
147 1
        $params = $this->getParameters();
148 1
        if ($params !== null) {
0 ignored issues
show
introduced by
The condition $params !== null is always true.
Loading history...
149 1
            for ($i = 0, $_i = count($params); $i < $_i; $i++) {
150 1
                if (self::SUFFIX_KEY == $params[$i]->getName()) {
151 1
                    $this->suffix = (string) $params[$i]->getValue();
152 1
                    break;
153
                }
154
            }
155
        }
156 1
    }
157
}
158