# TransparentFile.class.php

Go to the documentation of this file.

    00001 <?php
    00002 /***************************************************************************
    00003  *   Copyright (C) 2007 by Ivan Y. Khvostishkov                            *
    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
    00012     final class TransparentFile
    00013     {
    00014         private $path       = null;
    00015         private $rawData    = null;
    00016
    00017         private $tempFile   = null;
    00018
    00022         public static function create()
    00023         {
    00024             return new self;
    00025         }
    00026
    00030         public function setPath($path)
    00031         {
    00032             if (!is_readable($path))
    00033                 throw new WrongArgumentException(
    00034                     "cannot open source file {$path}"
    00035                 );
    00036
    00037             $this->path = $path;
    00038
    00039             $this->tempFile = null;
    00040             $this->rawData = null;
    00041
    00042             return $this;
    00043         }
    00044
    00045         public function getPath()
    00046         {
    00047             if (!$this->path && $this->rawData) {
    00048                 $this->tempFile = new TempFile();
    00049
    00050                 $this->path = $this->tempFile->getPath();
    00051
    00052                 file_put_contents($this->path, $this->rawData);
    00053             }
    00054
    00055             return $this->path;
    00056         }
    00057
    00061         public function setRawData($rawData)
    00062         {
    00063             $this->rawData = $rawData;
    00064
    00065             $this->tempFile = null;
    00066             $this->path = null;
    00067
    00068             return $this;
    00069         }
    00070
    00071         public function getRawData()
    00072         {
    00073             if (!$this->rawData && $this->path) {
    00074                 $this->rawData = file_get_contents($this->path);
    00075             }
    00076
    00077             return $this->rawData;
    00078         }
    00079
    00080         public function getSize()
    00081         {
    00082             if ($this->rawData)
    00083                 return strlen($this->rawData);
    00084             elseif ($this->path)
    00085                 return filesize($this->path);
    00086
    00087             return null;
    00088         }
    00089     }
    00090 ?>