[ Index ] |
PHP Cross Reference of Unnamed Project |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Copyright 2014 Horde LLC (http://www.horde.org/) 4 * 5 * See the enclosed file COPYING for license information (LGPL). If you 6 * did not receive this file, see http://www.horde.org/licenses/lgpl21. 7 * 8 * @category Horde 9 * @copyright 2014 Horde LLC 10 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 11 * @package Stream 12 */ 13 14 /** 15 * Implementation of Horde_Stream that uses a PHP native string variable 16 * until a certain size is reached, at which point it converts storage to a 17 * PHP temporary stream. 18 * 19 * @author Michael Slusarz <slusarz@horde.org> 20 * @category Horde 21 * @copyright 2014 Horde LLC 22 * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1 23 * @package Stream 24 * @since 1.6.0 25 * 26 * @property-read boolean $use_stream If true, the object is using a PHP temp 27 * stream internally. 28 */ 29 class Horde_Stream_TempString extends Horde_Stream_Temp 30 { 31 /** 32 * String stream object. 33 * 34 * @var Horde_Stream_String 35 */ 36 protected $_string; 37 38 /** 39 */ 40 public function __construct(array $opts = array()) 41 { 42 parent::__construct($opts); 43 44 $temp = ''; 45 $this->_string = new Horde_Stream_String(array( 46 'string' => $temp 47 )); 48 } 49 50 /** 51 */ 52 protected function _init() 53 { 54 if (!isset($this->_params['max_memory'])) { 55 $this->_params['max_memory'] = 2097152; 56 } 57 58 if (!$this->_string) { 59 parent::_init(); 60 } 61 } 62 63 /** 64 */ 65 public function __get($name) 66 { 67 switch ($name) { 68 case 'stream': 69 if ($this->_string) { 70 return $this->_string->stream; 71 } 72 break; 73 74 case 'use_stream': 75 return !(bool)$this->_string; 76 } 77 78 return parent::__get($name); 79 } 80 81 /** 82 */ 83 public function __set($name, $value) 84 { 85 switch ($name) { 86 case 'utf8_char': 87 if ($this->_string) { 88 $this->_string->utf8_char = $value; 89 } 90 break; 91 } 92 93 parent::__set($name, $value); 94 } 95 96 /** 97 */ 98 public function __clone() 99 { 100 if ($this->_string) { 101 $this->_string = clone $this->_string; 102 } else { 103 parent::__clone(); 104 } 105 } 106 107 /** 108 */ 109 public function __toString() 110 { 111 return $this->_string 112 ? strval($this->_string) 113 : parent::__toString(); 114 } 115 116 /** 117 */ 118 public function add($data, $reset = false) 119 { 120 if ($this->_string && is_string($data)) { 121 if ((strlen($data) + $this->_string->length()) < $this->_params['max_memory']) { 122 $this->_string->add($data, $reset); 123 return; 124 } 125 126 parent::_init(); 127 parent::add(strval($this->_string)); 128 $this->seek($this->_string->pos(), false); 129 unset($this->_string); 130 } 131 132 parent::add($data, $reset); 133 } 134 135 /** 136 */ 137 public function length($utf8 = false) 138 { 139 return $this->_string 140 ? $this->_string->length($utf8) 141 : parent::length($utf8); 142 } 143 144 /** 145 */ 146 public function getToChar($end, $all = true) 147 { 148 return $this->_string 149 ? $this->_string->getToChar($end, $all) 150 : parent::getToChar($end, $all); 151 } 152 153 154 /** 155 */ 156 public function peek($length = 1) 157 { 158 return $this->_string 159 ? $this->_string->peek($length) 160 : parent::peek($length); 161 } 162 163 /** 164 */ 165 public function search($char, $reverse = false, $reset = true) 166 { 167 return $this->_string 168 ? $this->_string->search($char, $reverse, $reset) 169 : parent::search($char, $reverse, $reset); 170 } 171 172 /** 173 */ 174 public function getString($start = null, $end = null) 175 { 176 return $this->_string 177 ? $this->_string->getString($start, $end) 178 : parent::getString($start, $end); 179 } 180 181 /** 182 */ 183 public function substring($start = 0, $length = null, $char = false) 184 { 185 return $this->_string 186 ? $this->_string->substring($start, $length, $char) 187 : parent::substring($start, $length, $char); 188 } 189 190 /** 191 */ 192 public function getChar() 193 { 194 return $this->_string 195 ? $this->_string->getChar() 196 : parent::getChar(); 197 } 198 199 /** 200 */ 201 public function pos() 202 { 203 return $this->_string 204 ? $this->_string->pos() 205 : parent::pos(); 206 } 207 208 /** 209 */ 210 public function rewind() 211 { 212 return $this->_string 213 ? $this->_string->rewind() 214 : parent::rewind(); 215 } 216 217 /** 218 */ 219 public function seek($offset = 0, $curr = true, $char = false) 220 { 221 return $this->_string 222 ? $this->_string->seek($offset, $curr, $char) 223 : parent::seek($offset, $curr, $char); 224 } 225 226 /** 227 */ 228 public function end($offset = 0) 229 { 230 return $this->_string 231 ? $this->_string->end($offset) 232 : parent::end($offset); 233 } 234 235 /** 236 */ 237 public function eof() 238 { 239 return $this->_string 240 ? $this->_string->eof() 241 : parent::eof(); 242 } 243 244 /* Serializable methods. */ 245 246 /** 247 */ 248 public function serialize() 249 { 250 if ($this->_string) { 251 $data = array( 252 $this->_string, 253 $this->_params 254 ); 255 } else { 256 $data = parent::serialize(); 257 } 258 259 return serialize($data); 260 } 261 262 /** 263 */ 264 public function unserialize($data) 265 { 266 $data = unserialize($data); 267 if ($data[0] instanceof Horde_Stream_String) { 268 $this->_string = $data[0]; 269 $this->_params = $data[1]; 270 } else { 271 parent::unserialize($data); 272 } 273 } 274 275 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Thu Aug 11 10:00:09 2016 | Cross-referenced by PHPXref 0.7.1 |