Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

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