StringReader.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2007 by Dmitry A. Lomash                                *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: StringReader.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class StringReader extends Reader
00017     {
00018         protected $string   = null;
00019         protected $length   = null;
00020         
00021         protected $next     = 0;
00022         protected $mark     = 0;
00023         
00027         public static function create($string)
00028         {
00029             return new self($string);
00030         }
00031         
00032         public function __construct($string)
00033         {
00034             $this->string = $string;
00035             $this->length = mb_strlen($this->string);
00036         }
00037         
00041         public function close()
00042         {
00043             $this->string = null;
00044             
00045             return $this;   
00046         }
00047         
00048         public function read($count)
00049         {
00050             $this->ensureOpen();
00051             
00052             if ($this->next >= $this->length)
00053                 return null;
00054             
00055             $result = mb_substr($this->string, $this->next, $count);
00056             
00057             $this->next += $count;
00058             
00059             return $result;
00060         }
00061         
00065         public function mark()
00066         {
00067             $this->ensureOpen();
00068             
00069             $this->mark = $this->next;
00070             
00071             return $this;
00072         }
00073         
00074         public function markSupported()
00075         {
00076             return true;
00077         }
00078         
00082         public function reset()
00083         {
00084             $this->ensureOpen();
00085             
00086             $this->next = $this->mark;
00087             
00088             return $this;
00089         }
00090         
00091         public function skip($count)
00092         {
00093             $this->ensureOpen();
00094             
00095             if ($this->isEof())
00096                 return 0;
00097             
00098             $actualSkip =
00099                 max(
00100                     -$this->next,
00101                     min($this->length - $this->next, $count)
00102                 );
00103             
00104             $this->next += $actualSkip;
00105             
00106             return $actualSkip;
00107         }
00108         
00109         public function isEof()
00110         {
00111             return ($this->next >= $this->length);
00112         }
00113         
00114         public function getWhole()
00115         {
00116             return $this->read($this->length - $this->next);
00117         }
00118         
00119         /* void */ private function ensureOpen()
00120         {
00121             if ($this->string === null)
00122                 throw new IOException('Stream closed');
00123         }
00124     }
00125 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4