[ Index ]

MailPress 7.2

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/composer/vendor/swiftmailer/swiftmailer/lib/classes/Swift/CharacterStream/ -> NgCharacterStream.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of SwiftMailer.
   5   * (c) 2004-2009 Chris Corbyn
   6   *
   7   * For the full copyright and license information, please view the LICENSE
   8   * file that was distributed with this source code.
   9   */
  10  
  11  /**
  12   * A CharacterStream implementation which stores characters in an internal array.
  13   *
  14   * @author     Xavier De Cock <xdecock@gmail.com>
  15   */
  16  class Swift_CharacterStream_NgCharacterStream implements Swift_CharacterStream
  17  {
  18      /**
  19       * The char reader (lazy-loaded) for the current charset.
  20       *
  21       * @var Swift_CharacterReader
  22       */
  23      private $charReader;
  24  
  25      /**
  26       * A factory for creating CharacterReader instances.
  27       *
  28       * @var Swift_CharacterReaderFactory
  29       */
  30      private $charReaderFactory;
  31  
  32      /**
  33       * The character set this stream is using.
  34       *
  35       * @var string
  36       */
  37      private $charset;
  38  
  39      /**
  40       * The data's stored as-is.
  41       *
  42       * @var string
  43       */
  44      private $datas = '';
  45  
  46      /**
  47       * Number of bytes in the stream.
  48       *
  49       * @var int
  50       */
  51      private $datasSize = 0;
  52  
  53      /**
  54       * Map.
  55       *
  56       * @var mixed
  57       */
  58      private $map;
  59  
  60      /**
  61       * Map Type.
  62       *
  63       * @var int
  64       */
  65      private $mapType = 0;
  66  
  67      /**
  68       * Number of characters in the stream.
  69       *
  70       * @var int
  71       */
  72      private $charCount = 0;
  73  
  74      /**
  75       * Position in the stream.
  76       *
  77       * @var int
  78       */
  79      private $currentPos = 0;
  80  
  81      /**
  82       * Constructor.
  83       *
  84       * @param string $charset
  85       */
  86      public function __construct(Swift_CharacterReaderFactory $factory, $charset)
  87      {
  88          $this->setCharacterReaderFactory($factory);
  89          $this->setCharacterSet($charset);
  90      }
  91  
  92      /* -- Changing parameters of the stream -- */
  93  
  94      /**
  95       * Set the character set used in this CharacterStream.
  96       *
  97       * @param string $charset
  98       */
  99      public function setCharacterSet($charset)
 100      {
 101          $this->charset = $charset;
 102          $this->charReader = null;
 103          $this->mapType = 0;
 104      }
 105  
 106      /**
 107       * Set the CharacterReaderFactory for multi charset support.
 108       */
 109      public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory)
 110      {
 111          $this->charReaderFactory = $factory;
 112      }
 113  
 114      /**
 115       * @see Swift_CharacterStream::flushContents()
 116       */
 117      public function flushContents()
 118      {
 119          $this->datas = null;
 120          $this->map = null;
 121          $this->charCount = 0;
 122          $this->currentPos = 0;
 123          $this->datasSize = 0;
 124      }
 125  
 126      /**
 127       * @see Swift_CharacterStream::importByteStream()
 128       */
 129      public function importByteStream(Swift_OutputByteStream $os)
 130      {
 131          $this->flushContents();
 132          $blocks = 512;
 133          $os->setReadPointer(0);
 134          while (false !== ($read = $os->read($blocks))) {
 135              $this->write($read);
 136          }
 137      }
 138  
 139      /**
 140       * @see Swift_CharacterStream::importString()
 141       *
 142       * @param string $string
 143       */
 144      public function importString($string)
 145      {
 146          $this->flushContents();
 147          $this->write($string);
 148      }
 149  
 150      /**
 151       * @see Swift_CharacterStream::read()
 152       *
 153       * @param int $length
 154       *
 155       * @return string
 156       */
 157      public function read($length)
 158      {
 159          if ($this->currentPos >= $this->charCount) {
 160              return false;
 161          }
 162          $ret = false;
 163          $length = ($this->currentPos + $length > $this->charCount) ? $this->charCount - $this->currentPos : $length;
 164          switch ($this->mapType) {
 165              case Swift_CharacterReader::MAP_TYPE_FIXED_LEN:
 166                  $len = $length * $this->map;
 167                  $ret = substr($this->datas,
 168                          $this->currentPos * $this->map,
 169                          $len);
 170                  $this->currentPos += $length;
 171                  break;
 172  
 173              case Swift_CharacterReader::MAP_TYPE_INVALID:
 174                  $ret = '';
 175                  for (; $this->currentPos < $length; ++$this->currentPos) {
 176                      if (isset($this->map[$this->currentPos])) {
 177                          $ret .= '?';
 178                      } else {
 179                          $ret .= $this->datas[$this->currentPos];
 180                      }
 181                  }
 182                  break;
 183  
 184              case Swift_CharacterReader::MAP_TYPE_POSITIONS:
 185                  $end = $this->currentPos + $length;
 186                  $end = $end > $this->charCount ? $this->charCount : $end;
 187                  $ret = '';
 188                  $start = 0;
 189                  if ($this->currentPos > 0) {
 190                      $start = $this->map['p'][$this->currentPos - 1];
 191                  }
 192                  $to = $start;
 193                  for (; $this->currentPos < $end; ++$this->currentPos) {
 194                      if (isset($this->map['i'][$this->currentPos])) {
 195                          $ret .= substr($this->datas, $start, $to - $start).'?';
 196                          $start = $this->map['p'][$this->currentPos];
 197                      } else {
 198                          $to = $this->map['p'][$this->currentPos];
 199                      }
 200                  }
 201                  $ret .= substr($this->datas, $start, $to - $start);
 202                  break;
 203          }
 204  
 205          return $ret;
 206      }
 207  
 208      /**
 209       * @see Swift_CharacterStream::readBytes()
 210       *
 211       * @param int $length
 212       *
 213       * @return int[]
 214       */
 215      public function readBytes($length)
 216      {
 217          $read = $this->read($length);
 218          if (false !== $read) {
 219              $ret = array_map('ord', str_split($read, 1));
 220  
 221              return $ret;
 222          }
 223  
 224          return false;
 225      }
 226  
 227      /**
 228       * @see Swift_CharacterStream::setPointer()
 229       *
 230       * @param int $charOffset
 231       */
 232      public function setPointer($charOffset)
 233      {
 234          if ($this->charCount < $charOffset) {
 235              $charOffset = $this->charCount;
 236          }
 237          $this->currentPos = $charOffset;
 238      }
 239  
 240      /**
 241       * @see Swift_CharacterStream::write()
 242       *
 243       * @param string $chars
 244       */
 245      public function write($chars)
 246      {
 247          if (!isset($this->charReader)) {
 248              $this->charReader = $this->charReaderFactory->getReaderFor(
 249                  $this->charset);
 250              $this->map = [];
 251              $this->mapType = $this->charReader->getMapType();
 252          }
 253          $ignored = '';
 254          $this->datas .= $chars;
 255          $this->charCount += $this->charReader->getCharPositions(substr($this->datas, $this->datasSize), $this->datasSize, $this->map, $ignored);
 256          if (false !== $ignored) {
 257              $this->datasSize = strlen($this->datas) - strlen($ignored);
 258          } else {
 259              $this->datasSize = strlen($this->datas);
 260          }
 261      }
 262  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1