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 
00015     final class StringReader extends Reader
00016     {
00017         private $string     = null;
00018         private $length     = null;
00019         
00020         private $next       = 0;
00021         private $mark       = 0;
00022         
00026         public static function create($string)
00027         {
00028             return new self($string);
00029         }
00030         
00031         public function __construct($string)
00032         {
00033             $this->string = $string;
00034             $this->length = mb_strlen($this->string);
00035         }
00036         
00040         public function close()
00041         {
00042             $this->string = null;
00043             
00044             return $this;   
00045         }
00046         
00047         public function read($count)
00048         {
00049             $this->ensureOpen();
00050             
00051             if ($this->next >= $this->length)
00052                 return null;
00053             
00054             $result = mb_substr($this->string, $this->next, $count);
00055             
00056             $this->next += $count;
00057             
00058             return $result;
00059         }
00060         
00064         public function mark()
00065         {
00066             $this->ensureOpen();
00067             
00068             $this->mark = $this->next;
00069             
00070             return $this;
00071         }
00072         
00073         public function markSupported()
00074         {
00075             return true;
00076         }
00077         
00081         public function reset()
00082         {
00083             $this->ensureOpen();
00084             
00085             $this->next = $this->mark;
00086             
00087             return $this;
00088         }
00089         
00090         public function skip($count)
00091         {
00092             $this->ensureOpen();
00093             
00094             if ($this->isEof())
00095                 return 0;
00096             
00097             $actualSkip =
00098                 max(
00099                     -$this->next,
00100                     min($this->length - $this->next, $count)
00101                 );
00102             
00103             $this->next += $actualSkip;
00104             
00105             return $actualSkip;
00106         }
00107         
00108         public function isEof()
00109         {
00110             return ($this->next >= $this->length);
00111         }
00112         
00113         public function getWhole()
00114         {
00115             return $this->read($this->length - $this->next);
00116         }
00117         
00118         /* void */ private function ensureOpen()
00119         {
00120             if ($this->string === null)
00121                 throw new IOException('Stream closed');
00122         }
00123     }
00124 ?>