I'm looking for code correctness and improvements for the following code. It's my first step into class based programing.
What the class does, is simply to read a directory (path is passed as constructor argument) and find all sub directories matched by the search pattern. Then it returns a random directory (strimg) based on the result set.
I assuming this directory structure:
Directory structure
backend
img
kombi-1
kombi-2
kombi-3
PHP
/**
* RandomImages
*
* This class reads based on the passed argument, all directories
* matched by the pattern ($pattern) and returns a random path
* from the result.
*/
class RandomImages {
private $array;
private $path;
private $pattern = "/kombis/*";
/**
* __construct function.
*
* @access public
* @param mixed $path
*/
public function __construct($location) {
$this->location = $this->cleanLocation($location);
}
/**
* getDirectories function.
*
* Return an array of directories matched by $pattern
*
* @access private
* @return array
*/
private function getDirectories(){
return glob( $this->location . $this->pattern , GLOB_ONLYDIR );
}
/**
* getRandomKey function.
*
* @access private
* @return int
*/
private function getRandomKey() {
$array = $this->getDirectories();
return array_rand($array, 1);
}
/**
* cleanLocation function.
*
* Remove safely the last /
*
* @access private
* @param mixed $string
* @return string
*/
private function cleanLocation($string) {
return rtrim($string, '/');
}
/**
* getRandomPath function.
*
* @access public
* @return string
*/
public function getRandomPath() {
$array = $this->getDirectories();
return $array[$this->getRandomKey()];
}
}
// Init
$path = new RandomImages('./backend/img');
// Save path to prevent false combinations
$randomPath = $path->getRandomPath();
// Build link and store string
$pattern = $randomPath . '/pattern.png';
$curtain = $randomPath . '/curtain.png';
UPDATED CLASS
<?php
/**
* RandomImages
*
* This class reads based on the passed argument, all directories
* matched by the pattern ($pattern) and returns a random path
* from the result.
*/
class RandomImages {
/**
* Basepath
*
* @var string Trailing slash is stripped
*/
private $location;
/**
* __construct
*
* @param string $path
*/
public function __construct($location) {
$this->location = $this->stripTrailingSlash($location);
}
/**
* setPattern
*
* @return void
*/
public function setPattern($pattern) {
$this->pattern = $pattern;
}
/**
* getPattern.
*
* @return string
*/
public function getPattern() {
return $this->pattern;
}
/**
* getDirectories
*
* Returns an array of directories matched by setPattern()
*
* @return array
*/
private function getDirectories(){
return glob( $this->location . $this->pattern , GLOB_ONLYDIR );
}
/**
* stripTrailingSlash
*
* Remove safely the last /
*
* @param mixed $string
* @return string
*/
private function stripTrailingSlash($string) {
return rtrim($string, '/');
}
/**
* getRandomPath
*
* @return string
*/
public function getRandomPath() {
$array = $this->getDirectories();
return $array[array_rand($array, 1)];
}
}
// Init
$path = new RandomImages('./backend/img/kombis');
$path->setPattern('/kombi*');