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
 * Encode data from <code>in</code> encoding to <code>out</code> encoding.
22
 *
23
 * Example:
24
 * <pre>
25
 * <iconvfilter inputencoding="UTF-8" outputencoding="CP1251" />
26
 * </pre>
27
 * Or:
28
 * <pre>
29
 * <filterreader classname="phing.filters.IconvFilter">
30
 *    <param name="inputencoding" value="UTF-8" />
31
 *    <param name="outputencoding" value="CP1251" />
32
 * </filterreader>
33
 * </pre>
34
 *
35
 * @author  Alexey Shockov, <alexey@shockov.com>
36
 * @package phing.filters
37
 */
38
class IconvFilter extends BaseParamFilterReader implements ChainableReader
39
{
40
    private $inputEncoding;
41

42
    private $outputEncoding;
43

44
    /**
45
     * Returns first n lines of stream.
46
     *
47
     * @param  int $len
48
     * @return string Characters read, or -1 if the end of the stream has been reached
49
     *
50
     * @throws IOException if the underlying stream throws an IOException
51
     * during reading
52
     */
53 0
    public function read($len = null)
54
    {
55 0
        $this->initialize();
56

57
        // Process whole text at once.
58 0
        $text = null;
59 0
        while (($data = $this->in->read($len)) !== -1) {
60 0
            $text .= $data;
61
        }
62

63
        // At the end.
64 0
        if (null === $text) {
65 0
            return -1;
66
        }
67

68 0
        $this->log(
69 0
            "Encoding " . $this->in->getResource() . " from " . $this->getInputEncoding() . " to " . $this->getOutputEncoding(),
70 0
            Project::MSG_VERBOSE
71
        );
72

73 0
        return iconv($this->inputEncoding, $this->outputEncoding, $text);
74
    }
75

76
    /**
77
     *
78
     * @param string $encoding Input encoding.
79
     */
80 0
    public function setInputEncoding($encoding)
81
    {
82 0
        $this->inputEncoding = $encoding;
83
    }
84

85
    /**
86
     *
87
     * @return string
88
     */
89 0
    public function getInputEncoding()
90
    {
91 0
        return $this->inputEncoding;
92
    }
93

94
    /**
95
     *
96
     * @param string $encoding Output encoding.
97
     */
98 0
    public function setOutputEncoding($encoding)
99
    {
100 0
        $this->outputEncoding = $encoding;
101
    }
102

103
    /**
104
     *
105
     * @return string
106
     */
107 0
    public function getOutputEncoding()
108
    {
109 0
        return $this->outputEncoding;
110
    }
111

112
    /**
113
     * Creates a new IconvFilter using the passed in Reader for instantiation.
114
     *
115
     * @param Reader $reader
116
     * @return self A new filter based on this configuration, but filtering the specified reader.
117
     * @internal param A $object Reader object providing the underlying stream. Must not be <code>null</code>.
118
     */
119 0
    public function chain(Reader $reader): Reader
120
    {
121 0
        $filter = new self($reader);
122

123 0
        $filter->setInputEncoding($this->getInputEncoding());
124 0
        $filter->setOutputEncoding($this->getOutputEncoding());
125

126 0
        $filter->setInitialized(true);
127 0
        $filter->setProject($this->getProject());
128

129 0
        return $filter;
130
    }
131

132
    /**
133
     * Configuring object from the parameters list.
134
     */
135 0
    private function initialize()
136
    {
137 0
        if ($this->getInitialized()) {
138 0
            return;
139
        }
140

141 0
        $params = $this->getParameters();
142 0
        if ($params !== null) {
143 0
            foreach ($params as $param) {
144 0
                if ('in' === $param->getName()) {
145 0
                    $this->setInputEncoding($param->getValue());
146
                } else {
147 0
                    if ('out' === $param->getName()) {
148 0
                        $this->setOutputEncoding($param->getValue());
149
                    }
150
                }
151
            }
152
        }
153

154 0
        $this->setInitialized(true);
155
    }
156
}

Read our documentation on viewing source code .

Loading