Lob.php

Go to the documentation of this file.
00001 <?php 00002 /* 00003 * $Id: Lob.php,v 1.10 2004/03/20 04:16:50 hlellelid Exp $ 00004 * 00005 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00006 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00007 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00008 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00009 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00010 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00011 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00012 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00013 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00014 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00015 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00016 * 00017 * This software consists of voluntary contributions made by many individuals 00018 * and is licensed under the LGPL. For more information please see 00019 * <http://creole.phpdb.org>. 00020 */ 00021 00029 abstract class Lob { 00030 00037 protected $data; 00038 00043 protected $outFile; 00044 00049 protected $inFile; 00050 00059 private $modified = null; 00060 00066 public function __construct($data = null) 00067 { 00068 if ($data !== null) { 00069 $this->setContents($data); 00070 } 00071 } 00072 00078 public function getContents() 00079 { 00080 if ($this->data === null && $this->isFromFile()) { 00081 $this->readFromFile(); 00082 } 00083 return $this->data; 00084 } 00085 00093 public function setContents($data) 00094 { 00095 $this->data = $data; 00096 00097 if ($this->modified === null) { 00098 // if modified bit hasn't been set yet, 00099 // then it should now be set to FALSE, since 00100 // we just did inital population 00101 $this->modified = false; 00102 } elseif ($this->modified === false) { 00103 // if it was already FALSE, then it should 00104 // now be set to TRUE, since this is a subsequent 00105 // modfiication. 00106 $this->modified = true; 00107 } 00108 } 00109 00117 abstract public function dump(); 00118 00124 public function setInputFile($filePath) 00125 { 00126 $this->inFile = $filePath; 00127 } 00128 00133 public function getInputFile() 00134 { 00135 return $this->inFile; 00136 } 00137 00143 public function setOutputFile($filePath) 00144 { 00145 $this->outFile = $filePath; 00146 } 00147 00152 public function getOutputFile() 00153 { 00154 return $this->outFile; 00155 } 00156 00162 public function isFromFile() 00163 { 00164 return ($this->inFile !== null); 00165 } 00166 00176 public function readFromFile($file = null) 00177 { 00178 if ($file !== null) { 00179 $this->setInputFile($file); 00180 } 00181 if (!$this->inFile) { 00182 throw Exception('No file specified for read.'); 00183 } 00184 $data = @file_get_contents($this->inFile); 00185 if ($data === false) { 00186 throw new Exception('Unable to read from file: '.$this->inFile); 00187 } 00188 $this->setContents($data); 00189 } 00190 00191 00199 public function writeToFile($file = null) 00200 { 00201 if ($file !== null) { 00202 $this->setOutputFile($file); 00203 } 00204 if (!$this->outFile) { 00205 throw new Exception('No file specified for write'); 00206 } 00207 if ($this->data === null) { 00208 throw new Exception('No data to write to file'); 00209 } 00210 if (false === @file_put_contents($this->outFile, $this->data)) { 00211 throw new Exception('Unable to write to file: '.$this->outFile); 00212 } 00213 } 00214 00219 public function __toString() 00220 { 00221 return $this->getContents(); 00222 } 00223 00228 public function setModified($b) 00229 { 00230 $this->modified = $b; 00231 } 00232 00238 public function isModified() 00239 { 00240 // cast it so that NULL will also eval to false 00241 return (boolean) $this->modified; 00242 } 00243 }

This file is part of the Creole[php5] library.


Copyright © 2004 Hans Lellelid  
Creole[php5] CVS