Overview

Packages

  • Stream
    • Wrapper

Classes

  • Horde_Stream_Wrapper_Combine
  • Horde_Stream_Wrapper_String

Interfaces

  • Horde_Stream_Wrapper_CombineStream
  • Horde_Stream_Wrapper_StringStream
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
  4:  *
  5:  * @author   Chuck Hagenbuch <chuck@horde.org>
  6:  * @license  http://www.horde.org/licenses/bsd BSD
  7:  * @category Horde
  8:  * @package  Stream_Wrapper
  9:  */
 10: 
 11: /**
 12:  * @author   Chuck Hagenbuch <chuck@horde.org>
 13:  * @license  http://www.horde.org/licenses/bsd BSD
 14:  * @category Horde
 15:  * @package  Stream_Wrapper
 16:  */
 17: class Horde_Stream_Wrapper_String
 18: {
 19:     /**
 20:      * @var resource
 21:      */
 22:     public $context;
 23: 
 24:     /**
 25:      * @var string
 26:      */
 27:     protected $_string;
 28: 
 29:     /**
 30:      * @var integer
 31:      */
 32:     protected $_length;
 33: 
 34:     /**
 35:      * @var integer
 36:      */
 37:     protected $_position;
 38: 
 39:     /**
 40:      * @param string $path
 41:      * @param string $mode
 42:      * @param integer $options
 43:      * @param string &$opened_path
 44:      */
 45:     public function stream_open($path, $mode, $options, &$opened_path)
 46:     {
 47:         $options = stream_context_get_options($this->context);
 48:         if (empty($options['horde-string']['string']) || ! $options['horde-string']['string'] instanceof Horde_Stream_Wrapper_StringStream) {
 49:             throw new Exception('String streams must be created using the Horde_Stream_Wrapper_StringStream interface');
 50:         }
 51: 
 52:         $this->_string =& $options['horde-string']['string']->getString();
 53:         if (is_null($this->_string)) {
 54:             return false;
 55:         }
 56: 
 57:         $this->_length = strlen($this->_string);
 58:         $this->_position = 0;
 59:         return true;
 60:     }
 61: 
 62:     /**
 63:      * @param integer $count
 64:      *
 65:      * @return string
 66:      */
 67:     public function stream_read($count)
 68:     {
 69:         $current = $this->_position;
 70:         $this->_position += $count;
 71:         return substr($this->_string, $current, $count);
 72:     }
 73: 
 74:     /**
 75:      * @param string $data
 76:      *
 77:      * @return integer
 78:      */
 79:     public function stream_write($data)
 80:     {
 81:         return strlen($data);
 82:     }
 83: 
 84:     /**
 85:      * @return integer
 86:      */
 87:     public function stream_tell()
 88:     {
 89:         return $this->_position;
 90:     }
 91: 
 92:     /**
 93:      * @return boolean
 94:      */
 95:     public function stream_eof()
 96:     {
 97:         return ($this->_position > $this->_length);
 98:     }
 99: 
100:     /**
101:      * @see streamWrapper::stream_stat()
102:      *
103:      * @return array
104:      */
105:     public function stream_stat()
106:     {
107:         return array(
108:             'dev' => 0,
109:             'ino' => 0,
110:             'mode' => 0,
111:             'nlink' => 0,
112:             'uid' => 0,
113:             'gid' => 0,
114:             'rdev' => 0,
115:             'size' => $this->_length,
116:             'atime' => 0,
117:             'mtime' => 0,
118:             'ctime' => 0,
119:             'blksize' => 0,
120:             'blocks' => 0
121:         );
122:     }
123: 
124:     /**
125:      * @param integer $offset
126:      * @param integer $whence SEEK_SET, SEEK_CUR, or SEEK_END
127:      */
128:     public function stream_seek($offset, $whence)
129:     {
130:         if ($offset > $this->_length) {
131:             return false;
132:         }
133: 
134:         switch ($whence) {
135:         case SEEK_SET:
136:             $this->_position = $offset;
137:             break;
138: 
139:         case SEEK_CUR:
140:             $target = $this->_position + $offset;
141:             if ($target < $this->_length) {
142:                 $this->_position = $target;
143:             } else {
144:                 return false;
145:             }
146:             break;
147: 
148:         case SEEK_END:
149:             $this->_position = $this->_length - $offset;
150:             break;
151:         }
152: 
153:         return true;
154:     }
155: 
156: }
157: 
API documentation generated by ApiGen