Hoa central
Bucket.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Stream;
38 
47 class Bucket
48 {
54  const IS_A_BRIGADE = true;
55 
61  const IS_A_STREAM = false;
62 
68  protected $_type = null;
69 
75  protected $_brigade = null;
76 
82  protected $_bucket = null;
83 
84 
85 
97  public function __construct(&$brigade, $is = self::IS_A_BRIGADE, $buffer = '')
98  {
99  $this->setType($is);
100 
101  if (self::IS_A_BRIGADE === $this->getType()) {
102  $this->setBrigade($brigade);
103  } else {
104  $this->setBucket(stream_bucket_new($brigade, $buffer));
105  $bucket = $this->getBucket();
106  $this->setBrigade($bucket);
107  }
108 
109  return;
110  }
111 
118  public function eob()
119  {
120  $this->_bucket = null;
121 
122  return false == $this->getBucket();
123  }
124 
131  public function append(Bucket $bucket)
132  {
133  stream_bucket_append($this->getBrigade(), $bucket->getBucket());
134 
135  return;
136  }
137 
144  public function prepend(Bucket $bucket)
145  {
146  stream_bucket_prepend($this->getBrigade(), $bucket->getBucket());
147 
148  return;
149  }
150 
157  protected function setType($type)
158  {
159  $old = $this->_type;
160  $this->_type = $type;
161 
162  return $old;
163  }
164 
170  public function getType()
171  {
172  return $this->_type;
173  }
174 
181  public function setData($data)
182  {
183  $old = $this->getBucket()->data;
184  $this->getBucket()->data = $data;
185  $this->getBucket()->datalen = strlen($this->getBucket()->data);
186 
187  return $old;
188  }
189 
195  public function getData()
196  {
197  if (null === $this->getBucket()) {
198  return null;
199  }
200 
201  return $this->getBucket()->data;
202  }
203 
209  public function getLength()
210  {
211  if (null === $this->getBucket()) {
212  return 0;
213  }
214 
215  return $this->getBucket()->datalen;
216  }
217 
224  protected function setBrigade(&$brigade)
225  {
226  $old = $this->_brigade;
227  $this->_brigade = $brigade;
228 
229  return $old;
230  }
231 
237  public function getBrigade()
238  {
239  return $this->_brigade;
240  }
241 
248  protected function setBucket($bucket)
249  {
250  $old = $this->_bucket;
251  $this->_bucket = $bucket;
252 
253  return $old;
254  }
255 
261  protected function getBucket()
262  {
263  if (null === $this->_bucket && self::IS_A_BRIGADE === $this->getType()) {
264  $this->_bucket = stream_bucket_make_writeable($this->getBrigade());
265  }
266 
267  return $this->_bucket;
268  }
269 }
setBucket($bucket)
Definition: Bucket.php:248
setType($type)
Definition: Bucket.php:157
append(Bucket $bucket)
Definition: Bucket.php:131
setData($data)
Definition: Bucket.php:181
const IS_A_BRIGADE
Definition: Bucket.php:54
const IS_A_STREAM
Definition: Bucket.php:61
__construct(&$brigade, $is=self::IS_A_BRIGADE, $buffer= '')
Definition: Bucket.php:97
setBrigade(&$brigade)
Definition: Bucket.php:224
prepend(Bucket $bucket)
Definition: Bucket.php:144