Okay here is the class after some more additions.
I think I have set and get methods now for every property that makes sense to have one for.
I have two methods with die statements. Am I correct in assuming these would be better written with try and catch? And if so how? How's my naming conventions?
d11 how would I implement _call and _get in this class?
Code: Select all
class globFiles{
protected $ext = array();
private $files = array();
private $path;
private $pattern;
private $filesWithInfo = array();
private $imageExt = array('jpg',
'JPEG',
'bmp',
'BMP',
'png',
'PNG',
'GIF',
'gif',
'giff' );
private $fileProperties = array ( 'basename',
'realpath',
'dirname',
'filesize',
'filectime',
'fileowner',
'filegroup',
'is_writeable',
'is_readable',
'is_executable');
private function globbers(){
if(!empty($this->pattern)) {
$this->files = glob($this->path.$this->pattern);
}
elseif(!empty($this->ext)){
$extPattern = implode( ',*.', $this->ext);
$pattern = '{*.';
$pattern .= $extPattern.'}';
$this->files = glob( $this->path.$pattern, GLOB_BRACE);
}else{
die('Pattern or ext list required.');
}
}
private function getGlobFileExt($file){
return substr($file, strrpos($file, '.')+1);
}
private function globInfo($file){
//gets file specific information
//check the extension and try to get image info if possible;
foreach ($this->fileProperties as $func){
$this->filesWithInfo[$func] = $func($file);
}
}
private function addGlobInfo(){
//loop through files array add filectime() filesize() filemode() and image info if possible.
foreach ($this->files as $key =>$val){
$ext = $this->getGlobFileExt($val);
if(in_array($ext, $this->imageExt)){
//try to get image information
if($imgInfo = getimagesize($this->file)){
//make the imageinfo array tied to useful keys
//add it to $fileWithInfo
foreach ($imgInfo as $k => $info){
switch($k){
case 0:
$this->fileWithInfo['image_width']= $info;
break;
case 1:
$this->fileWithInfo['image_height']= $info;
break;
case 2:
switch ($info){
case 1:
$type = 'GIF';
break;
case 2:
$type = 'JPG';
break;
case 3:
$type = 'PNG';
break;
case 4:
$type = 'SWF';
break;
case 5:
$type = 'PSD';
break;
case 6:
$type = 'BMP';
break;
case 7:
$type = 'TIFF(intel byte order)';
break;
case 8:
$type = 'TIFF(motorola byte order)';
break;
case 9:
$type = 'JPC';
break;
}
$this->fileWithInfo['image_type']= $type;
break;
case 3:
$this->fileWithInfo['image_wxh'] = $info;
break;
}
}
}
}
//try to get the average info
$this->globInfo($val);
$this->filesWithInfo['glob_path']= $val;
$this->files[$key]= $this->filesWithInfo;
}
}
public function setFileProperties( $properties, $add = false){
if( is_array($properties) ){
if($add === false){
$this->fileProperties = $properties;
} else {
$this->fileProperties = array_merge($properties, $this->fileProperties);
}
}
}
public function getGlobInfo(){
$this->addGlobInfo();
return $this->files;
}
public function setGlobFiles($filesArray){
//override the $this->files array with one of our own making;
if(is_array($filesArray)){
$this->files=$filesArray;
}
}
public function setGlobPath($path){
if (!strcmp($this->path{strlen($this->path)-1}, '/' )==0 ){
$path .= '/';
}
if(is_dir($path)){
$this->path = $path;
return true;
} else {
return false;
}
}
public function getGlob(){
$this->globbers();
return $this->files;
}
public function setGlobPattern($pattern){
$this->pattern = $pattern;
}
public function setGlobExt($ext){
if(is_array($ext)){
$this->ext = $ext;
} else {
//throw exception?
die('oh smurf! I am not an array');
}
}
}