[ Index ]

MailPress 544

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

title

Body

[close]

/mp-includes/Swiftmailer/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 Swift_CharacterReaderFactory $factory
  85       * @param string                       $charset
  86       */
  87      public function __construct(Swift_CharacterReaderFactory $factory, $charset)
  88      {
  89          $this->setCharacterReaderFactory($factory);
  90          $this->setCharacterSet($charset);
  91      }
  92  
  93      /* -- Changing parameters of the stream -- */
  94  
  95      /**
  96       * Set the character set used in this CharacterStream.
  97       *
  98       * @param string $charset
  99       */
 100      public function setCharacterSet($charset)
 101      {
 102          $this->_charset = $charset;
 103          $this->_charReader = null;
 104          $this->_mapType = 0;
 105      }
 106  
 107      /**
 108       * Set the CharacterReaderFactory for multi charset support.
 109       *
 110       * @param Swift_CharacterReaderFactory $factory
 111       */
 112      public function setCharacterReaderFactory(Swift_CharacterReaderFactory $factory)
 113      {
 114          $this->_charReaderFactory = $factory;
 115      }
 116  
 117      /**
 118       * @see Swift_CharacterStream::flushContents()
 119       */
 120      public function flushContents()
 121      {
 122          $this->_datas = null;
 123          $this->_map = null;
 124          $this->_charCount = 0;
 125          $this->_currentPos = 0;
 126          $this->_datasSize = 0;
 127      }
 128  
 129      /**
 130       * @see Swift_CharacterStream::importByteStream()
 131       *
 132       * @param Swift_OutputByteStream $os
 133       */
 134      public function importByteStream(Swift_OutputByteStream $os)
 135      {
 136          $this->flushContents();
 137          $blocks = 512;
 138          $os->setReadPointer(0);
 139          while (false !== ($read = $os->read($blocks))) {
 140              $this->write($read);
 141          }
 142      }
 143  
 144      /**
 145       * @see Swift_CharacterStream::importString()
 146       *
 147       * @param string $string
 148       */
 149      public function importString($string)
 150      {
 151          $this->flushContents();
 152          $this->write($string);
 153      }
 154  
 155      /**
 156       * @see Swift_CharacterStream::read()
 157       *
 158       * @param int $length
 159       *
 160       * @return string
 161       */
 162      public function read($length)
 163      {
 164          if ($this->_currentPos >= $this->_charCount) {
 165              return false;
 166          }
 167          $ret = false;
 168          $length = ($this->_currentPos + $length > $this->_charCount)
 169            ? $this->_charCount - $this->_currentPos
 170            : $length;
 171          switch ($this->_mapType) {
 172              case Swift_CharacterReader::MAP_TYPE_FIXED_LEN:
 173                  $len = $length * $this->_map;
 174                  $ret = substr($this->_datas,
 175                          $this->_currentPos * $this->_map,
 176                          $len);
 177                  $this->_currentPos += $length;
 178                  break;
 179  
 180              case Swift_CharacterReader::MAP_TYPE_INVALID:
 181                  $end = $this->_currentPos + $length;
 182                  $end = $end > $this->_charCount
 183                      ? $this->_charCount
 184                      : $end;
 185                  $ret = '';
 186                  for (; $this->_currentPos < $length; ++$this->_currentPos) {
 187                      if (isset($this->_map[$this->_currentPos])) {
 188                          $ret .= '?';
 189                      } else {
 190                          $ret .= $this->_datas[$this->_currentPos];
 191                      }
 192                  }
 193                  break;
 194  
 195              case Swift_CharacterReader::MAP_TYPE_POSITIONS:
 196                  $end = $this->_currentPos + $length;
 197                  $end = $end > $this->_charCount
 198                      ? $this->_charCount
 199                      : $end;
 200                  $ret = '';
 201                  $start = 0;
 202                  if ($this->_currentPos > 0) {
 203                      $start = $this->_map['p'][$this->_currentPos - 1];
 204                  }
 205                  $to = $start;
 206                  for (; $this->_currentPos < $end; ++$this->_currentPos) {
 207                      if (isset($this->_map['i'][$this->_currentPos])) {
 208                          $ret .= substr($this->_datas, $start, $to - $start).'?';
 209                          $start = $this->_map['p'][$this->_currentPos];
 210                      } else {
 211                          $to = $this->_map['p'][$this->_currentPos];
 212                      }
 213                  }
 214                  $ret .= substr($this->_datas, $start, $to - $start);
 215                  break;
 216          }
 217  
 218          return $ret;
 219      }
 220  
 221      /**
 222       * @see Swift_CharacterStream::readBytes()
 223       *
 224       * @param int $length
 225       *
 226       * @return integer[]
 227       */
 228      public function readBytes($length)
 229      {
 230          $read = $this->read($length);
 231          if ($read !== false) {
 232              $ret = array_map('ord', str_split($read, 1));
 233  
 234              return $ret;
 235          }
 236  
 237          return false;
 238      }
 239  
 240      /**
 241       * @see Swift_CharacterStream::setPointer()
 242       *
 243       * @param int $charOffset
 244       */
 245      public function setPointer($charOffset)
 246      {
 247          if ($this->_charCount < $charOffset) {
 248              $charOffset = $this->_charCount;
 249          }
 250          $this->_currentPos = $charOffset;
 251      }
 252  
 253      /**
 254       * @see Swift_CharacterStream::write()
 255       *
 256       * @param string $chars
 257       */
 258      public function write($chars)
 259      {
 260          if (!isset($this->_charReader)) {
 261              $this->_charReader = $this->_charReaderFactory->getReaderFor(
 262                  $this->_charset);
 263              $this->_map = array();
 264              $this->_mapType = $this->_charReader->getMapType();
 265          }
 266          $ignored = '';
 267          $this->_datas .= $chars;
 268          $this->_charCount += $this->_charReader->getCharPositions(substr($this->_datas, $this->_datasSize), $this->_datasSize, $this->_map, $ignored);
 269          if ($ignored !== false) {
 270              $this->_datasSize = strlen($this->_datas) - strlen($ignored);
 271          } else {
 272              $this->_datasSize = strlen($this->_datas);
 273          }
 274      }
 275  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1