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