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

TidyFilter::initialize()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 5
nop 0
dl 0
loc 10
ccs 0
cts 8
cp 0
crap 30
rs 9.6111
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
 * This filter uses the bundled-with-PHP Tidy extension to filter input.
22
 *
23
 * <p>
24
 * Example:<br/>
25
 * <pre>
26
 * <tidyfilter encoding="utf8">
27
 *   <config name="indent" value="true"/>
28
 *   <config name="output-xhtml" value="true"/>
29
 * </tidyfilter>
30
 * </pre>
31
 *
32
 * @author  Hans Lellelid <[email protected]>
33
 * @package phing.filters
34
 */
35
class TidyFilter extends BaseParamFilterReader implements ChainableReader
36
{
37
38
    /**
39
     * @var string Encoding of resulting document.
40
     */
41
    private $encoding = 'utf8';
42
43
    /**
44
     * @var array Parameter[]
45
     */
46
    private $configParameters = [];
47
48
    /**
49
     * Set the encoding for resulting (X)HTML document.
50
     *
51
     * @param string $v
52
     */
53
    public function setEncoding($v)
54
    {
55
        $this->encoding = $v;
56
    }
57
58
    /**
59
     * Sets the config params.
60
     *
61
     * @param array Parameter[]
62
     * @see   chain()
63
     */
64
    public function setConfigParameters($params)
65
    {
66
        $this->configParameters = $params;
67
    }
68
69
    /**
70
     * Adds a <config> element (which is a Parameter).
71
     *
72
     * @return Parameter
73
     */
74
    public function createConfig()
75
    {
76
        $num = array_push($this->configParameters, new Parameter());
77
78
        return $this->configParameters[$num - 1];
79
    }
80
81
    /**
82
     * Converts the Parameter objects being used to store configuration into a simle assoc array.
83
     *
84
     * @return array
85
     */
86
    private function getDistilledConfig()
87
    {
88
        $config = [];
89
        foreach ($this->configParameters as $p) {
90
            $config[$p->getName()] = $p->getValue();
91
        }
92
93
        return $config;
94
    }
95
96
    /**
97
     * Reads input and returns Tidy-filtered output.
98
     *
99
     * @param  int $len
100
     * @throws BuildException
101
     * @return string Characters read, or -1 if the end of the stream has been reached
102
     */
103
    public function read($len = null)
104
    {
105
        if (!class_exists('Tidy')) {
106
            throw new BuildException("You must enable the 'tidy' extension in your PHP configuration in order to use the Tidy filter.");
107
        }
108
109
        if (!$this->getInitialized()) {
110
            $this->initialize();
111
            $this->setInitialized(true);
112
        }
113
114
        $buffer = $this->in->read($len);
115
        if ($buffer === -1) {
116
            return -1;
117
        }
118
119
        $config = $this->getDistilledConfig();
120
121
        $tidy = new tidy();
122
        $tidy->parseString($buffer, $config, $this->encoding);
123
        $tidy->cleanRepair();
124
125
        return tidy_get_output($tidy);
126
    }
127
128
    /**
129
     * Creates a new TidyFilter using the passed in Reader for instantiation.
130
     *
131
     * @param Reader $reader Reader object providing the underlying stream.
132
     *                    Must not be <code>null</code>.
133
     * @return TidyFilter a new filter based on this configuration, but filtering the specified reader
134
     */
135
    public function chain(Reader $reader): Reader
136
    {
137
        $newFilter = new self($reader);
138
        $newFilter->setConfigParameters($this->configParameters);
139
        $newFilter->setEncoding($this->encoding);
140
        $newFilter->setProject($this->getProject());
141
142
        return $newFilter;
143
    }
144
145
    /**
146
     * Initializes any parameters (e.g. config options).
147
     * This method is only called when this filter is used through a <filterreader> tag in build file.
148
     */
149
    private function initialize()
150
    {
151
        $params = $this->getParameters();
152
        if (!empty($params)) {
153
            foreach ($params as $param) {
154
                if ($param->getType() == "config") {
155
                    $this->configParameters[] = $param;
156
                } else {
157
                    if ($param->getName() == "encoding") {
158
                        $this->setEncoding($param->getValue());
159
                    }
160
                }
161
            }
162
        }
163
    }
164
}
165