1<?php
2
3/**
4 * Hoa
5 *
6 *
7 * @license
8 *
9 * New BSD License
10 *
11 * Copyright © 2007-2017, Hoa community. All rights reserved.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions are met:
15 *     * Redistributions of source code must retain the above copyright
16 *       notice, this list of conditions and the following disclaimer.
17 *     * Redistributions in binary form must reproduce the above copyright
18 *       notice, this list of conditions and the following disclaimer in the
19 *       documentation and/or other materials provided with the distribution.
20 *     * Neither the name of the Hoa nor the names of its contributors may be
21 *       used to endorse or promote products derived from this software without
22 *       specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37namespace Hoa\Stream\Filter;
38
39use Hoa\Stream;
40
41/**
42 * Class \Hoa\Stream\Filter\Basic.
43 *
44 * Basic filter. Force to implement some methods.
45 * Actually, it extends the php_user_filter class.
46 *
47 * @copyright  Copyright © 2007-2017 Hoa community
48 * @license    New BSD License
49 */
50abstract class Basic extends \php_user_filter implements Stream\IStream\Stream
51{
52    /**
53     * Filter processed successfully with data available in the out bucket
54     * brigade.
55     *
56     * @const int
57     */
58    const PASS_ON          = PSFS_PASS_ON;
59
60    /**
61     * Filter processed successfully, however no data was available to return.
62     * More data is required from the stream or prior filter.
63     *
64     * @const int
65     */
66    const FEED_ME          = PSFS_FEED_ME;
67
68    /**
69     * The filter experienced and unrecoverable error and cannot continue.
70     *
71     * @const int
72     */
73    const FATAL_ERROR      = PSFS_ERR_FATAL;
74
75    /**
76     * Regular read/write.
77     *
78     * @const int
79     */
80    const FLAG_NORMAL      = PSFS_FLAG_NORMAL;
81
82    /**
83     * An incremental flush.
84     *
85     * @const int
86     */
87    const FLAG_FLUSH_INC   = PSFS_FLAG_FLUSH_INC;
88
89    /**
90     * Final flush prior to closing.
91     *
92     * @const int
93     */
94    const FLAG_FLUSH_CLOSE = PSFS_FLAG_FLUSH_CLOSE;
95
96
97
98    /**
99     * Filter data.
100     * This method is called whenever data is read from or written to the attach
101     * stream.
102     *
103     * @param   resource  $in           A resource pointing to a bucket brigade
104     *                                  which contains one or more bucket
105     *                                  objects containing data to be filtered.
106     * @param   resource  $out          A resource pointing to a second bucket
107     *                                  brigade into which your modified buckets
108     *                                  should be replaced.
109     * @param   int       &$consumed    Which must always be declared by
110     *                                  reference, should be incremented by the
111     *                                  length of the data which your filter
112     *                                  reads in and alters.
113     * @param   bool      $closing      If the stream is in the process of
114     *                                  closing (and therefore this is the last
115     *                                  pass through the filterchain), the
116     *                                  closing parameter will be set to true.
117     * @return  int
118     */
119    public function filter($in, $out, &$consumed, $closing)
120    {
121        $iBucket = new Stream\Bucket($in);
122        $oBucket = new Stream\Bucket($out);
123
124        while (false === $iBucket->eob()) {
125            $consumed += $iBucket->getLength();
126            $oBucket->append($iBucket);
127        }
128
129        unset($iBucket);
130        unset($oBucket);
131
132        return self::PASS_ON;
133    }
134
135    /**
136     * Called during instanciation of the filter class object.
137     *
138     * @return  bool
139     */
140    public function onCreate()
141    {
142        return true;
143    }
144
145    /**
146     * Called upon filter shutdown (typically, this is also during stream
147     * shutdown), and is executed after the flush method is called.
148     *
149     * @return  void
150     */
151    public function onClose()
152    {
153        return;
154    }
155
156    /**
157     * Set the filter name.
158     *
159     * @param   string  $name    Filter name.
160     * @return  string
161     */
162    public function setName($name)
163    {
164        $old              = $this->filtername;
165        $this->filtername = $name;
166
167        return $old;
168    }
169
170    /**
171     * Set the filter parameters.
172     *
173     * @param   mixed   $parameters    Filter parameters.
174     * @return  mixed
175     */
176    public function setParameters($parameters)
177    {
178        $old          = $this->params;
179        $this->params = $parameters;
180
181        return $old;
182    }
183
184    /**
185     * Get the filter name.
186     *
187     * @return  string
188     */
189    public function getName()
190    {
191        return $this->filtername;
192    }
193
194    /**
195     * Get the filter parameters.
196     *
197     * @return  mixed
198     */
199    public function getParameters()
200    {
201        return $this->params;
202    }
203
204    /**
205     * Get the stream resource being filtered.
206     * Maybe available only during **filter** calls when the closing parameter
207     * is set to false.
208     *
209     * @return  resource
210     */
211    public function getStream()
212    {
213        return isset($this->stream) ? $this->stream : null;
214    }
215}
216