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\LateComputed.
43 *
44 * A late computed filter computes the data when closing the filtering.
45 *
46 * @copyright  Copyright © 2007-2017 Hoa community
47 * @license    New BSD License
48 */
49abstract class LateComputed extends Basic
50{
51    /**
52     * Buffer.
53     *
54     * @var string
55     */
56    protected $_buffer = null;
57
58
59
60    /**
61     * Filter data.
62     * This method is called whenever data is read from or written to the attach
63     * stream.
64     *
65     * @param   resource  $in           A resource pointing to a bucket brigade
66     *                                  which contains one or more bucket
67     *                                  objects containing data to be filtered.
68     * @param   resource  $out          A resource pointing to a second bucket
69     *                                  brigade into which your modified buckets
70     *                                  should be replaced.
71     * @param   int       &$consumed    Which must always be declared by
72     *                                  reference, should be incremented by the
73     *                                  length of the data which your filter
74     *                                  reads in and alters.
75     * @param   bool      $closing      If the stream is in the process of
76     *                                  closing (and therefore this is the last
77     *                                  pass through the filterchain), the
78     *                                  closing parameter will be set to true.
79     * @return  int
80     */
81    public function filter($in, $out, &$consumed, $closing)
82    {
83        $return  = self::FEED_ME;
84        $iBucket = new Stream\Bucket($in);
85
86        while (false === $iBucket->eob()) {
87            $this->_buffer .= $iBucket->getData();
88            $consumed      += $iBucket->getLength();
89        }
90
91        if (null !== $consumed) {
92            $return = self::PASS_ON;
93        }
94
95        if (true === $closing) {
96            $stream = $this->getStream();
97            $this->compute();
98            $bucket = new Stream\Bucket(
99                $stream,
100                Stream\Bucket::IS_A_STREAM,
101                $this->_buffer
102            );
103            $oBucket = new Stream\Bucket($out);
104            $oBucket->append($bucket);
105
106            $return        = self::PASS_ON;
107            $this->_buffer = null;
108        }
109
110        return $return;
111    }
112
113    /**
114     * Compute the whole data (stored in $this->_buffer).
115     *
116     * @return  string
117     */
118    abstract protected function compute();
119}
120