Passed
Push — main ( f5700b...aaf4f3 )
by Michiel
32:12 queued 25:00
created

src/Phing/Filter/IconvFilter.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\Io\IOException;
24
use Phing\Io\Reader;
25
use Phing\Project;
26
27
/**
28
 * Encode data from <code>in</code> encoding to <code>out</code> encoding.
29
 *
30
 * Example:
31
 * <pre>
32
 * <iconvfilter inputencoding="UTF-8" outputencoding="CP1251" />
33
 * </pre>
34
 * Or:
35
 * <pre>
36
 * <filterreader classname="phing.filters.IconvFilter">
37
 *    <param name="inputencoding" value="UTF-8" />
38
 *    <param name="outputencoding" value="CP1251" />
39
 * </filterreader>
40
 * </pre>
41
 *
42
 * @author  Alexey Shockov, <[email protected]>
43
 */
44
class IconvFilter extends BaseParamFilterReader implements ChainableReader
45
{
46
    private $inputEncoding;
47
48
    private $outputEncoding;
49
50
    /**
51
     * Returns first n lines of stream.
52
     *
53
     * @param int $len
54
     *
55
     * @throws IOException if the underlying stream throws an IOException
56
     *                     during reading
57
     *
58
     * @return string Characters read, or -1 if the end of the stream has been reached
59
     */
60
    public function read($len = null)
61
    {
62
        $this->initialize();
63
64
        // Process whole text at once.
65
        $text = null;
66
        while (($data = $this->in->read($len)) !== -1) {
67
            $text .= $data;
68
        }
69
70
        // At the end.
71
        if (null === $text) {
72
            return -1;
73
        }
74
75
        $this->log(
76
            'Encoding ' . $this->in->getResource() . ' from ' . $this->getInputEncoding() . ' to ' . $this->getOutputEncoding(),
77
            Project::MSG_VERBOSE
78
        );
79
80
        return iconv($this->inputEncoding, $this->outputEncoding, $text);
81
    }
82
83
    /**
84
     * @param string $encoding input encoding
85
     */
86
    public function setInputEncoding($encoding)
87
    {
88
        $this->inputEncoding = $encoding;
89
    }
90
91
    /**
92
     * @return string
93
     */
94
    public function getInputEncoding()
95
    {
96
        return $this->inputEncoding;
97
    }
98
99
    /**
100
     * @param string $encoding output encoding
101
     */
102
    public function setOutputEncoding($encoding)
103
    {
104
        $this->outputEncoding = $encoding;
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getOutputEncoding()
111
    {
112
        return $this->outputEncoding;
113
    }
114
115
    /**
116
     * Creates a new IconvFilter using the passed in Reader for instantiation.
117
     *
118
     * @return self a new filter based on this configuration, but filtering the specified reader
119
     *
120
     * @internal param A $object Reader object providing the underlying stream. Must not be <code>null</code>.
121
     */
122
    public function chain(Reader $reader): Reader
123
    {
124
        $filter = new self($reader);
125
126
        $filter->setInputEncoding($this->getInputEncoding());
127
        $filter->setOutputEncoding($this->getOutputEncoding());
128
129
        $filter->setInitialized(true);
130
        $filter->setProject($this->getProject());
131
132
        return $filter;
133
    }
134
135
    /**
136
     * Configuring object from the parameters list.
137
     */
138
    private function initialize()
139
    {
140
        if ($this->getInitialized()) {
141
            return;
142
        }
143
144
        $params = $this->getParameters();
145
        if (null !== $params) {
0 ignored issues
show
The condition null !== $params is always true.
Loading history...
146
            foreach ($params as $param) {
147
                if ('in' === $param->getName()) {
148
                    $this->setInputEncoding($param->getValue());
149
                } else {
150
                    if ('out' === $param->getName()) {
151
                        $this->setOutputEncoding($param->getValue());
152
                    }
153
                }
154
            }
155
        }
156
157
        $this->setInitialized(true);
158
    }
159
}
160