Witam,
ostatnio dawno nie kodowałem w PHP właściwie większość czasu poświęcam JavaScript, dlatego postanowiłem napisać klasę(ObjectConverter) w celu poznania bliżej refleksji i odświeżenia umiejętności. Klasa ma na celu pobieranie wyników metod klasy podanej konwersji, metody są pobierane na podstawie członu nazwy np: wszystkie publiczne metody zaczynające się na "get" zostaną użyte do konwersji, po czym wynik zostanie zapisany do tablicy i zwrócony. Metody podczas konwersji mogą otrzymywać zewnętrznie parametry.
<?php
class ObjectConverter {
protected $object;
protected $params;
protected $notConvert;
protected $member = 'get';
protected
$methods = array();
protected
$methodsName = array();
protected
$quantityParams = array();
'object' => '%s is not an object',
'array' => '%s must be an array',
'not-param' => '%s expected parameter',
'not-exists' => 'public methods %s not exists',
'bad-quantity-param' => '%s bad quantity parameter in $params',
'too-many-param' => 'your method have too many parameter - allowed 5'
);
public function convert
($object, $params = array(), $not_convert = array()) {
$this->exception('object', '$object');
}
$this->exception('array', '$params');
}
$this->exception('array', '$not_convert');
}
$this->setObject($object);
$this->setParams($params);
$this->setNotConvert($not_convert);
return $this->prepareDataObject();
}
protected function setObject($obj) {
$this->object = $obj;
}
protected function setParams($params) {
$this->params = $params;
}
protected function setNotConvert($not_convert) {
}
protected function prepareDataObject() {
$methods = $this->reflectionClass()->getMethods();
for ($i = 0; $i < count($methods); $i++) {
if ($methods[$i]->isPublic()
&& $this->isMember($methods[$i]->name)
&& $this->isConvertMethod($methods[$i]->name))
{
$this->methods[] = $methods[$i];
$this->methodsName[] = $methods[$i]->name;
$this->quantityParams[] = $methods[$i]->getNumberOfRequiredParameters();
}
}
return count($this->methodsName) > 0 ?
$this : $this->exception('not-exists', $this->member); }
public function toArray() {
for ($i = 0; $i < count($this->methodsName); $i++) {
if (0 < $this->quantityParams[$i]) {
$result[$this->createIndex($i)] = $this->withParams($i);
} else {
$result[$this->createIndex($i)] = $this->withoutParams($i);
}
}
return $result;
}
protected function withParams($i) {
$method_name = $this->methodsName[$i];
if (!isset($this->params[$method_name])) { $this->exception('not-param', $method_name);
}
$params = $this->params[$method_name];
if ($this->quantityParams[$i] == count($this->params[$method_name])) {
switch ($this->quantityParams[$i]) {
case 1 : return $this->object->$method_name($params[0]);
break;
case 2 : return $this->object->$method_name($params[0], $params[1]);
break;
case 3 : return $this->object->$method_name($params[0], $params[1], $params[2]);
break;
case 4 : return $this->object->$method_name($params[0], $params[1], $params[2], $params[3]);
break;
case 5 : return $this->object->$method_name($params[0], $params[1], $params[2], $params[3], $params[4]);
break;
default : $this->exception('too-many-param', $method_name);
}
} else {
$this->exception('bad-quantity-param');
}
}
protected function reflectionClass() {
return new \ReflectionClass($this->object);
}
protected function withoutParams($i) {
$method_name = $this->methodsName[$i];
return $this->object->$method_name();
}
protected function createIndex($i) {
}
protected function isConvertMethod($method) {
return !isset($this->notConvert[$method]); }
protected function isMember($method) {
return $this->member == substr($method, 0
, strlen($this->member)); }
public function setMemberNameMethods($member) {
$this->member = $member;
return $this;
}
protected function exception($index, $var_name = null) {
throw
new Exception
(sprintf(self::$exceptionMessage[$index], $var_name)); }
}
?>
Poniżej zamieszczam dwa przykłady działania nie tłumacze dokładnie całego procesu zachodzącego w klasie, ponieważ zależy mi na waszej ocenie czytelności/przejrzystości kodu. Kod jest kompletny można go odpalić.
class Example {
protected $enlargement = 'txt';
protected $file = 'log';
public function getEnlargement() {
return $this->enlargement[0] !== '.' ? '.' . $this->enlargement : $this->enlargement;
}
public function getFileName() {
return $this->file;
}
public function getLengthFileName() {
}
}
$c = new ObjectConverter();
$example = new Example();
try {
$result = $c->convert($example)
->toArray();
} catch (Exception $e) {
}
/*
Array
(
[enlargement] => .txt
[file_name] => log
[length_file_name] => 3
)
*/
class Example2 {
public function calculateAvg($a, $b) {
return @($a / $b);
}
public function calculateSum($a, $b) {
return @($a * $b);
}
public function calculateDiff($a, $b) {
return $a - $b;
}
public function calculatePi($a) {
return 3.14 * $a;
}
}
$c = new ObjectConverter();
$example2 = new Example2();
'calculateAvg' => array(5, 25), 'calculateSum' => array(5, 55), 'calculateDiff' => array(20
, 5
), );
$notConvert = array('calculatePi');
try {
$result = $c->setMemberNameMethods('calculate')
->convert($example2, $params, $notConvert)
->toArray();
} catch (Exception $e) {
}
/*
Array
(
[avg] => 0.2
[sum] => 275
[diff] => 15
)
*/
Ten post edytował q3trm 10.12.2013, 23:39:03