Config Object PHPDoc

This commit is contained in:
Michael Yoo 2013-12-19 18:42:45 +10:30
parent 5dc52ad97d
commit a141ea5018

View File

@ -28,11 +28,29 @@ define("CONFIG_YAML", 2); // .yml, .yaml
define("CONFIG_SERIALIZED", 4); // .sl define("CONFIG_SERIALIZED", 4); // .sl
define("CONFIG_LIST", 5); // .txt, .list define("CONFIG_LIST", 5); // .txt, .list
/**
* Class Config
*
* Config Class for simple config manipulation of multiple formats.
*/
class Config{ class Config{
/**
* @var array
*/
private $config; private $config;
/**
* @var string
*/
private $file; private $file;
/**
* @var boolean
*/
private $correct; private $correct;
/**
* @var integer
*/
private $type = CONFIG_DETECT; private $type = CONFIG_DETECT;
public static $formats = array( public static $formats = array(
"properties" => CONFIG_PROPERTIES, "properties" => CONFIG_PROPERTIES,
"cnf" => CONFIG_CNF, "cnf" => CONFIG_CNF,
@ -49,6 +67,13 @@ class Config{
"txt" => CONFIG_LIST, "txt" => CONFIG_LIST,
"list" => CONFIG_LIST, "list" => CONFIG_LIST,
); );
/**
* @param string $file
* @param int $type
* @param array $default
* @param null|boolean $correct
*/
public function __construct($file, $type = CONFIG_DETECT, $default = array(), &$correct = null){ public function __construct($file, $type = CONFIG_DETECT, $default = array(), &$correct = null){
$this->load($file, $type, $default); $this->load($file, $type, $default);
$correct = $this->check(); $correct = $this->check();
@ -62,6 +87,13 @@ class Config{
$correct = $this->check(); $correct = $this->check();
} }
/**
* @param string $file
* @param int $type
* @param array $default
*
* @return boolean
*/
public function load($file, $type = CONFIG_DETECT, $default = array()){ public function load($file, $type = CONFIG_DETECT, $default = array()){
$this->correct = true; $this->correct = true;
$this->type = (int) $type; $this->type = (int) $type;
@ -119,10 +151,16 @@ class Config{
return true; return true;
} }
/**
* @return boolean
*/
public function check(){ public function check(){
return $this->correct === true; return $this->correct === true;
} }
/**
* @return boolean
*/
public function save(){ public function save(){
if($this->correct === true){ if($this->correct === true){
switch($this->type){ switch($this->type){
@ -150,22 +188,44 @@ class Config{
} }
} }
/**
* @param $k
*
* @return boolean|mixed
*/
public function &__get($k){ public function &__get($k){
return $this->get($k); return $this->get($k);
} }
/**
* @param $k
* @param $v
*/
public function __set($k, $v){ public function __set($k, $v){
return $this->set($k, $v); $this->set($k, $v);
} }
/**
* @param $k
*
* @return boolean
*/
public function __isset($k){ public function __isset($k){
return $this->exists($k); return $this->exists($k);
} }
/**
* @param $k
*/
public function __unset($k){ public function __unset($k){
return $this->remove($k); $this->remove($k);
} }
/**
* @param $k
*
* @return boolean|mixed
*/
public function &get($k){ public function &get($k){
if(isset($this->correct) and ($this->correct === false or !isset($this->config[$k]))){ if(isset($this->correct) and ($this->correct === false or !isset($this->config[$k]))){
$false = false; $false = false;
@ -174,16 +234,23 @@ class Config{
return $this->config[$k]; return $this->config[$k];
} }
/**
* @param $k
* @param bool $v
*/
public function set($k, $v = true){ public function set($k, $v = true){
$this->config[$k] = $v; $this->config[$k] = $v;
} }
/**
* @param array $v
*/
public function setAll($v){ public function setAll($v){
$this->config = $v; $this->config = $v;
} }
/** /**
* @param mixed $k * @param $k
* @param bool $lowercase If set, searches Config in single-case / lowercase. * @param bool $lowercase If set, searches Config in single-case / lowercase.
* *
* @return boolean * @return boolean
@ -198,14 +265,28 @@ class Config{
endif; endif;
} }
/**
* @param $k
*/
public function remove($k){ public function remove($k){
unset($this->config[$k]); unset($this->config[$k]);
} }
/**
* @param bool $keys
*
* @return array
*/
public function getAll($keys = false){ public function getAll($keys = false){
return ($keys === true ? array_keys($this->config):$this->config); return ($keys === true ? array_keys($this->config):$this->config);
} }
/**
* @param $default
* @param $data
*
* @return integer
*/
private function fillDefaults($default, &$data){ private function fillDefaults($default, &$data){
$changed = 0; $changed = 0;
foreach($default as $k => $v){ foreach($default as $k => $v){
@ -222,6 +303,9 @@ class Config{
return $changed; return $changed;
} }
/**
* @param $content
*/
private function parseList($content){ private function parseList($content){
foreach(explode("\n", trim(str_replace("\r\n", "\n", $content))) as $v){ foreach(explode("\n", trim(str_replace("\r\n", "\n", $content))) as $v){
$v = trim($v); $v = trim($v);
@ -232,6 +316,9 @@ class Config{
} }
} }
/**
* @return string
*/
private function writeProperties(){ private function writeProperties(){
$content = "#Properties Config file\r\n#".date("D M j H:i:s T Y")."\r\n"; $content = "#Properties Config file\r\n#".date("D M j H:i:s T Y")."\r\n";
foreach($this->config as $k => $v){ foreach($this->config as $k => $v){
@ -245,6 +332,9 @@ class Config{
return $content; return $content;
} }
/**
* @param $content
*/
private function parseProperties($content){ private function parseProperties($content){
if(preg_match_all('/([a-zA-Z0-9\-_\.]*)=([^\r\n]*)/u', $content, $matches) > 0){ //false or 0 matches if(preg_match_all('/([a-zA-Z0-9\-_\.]*)=([^\r\n]*)/u', $content, $matches) > 0){ //false or 0 matches
foreach($matches[1] as $i => $k){ foreach($matches[1] as $i => $k){