<?php
/**
*
* QBaseClass
* @package
* @subpackage
* @author Thomas Schäfer
* @since 26.07.2008 13:30:48
* @desc
*/
class QBaseClass extends stdClass {
/**
* register
*
* @var array
*/
private $register = array("has"=>true, "add"=>true, "set"=>true, "get"=>true, "merge"=>true);
/**
* register new method
*
* @param string $methodType
*/
public function addRegister($methodType) {
$this->register[$methodType] = true;
}
/**
* build method type
*
* @param string $word
* @return object
*/
public static function getMethodType($word) {
$tmp = explode("_", self::underscore($word));
$methodType = new stdClass();
$methodType->length = strlen($tmp[0]);
$methodType->type = $tmp[0];
$methodType->name = substr($word, $methodType->length);
return $methodType;
}
/**
* helper method
*
* @param string $word
* @return string
*/
public static function underscore($word=null)
{
$tmp = self::replace($word, array('/([A-Z]+)([A-Z][a-z])/' => '\\1_\\2', '/([a-z\d])([A-Z])/' => '\\1_\\2'));
return strtolower($tmp);
}
/**
* helper method
*/
public static function camelize($lower_case_and_underscored_word) {
$replace = str_replace(" ","", ucwords(str_replace("_", " ", $lower_case_and_underscored_word)));
return $replace;
}
/**
* helper method
*
* @param string $search
* @param string $replacePairs
* @return string
*/
protected static function replace($search, $replacePairs)
{
return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
}
/**
* it's a kind of magic, Freddy for ever
*
* @param string $funcName
* @param array $args
* @return mixed
*/
public function __call($funcName, $args) {
$method = self::getMethodType($funcName);
if(empty($this->register[$method->type])) {
throw new QCoreException("Unsupported method type ".$method->type." for ".$funcName.". Supported methods: ".implode(", ", array_keys($this->register)));
return false;
}
switch ($method->type)
{
case "has":
$num = count($args);
switch($num) {
case 1:
return (isset($this->{$method->name}[$args[0]])?true:false);
default:
return (isset($this->{$method->name})?true:false);
}
break;
case "add":
$this->{$method->name}[] = $args[0];
return $this;
case "merge":
foreach($args[0] as $key => $value) {
$this->{$method->name}[$key] = $value;
}
return $this;
case "set":
$num = count($args);
switch($num){
case 1:
$this->{$method->name} = $args[0];
break;
case 2:
$this->{$method->name}[$args[0]] = $args[1];
break;
case 3:
$this->{$method->name}[$args[0]][$args[1]] = $args[2];
break;
}
return $this;
case "get":
if(empty($args) and isset($this->{$method->name})) {
return $this->{$method->name};
} elseif(isset($args) and isset($this->{$method->name}[$args[0]]) ) {
return $this->{$method->name}[$args[0]];
} else {
return $this->{$method->name};
}
break;
}
}
}